diff --git a/pkg/utils/file/file.go b/pkg/utils/file/file.go index bf89988..c411a8f 100644 --- a/pkg/utils/file/file.go +++ b/pkg/utils/file/file.go @@ -39,7 +39,7 @@ func CheckPermission(src string) bool { // IsNotExistMkDir create a directory if it does not exist func IsNotExistMkDir(src string) error { - if notExist := CheckNotExist(src); notExist == true { + if notExist := CheckNotExist(src); notExist { if err := MkDir(src); err != nil { return err } @@ -293,7 +293,7 @@ func WriteToPath(data []byte, path, name string) error { } //最终拼接 -func SpliceFiles(dir, path string, length int) error { +func SpliceFiles(dir, path string, length int, startPoint int) error { fullPath := path @@ -305,8 +305,8 @@ func SpliceFiles(dir, path string, length int) error { ) defer file.Close() bufferedWriter := bufio.NewWriter(file) - for i := 0; i < length; i++ { - data, err := ioutil.ReadFile(path + strconv.Itoa(i)) + for i := 0; i < length+startPoint; i++ { + data, err := ioutil.ReadFile(dir + "/" + strconv.Itoa(i+startPoint)) if err != nil { return err } diff --git a/route/v1/file.go b/route/v1/file.go index 0a5c83a..ce8145f 100644 --- a/route/v1/file.go +++ b/route/v1/file.go @@ -292,27 +292,23 @@ func GetFileUpload(c *gin.Context) { relative := c.Query("relativePath") fileName := c.Query("filename") - size, _ := strconv.ParseInt(c.Query("totalSize"), 10, 64) + chunkNumber := c.Query("chunkNumber") path := c.Query("path") + dirPath := "" if fileName != relative { - dirPath := strings.TrimSuffix(relative, fileName) + dirPath = strings.TrimSuffix(relative, fileName) file.MkDir(path + "/" + dirPath) } - path += "/" + relative - - if !file.CheckNotExist(path) { - f, _ := os.Stat(path) - - if f.Size() == size { - - c.JSON(200, model.Result{Success: 200, Message: oasis_err2.GetMsg(oasis_err2.FILE_ALREADY_EXISTS)}) - return - } - - os.Remove(path) - c.JSON(204, model.Result{Success: 204, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS)}) + hash := file.GetHashByContent([]byte(fileName)) + tempDir := path + "/" + if len(dirPath) > 0 { + tempDir += dirPath + "/" + hash + "/" + chunkNumber + } else { + tempDir += hash + "/" + chunkNumber + } + if !file.CheckNotExist(tempDir) { + c.JSON(200, model.Result{Success: 200, Message: oasis_err2.GetMsg(oasis_err2.FILE_ALREADY_EXISTS)}) return - } c.JSON(204, model.Result{Success: 204, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS)}) @@ -331,32 +327,66 @@ func PostFileUpload(c *gin.Context) { f, _, _ := c.Request.FormFile("file") relative := c.PostForm("relativePath") fileName := c.PostForm("filename") + totalChunks, _ := strconv.Atoi(c.DefaultPostForm("totalChunks", "0")) + chunkNumber := c.PostForm("chunkNumber") + dirPath := "" path := c.PostForm("path") + hash := file.GetHashByContent([]byte(fileName)) + if len(path) == 0 { c.JSON(oasis_err2.INVALID_PARAMS, model.Result{Success: oasis_err2.INVALID_PARAMS, Message: oasis_err2.GetMsg(oasis_err2.INVALID_PARAMS)}) return } if fileName != relative { - dirPath := strings.TrimSuffix(relative, fileName) + dirPath = strings.TrimSuffix(relative, fileName) file.MkDir(path + "/" + dirPath) } + tempDir := path + "/" + if len(dirPath) > 0 { + tempDir += dirPath + "/" + hash + } else { + tempDir += hash + } path += "/" + relative - if !file.CheckNotExist(path) { - + if !file.CheckNotExist(tempDir + "/" + chunkNumber) { c.JSON(oasis_err2.FILE_ALREADY_EXISTS, model.Result{Success: oasis_err2.FILE_ALREADY_EXISTS, Message: oasis_err2.GetMsg(oasis_err2.FILE_ALREADY_EXISTS)}) return } - out, _ := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0644) - defer out.Close() - _, err := io.Copy(out, f) + if totalChunks > 1 { + file.IsNotExistMkDir(tempDir) + + out, _ := os.OpenFile(tempDir+"/"+chunkNumber, os.O_WRONLY|os.O_CREATE, 0644) + defer out.Close() + _, err := io.Copy(out, f) + if err != nil { + c.JSON(oasis_err2.ERROR, model.Result{Success: oasis_err2.ERROR, Message: oasis_err2.GetMsg(oasis_err2.ERROR), Data: err.Error()}) + return + } + } else { + out, _ := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0644) + defer out.Close() + _, err := io.Copy(out, f) + if err != nil { + c.JSON(oasis_err2.ERROR, model.Result{Success: oasis_err2.ERROR, Message: oasis_err2.GetMsg(oasis_err2.ERROR), Data: err.Error()}) + return + } + c.JSON(http.StatusOK, model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS)}) + return + } + fileNum, err := ioutil.ReadDir(tempDir) if err != nil { c.JSON(oasis_err2.ERROR, model.Result{Success: oasis_err2.ERROR, Message: oasis_err2.GetMsg(oasis_err2.ERROR), Data: err.Error()}) return } + if totalChunks == len(fileNum) { + file.SpliceFiles(tempDir, path, totalChunks, 1) + file.RMDir(tempDir) + } + c.JSON(http.StatusOK, model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS)}) } diff --git a/service/udpconn.go b/service/udpconn.go index a476f82..c22c762 100644 --- a/service/udpconn.go +++ b/service/udpconn.go @@ -157,7 +157,7 @@ func ReadContent(stream quic.Stream) (model.MessageModel, error) { err = ioutil.WriteFile(filepath, dataByte, 0644) if dataModel.Index >= (dataModel.Length - 1) { - file.SpliceFiles("", path, dataModel.Length) + //file.SpliceFiles("", path, dataModel.Length) break } } else { diff --git a/types/system.go b/types/system.go index 22e307a..f9d3235 100644 --- a/types/system.go +++ b/types/system.go @@ -2,4 +2,4 @@ package types const CURRENTVERSION = "0.2.10" -const BODY = "