Skip to content

Commit

Permalink
feat: 提升分段上传文件的体验 (#5123)
Browse files Browse the repository at this point in the history
* feat: 提升分段上传文件的体验

* 原新建文件 mode 为755,修正为644
  • Loading branch information
qwenode committed May 24, 2024
1 parent 9c357f1 commit b80b67b
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions backend/app/api/v1/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ func (b *BaseApi) Size(c *gin.Context) {
helper.SuccessWithData(c, res)
}

func mergeChunks(fileName string, fileDir string, dstDir string, chunkCount int) error {
func mergeChunks(fileName string, fileDir string, dstDir string, chunkCount int, overwrite bool) error {
op := files.NewFileOp()
dstDir = strings.TrimSpace(dstDir)
mode, _ := files.GetParentMode(dstDir)
Expand All @@ -604,8 +604,17 @@ func mergeChunks(fileName string, fileDir string, dstDir string, chunkCount int)
return err
}
}

targetFile, err := os.OpenFile(filepath.Join(dstDir, fileName), os.O_RDWR|os.O_CREATE, mode)
dstFileName := filepath.Join(dstDir, fileName)
dstInfo, statErr := os.Stat(dstFileName)
if statErr == nil {
mode = dstInfo.Mode()
} else {
mode = 0644
}
if overwrite {
_ = os.Remove(dstFileName)
}
targetFile, err := os.OpenFile(dstFileName, os.O_RDWR|os.O_CREATE, mode)
if err != nil {
return err
}
Expand Down Expand Up @@ -704,7 +713,11 @@ func (b *BaseApi) UploadChunkFiles(c *gin.Context) {
}

if chunkIndex+1 == chunkCount {
err = mergeChunks(filename, fileDir, c.PostForm("path"), chunkCount)
overwrite := true
if ow := c.PostForm("overwrite"); ow != "" {
overwrite, _ = strconv.ParseBool(ow)
}
err = mergeChunks(filename, fileDir, c.PostForm("path"), chunkCount, overwrite)
if err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, buserr.WithMap(constant.ErrFileUpload, map[string]interface{}{"name": filename, "detail": err.Error()}, err))
return
Expand Down

0 comments on commit b80b67b

Please sign in to comment.