feat: 上传文件使用父目录用户和用户组 (#5868)

Refs https://github.com/1Panel-dev/1Panel/issues/5866
pull/5869/head
zhengkunwang 4 months ago committed by GitHub
parent 16a35141dd
commit dcf2afb4a6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -11,6 +11,7 @@ import (
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"syscall"
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper" "github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
"github.com/1Panel-dev/1Panel/backend/app/dto" "github.com/1Panel-dev/1Panel/backend/app/dto"
@ -336,8 +337,12 @@ func (b *BaseApi) UploadFiles(c *gin.Context) {
return return
} }
mode := info.Mode() mode := info.Mode()
fileOp := files.NewFileOp() 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 success := 0
failures := make(buserr.MultiErr) failures := make(buserr.MultiErr)
@ -378,6 +383,9 @@ func (b *BaseApi) UploadFiles(c *gin.Context) {
} else { } else {
_ = os.Chmod(dstFilename, mode) _ = os.Chmod(dstFilename, mode)
} }
if uid != -1 && gid != -1 {
_ = os.Chown(dstFilename, uid, gid)
}
success++ success++
} }
if success == 0 { if success == 0 {
@ -603,9 +611,17 @@ func mergeChunks(fileName string, fileDir string, dstDir string, chunkCount int,
if mode == 0 { if mode == 0 {
mode = 0755 mode = 0755
} }
if _, err := os.Stat(dstDir); err != nil && os.IsNotExist(err) { uid, gid := -1, -1
if err = op.CreateDir(dstDir, mode); err != nil { if info, err := os.Stat(dstDir); err != nil {
return err 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) dstFileName := filepath.Join(dstDir, fileName)
@ -615,6 +631,7 @@ func mergeChunks(fileName string, fileDir string, dstDir string, chunkCount int,
} else { } else {
mode = 0644 mode = 0644
} }
if overwrite { if overwrite {
_ = os.Remove(dstFileName) _ = os.Remove(dstFileName)
} }
@ -635,6 +652,9 @@ func mergeChunks(fileName string, fileDir string, dstDir string, chunkCount int,
} }
_ = os.Remove(chunkPath) _ = os.Remove(chunkPath)
} }
if uid != -1 && gid != -1 {
_ = os.Chown(dstFileName, uid, gid)
}
return nil return nil
} }

Loading…
Cancel
Save