mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-01-31 22:18:07 +08:00
提升文件上传体验 (#4393)
* 上传文件支持覆盖被占用的文件且上传失败时不影响原文件,增加强制覆盖选项 * 原文件存在时,保持同样的文件权限 * 制表符更新
This commit is contained in:
parent
08da7802c6
commit
e3eebc938a
@ -303,6 +303,15 @@ func (b *BaseApi) UploadFiles(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
files := form.File["file"]
|
files := form.File["file"]
|
||||||
paths := form.Value["path"]
|
paths := form.Value["path"]
|
||||||
|
|
||||||
|
overwrite := true
|
||||||
|
if ow, ok := form.Value["overwrite"]; ok {
|
||||||
|
if len(ow) != 0 {
|
||||||
|
parseBool, _ := strconv.ParseBool(ow[0])
|
||||||
|
overwrite = parseBool
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if len(paths) == 0 || !strings.Contains(paths[0], "/") {
|
if len(paths) == 0 || !strings.Contains(paths[0], "/") {
|
||||||
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, errors.New("error paths in request"))
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, errors.New("error paths in request"))
|
||||||
return
|
return
|
||||||
@ -317,12 +326,31 @@ func (b *BaseApi) UploadFiles(c *gin.Context) {
|
|||||||
success := 0
|
success := 0
|
||||||
failures := make(buserr.MultiErr)
|
failures := make(buserr.MultiErr)
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
if err := c.SaveUploadedFile(file, path.Join(paths[0], file.Filename)); err != nil {
|
dstFilename := path.Join(paths[0], file.Filename)
|
||||||
|
tmpFilename := dstFilename + ".tmp"
|
||||||
|
if err := c.SaveUploadedFile(file, tmpFilename); err != nil {
|
||||||
|
_ = os.Remove(tmpFilename)
|
||||||
e := fmt.Errorf("upload [%s] file failed, err: %v", file.Filename, err)
|
e := fmt.Errorf("upload [%s] file failed, err: %v", file.Filename, err)
|
||||||
failures[file.Filename] = e
|
failures[file.Filename] = e
|
||||||
global.LOG.Error(e)
|
global.LOG.Error(e)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
stat, statErr := os.Stat(dstFilename)
|
||||||
|
if overwrite {
|
||||||
|
_ = os.Remove(dstFilename)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.Rename(tmpFilename, dstFilename)
|
||||||
|
if err != nil {
|
||||||
|
_ = os.Remove(tmpFilename)
|
||||||
|
e := fmt.Errorf("upload [%s] file failed, err: %v", file.Filename, err)
|
||||||
|
failures[file.Filename] = e
|
||||||
|
global.LOG.Error(e)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if statErr == nil {
|
||||||
|
_ = os.Chmod(dstFilename, stat.Mode())
|
||||||
|
}
|
||||||
success++
|
success++
|
||||||
}
|
}
|
||||||
if success == 0 {
|
if success == 0 {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user