Skip to content

Commit eadf03a

Browse files
jyxjjjcodex
andauthored
fix(upload): respect overwrite for direct uploads (#2625)
* fix(upload): respect overwrite for direct uploads - 将 Direct Upload 的 overwrite 参数传递到后端检查逻辑 - 覆盖上传时允许已存在目标继续生成直传信息 - 非覆盖上传时按完整目标路径返回 file exists Co-authored-by: Codex <267193182+codex@users.noreply.github.com> Signed-off-by: jyxjjj <16695261+jyxjjj@users.noreply.github.com> * fix(upload): validate direct upload target checks - 校验直传文件名只能是单个路径段 - 在非覆盖直传检查中返回非 object not found 错误 - 在底层直传信息生成前保留真实探测错误 Co-authored-by: Codex <267193182+codex@users.noreply.github.com> Signed-off-by: jyxjjj <16695261+jyxjjj@users.noreply.github.com> * fix(upload): preserve direct upload conflict status - 将非覆盖直传的并发已存在错误返回为 403 - 保持直传存在性检查的前端错误文案不暴露路径 Co-authored-by: Codex <267193182+codex@users.noreply.github.com> Signed-off-by: jyxjjj <16695261+jyxjjj@users.noreply.github.com> --------- Signed-off-by: jyxjjj <16695261+jyxjjj@users.noreply.github.com> Co-authored-by: Codex <267193182+codex@users.noreply.github.com>
1 parent da84a80 commit eadf03a

5 files changed

Lines changed: 33 additions & 11 deletions

File tree

internal/fs/fs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ func PutURL(ctx context.Context, path, dstName, urlStr string) error {
199199
return op.PutURL(ctx, storage, dstDirActualPath, dstName, urlStr)
200200
}
201201

202-
func GetDirectUploadInfo(ctx context.Context, tool, path, dstName string, fileSize int64) (any, error) {
203-
info, err := getDirectUploadInfo(ctx, tool, path, dstName, fileSize)
202+
func GetDirectUploadInfo(ctx context.Context, tool, path, dstName string, fileSize int64, overwrite bool) (any, error) {
203+
info, err := getDirectUploadInfo(ctx, tool, path, dstName, fileSize, overwrite)
204204
if err != nil {
205205
log.Errorf("failed get %s direct upload info for %s(%d bytes): %+v", path, dstName, fileSize, err)
206206
}

internal/fs/put.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ func putDirectly(ctx context.Context, dstDirPath string, file model.FileStreamer
110110
return op.Put(ctx, storage, dstDirActualPath, file, nil)
111111
}
112112

113-
func getDirectUploadInfo(ctx context.Context, tool, dstDirPath, dstName string, fileSize int64) (any, error) {
113+
func getDirectUploadInfo(ctx context.Context, tool, dstDirPath, dstName string, fileSize int64, overwrite bool) (any, error) {
114114
storage, dstDirActualPath, err := op.GetStorageAndActualPath(dstDirPath)
115115
if err != nil {
116116
return nil, errors.WithMessage(err, "failed get storage")
117117
}
118-
return op.GetDirectUploadInfo(ctx, tool, storage, dstDirActualPath, dstName, fileSize)
118+
return op.GetDirectUploadInfo(ctx, tool, storage, dstDirActualPath, dstName, fileSize, overwrite)
119119
}

internal/op/fs.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ func GetDirectUploadTools(storage driver.Driver) []string {
779779
return du.GetDirectUploadTools()
780780
}
781781

782-
func GetDirectUploadInfo(ctx context.Context, tool string, storage driver.Driver, dstDirPath, dstName string, fileSize int64) (any, error) {
782+
func GetDirectUploadInfo(ctx context.Context, tool string, storage driver.Driver, dstDirPath, dstName string, fileSize int64, overwrite bool) (any, error) {
783783
du, ok := storage.(driver.DirectUploader)
784784
if !ok {
785785
return nil, errors.WithStack(errs.NotImplement)
@@ -789,9 +789,15 @@ func GetDirectUploadInfo(ctx context.Context, tool string, storage driver.Driver
789789
}
790790
dstDirPath = utils.FixAndCleanPath(dstDirPath)
791791
dstPath := stdpath.Join(dstDirPath, dstName)
792-
_, err := Get(ctx, storage, dstPath)
793-
if err == nil {
794-
return nil, errors.WithStack(errs.ObjectAlreadyExists)
792+
var err error
793+
if !overwrite {
794+
_, err = Get(ctx, storage, dstPath)
795+
if err == nil {
796+
return nil, errors.WithStack(errs.ObjectAlreadyExists)
797+
}
798+
if !errs.IsObjectNotFound(err) {
799+
return nil, errors.WithMessage(err, "failed to check if object exists")
800+
}
795801
}
796802
err = MakeDir(ctx, storage, dstDirPath)
797803
if err != nil {

server/handles/direct_upload.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package handles
22

33
import (
44
"net/url"
5+
stdpath "path"
56

67
"github.com/OpenListTeam/OpenList/v4/internal/conf"
8+
"github.com/OpenListTeam/OpenList/v4/internal/errs"
79
"github.com/OpenListTeam/OpenList/v4/internal/fs"
810
"github.com/OpenListTeam/OpenList/v4/internal/model"
911
"github.com/OpenListTeam/OpenList/v4/server/common"
@@ -38,15 +40,29 @@ func FsGetDirectUploadInfo(c *gin.Context) {
3840
common.ErrorResp(c, err, 403)
3941
return
4042
}
43+
if err := checkRelativePath(req.FileName); err != nil {
44+
common.ErrorResp(c, err, 403)
45+
return
46+
}
4147
overwrite := c.GetHeader("Overwrite") != "false"
48+
dstPath := stdpath.Join(path, req.FileName)
4249
if !overwrite {
43-
if res, _ := fs.Get(c.Request.Context(), path, &fs.GetArgs{NoLog: true}); res != nil {
50+
res, err := fs.Get(c.Request.Context(), dstPath, &fs.GetArgs{NoLog: true})
51+
if err != nil && !errs.IsObjectNotFound(err) {
52+
common.ErrorResp(c, err, 500)
53+
return
54+
}
55+
if res != nil {
4456
common.ErrorStrResp(c, "file exists", 403)
4557
return
4658
}
4759
}
48-
directUploadInfo, err := fs.GetDirectUploadInfo(c, req.Tool, path, req.FileName, req.FileSize)
60+
directUploadInfo, err := fs.GetDirectUploadInfo(c, req.Tool, path, req.FileName, req.FileSize, overwrite)
4961
if err != nil {
62+
if !overwrite && errs.IsObjectAlreadyExists(err) {
63+
common.ErrorStrResp(c, "file exists", 403)
64+
return
65+
}
5066
common.ErrorResp(c, err, 500)
5167
return
5268
}

server/s3/redirect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func directUploadURL(r *http.Request, authPairs map[string]string) (string, bool
9898
if err != nil || storage.Config().NoUpload {
9999
return "", false
100100
}
101-
info, err := op.GetDirectUploadInfo(r.Context(), "HttpDirect", storage, dstDirActualPath, path.Base(reqPath), r.ContentLength)
101+
info, err := op.GetDirectUploadInfo(r.Context(), "HttpDirect", storage, dstDirActualPath, path.Base(reqPath), r.ContentLength, true)
102102
if err != nil {
103103
return "", false
104104
}

0 commit comments

Comments
 (0)