From dcf2afb4a6215c542b7f45daaa549e570bd6c6bf Mon Sep 17 00:00:00 2001 From: zhengkunwang <31820853+zhengkunwang223@users.noreply.github.com> Date: Fri, 19 Jul 2024 15:33:28 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=88=B6=E7=9B=AE=E5=BD=95=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E5=92=8C=E7=94=A8=E6=88=B7=E7=BB=84=20(#5868)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refs https://github.com/1Panel-dev/1Panel/issues/5866 --- backend/app/api/v1/file.go | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/backend/app/api/v1/file.go b/backend/app/api/v1/file.go index 19b44eb57..70600cbaa 100644 --- a/backend/app/api/v1/file.go +++ b/backend/app/api/v1/file.go @@ -11,6 +11,7 @@ import ( "path/filepath" "strconv" "strings" + "syscall" "github.com/1Panel-dev/1Panel/backend/app/api/v1/helper" "github.com/1Panel-dev/1Panel/backend/app/dto" @@ -336,8 +337,12 @@ func (b *BaseApi) UploadFiles(c *gin.Context) { return } mode := info.Mode() - fileOp := files.NewFileOp() + stat, ok := info.Sys().(*syscall.Stat_t) + uid, gid := -1, -1 + if ok { + uid, gid = int(stat.Uid), int(stat.Gid) + } success := 0 failures := make(buserr.MultiErr) @@ -378,6 +383,9 @@ func (b *BaseApi) UploadFiles(c *gin.Context) { } else { _ = os.Chmod(dstFilename, mode) } + if uid != -1 && gid != -1 { + _ = os.Chown(dstFilename, uid, gid) + } success++ } if success == 0 { @@ -603,9 +611,17 @@ func mergeChunks(fileName string, fileDir string, dstDir string, chunkCount int, if mode == 0 { mode = 0755 } - if _, err := os.Stat(dstDir); err != nil && os.IsNotExist(err) { - if err = op.CreateDir(dstDir, mode); err != nil { - return err + uid, gid := -1, -1 + if info, err := os.Stat(dstDir); err != nil { + if os.IsNotExist(err) { + if err = op.CreateDir(dstDir, mode); err != nil { + return err + } + } + } else { + stat, ok := info.Sys().(*syscall.Stat_t) + if ok { + uid, gid = int(stat.Uid), int(stat.Gid) } } dstFileName := filepath.Join(dstDir, fileName) @@ -615,6 +631,7 @@ func mergeChunks(fileName string, fileDir string, dstDir string, chunkCount int, } else { mode = 0644 } + if overwrite { _ = os.Remove(dstFileName) } @@ -635,6 +652,9 @@ func mergeChunks(fileName string, fileDir string, dstDir string, chunkCount int, } _ = os.Remove(chunkPath) } + if uid != -1 && gid != -1 { + _ = os.Chown(dstFileName, uid, gid) + } return nil }