Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ func PutURL(ctx context.Context, path, dstName, urlStr string) error {
return op.PutURL(ctx, storage, dstDirActualPath, dstName, urlStr)
}

func GetDirectUploadInfo(ctx context.Context, tool, path, dstName string, fileSize int64) (any, error) {
info, err := getDirectUploadInfo(ctx, tool, path, dstName, fileSize)
func GetDirectUploadInfo(ctx context.Context, tool, path, dstName string, fileSize int64, overwrite bool) (any, error) {
info, err := getDirectUploadInfo(ctx, tool, path, dstName, fileSize, overwrite)
if err != nil {
log.Errorf("failed get %s direct upload info for %s(%d bytes): %+v", path, dstName, fileSize, err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/fs/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ func putDirectly(ctx context.Context, dstDirPath string, file model.FileStreamer
return op.Put(ctx, storage, dstDirActualPath, file, nil)
}

func getDirectUploadInfo(ctx context.Context, tool, dstDirPath, dstName string, fileSize int64) (any, error) {
func getDirectUploadInfo(ctx context.Context, tool, dstDirPath, dstName string, fileSize int64, overwrite bool) (any, error) {
storage, dstDirActualPath, err := op.GetStorageAndActualPath(dstDirPath)
if err != nil {
return nil, errors.WithMessage(err, "failed get storage")
}
return op.GetDirectUploadInfo(ctx, tool, storage, dstDirActualPath, dstName, fileSize)
return op.GetDirectUploadInfo(ctx, tool, storage, dstDirActualPath, dstName, fileSize, overwrite)
}
14 changes: 10 additions & 4 deletions internal/op/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ func GetDirectUploadTools(storage driver.Driver) []string {
return du.GetDirectUploadTools()
}

func GetDirectUploadInfo(ctx context.Context, tool string, storage driver.Driver, dstDirPath, dstName string, fileSize int64) (any, error) {
func GetDirectUploadInfo(ctx context.Context, tool string, storage driver.Driver, dstDirPath, dstName string, fileSize int64, overwrite bool) (any, error) {
du, ok := storage.(driver.DirectUploader)
if !ok {
return nil, errors.WithStack(errs.NotImplement)
Expand All @@ -789,9 +789,15 @@ func GetDirectUploadInfo(ctx context.Context, tool string, storage driver.Driver
}
dstDirPath = utils.FixAndCleanPath(dstDirPath)
dstPath := stdpath.Join(dstDirPath, dstName)
_, err := Get(ctx, storage, dstPath)
if err == nil {
return nil, errors.WithStack(errs.ObjectAlreadyExists)
var err error
if !overwrite {
_, err = Get(ctx, storage, dstPath)
if err == nil {
return nil, errors.WithStack(errs.ObjectAlreadyExists)
}
if !errs.IsObjectNotFound(err) {
return nil, errors.WithMessage(err, "failed to check if object exists")
}
Comment thread
jyxjjj marked this conversation as resolved.
}
err = MakeDir(ctx, storage, dstDirPath)
if err != nil {
Expand Down
20 changes: 18 additions & 2 deletions server/handles/direct_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package handles

import (
"net/url"
stdpath "path"

"github.com/OpenListTeam/OpenList/v4/internal/conf"
"github.com/OpenListTeam/OpenList/v4/internal/errs"
"github.com/OpenListTeam/OpenList/v4/internal/fs"
"github.com/OpenListTeam/OpenList/v4/internal/model"
"github.com/OpenListTeam/OpenList/v4/server/common"
Expand Down Expand Up @@ -38,15 +40,29 @@ func FsGetDirectUploadInfo(c *gin.Context) {
common.ErrorResp(c, err, 403)
return
}
if err := checkRelativePath(req.FileName); err != nil {
common.ErrorResp(c, err, 403)
return
}
overwrite := c.GetHeader("Overwrite") != "false"
dstPath := stdpath.Join(path, req.FileName)
if !overwrite {
Comment thread
jyxjjj marked this conversation as resolved.
if res, _ := fs.Get(c.Request.Context(), path, &fs.GetArgs{NoLog: true}); res != nil {
res, err := fs.Get(c.Request.Context(), dstPath, &fs.GetArgs{NoLog: true})
if err != nil && !errs.IsObjectNotFound(err) {
common.ErrorResp(c, err, 500)
return
}
if res != nil {
common.ErrorStrResp(c, "file exists", 403)
return
}
}
directUploadInfo, err := fs.GetDirectUploadInfo(c, req.Tool, path, req.FileName, req.FileSize)
directUploadInfo, err := fs.GetDirectUploadInfo(c, req.Tool, path, req.FileName, req.FileSize, overwrite)
if err != nil {
if !overwrite && errs.IsObjectAlreadyExists(err) {
common.ErrorStrResp(c, "file exists", 403)
return
}
common.ErrorResp(c, err, 500)
return
Comment thread
jyxjjj marked this conversation as resolved.
}
Expand Down
2 changes: 1 addition & 1 deletion server/s3/redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func directUploadURL(r *http.Request, authPairs map[string]string) (string, bool
if err != nil || storage.Config().NoUpload {
return "", false
}
info, err := op.GetDirectUploadInfo(r.Context(), "HttpDirect", storage, dstDirActualPath, path.Base(reqPath), r.ContentLength)
info, err := op.GetDirectUploadInfo(r.Context(), "HttpDirect", storage, dstDirActualPath, path.Base(reqPath), r.ContentLength, true)
if err != nil {
return "", false
}
Expand Down
Loading