Skip to content
Open
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
3 changes: 2 additions & 1 deletion internal/conf/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,6 @@ const (

// ContextKey is the type of context keys.
const (
NoTaskKey = "no_task"
NoTaskKey = "no_task"
SkipExistingKey = "skip_existing"
)
23 changes: 23 additions & 0 deletions internal/fs/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type CopyTask struct {
dstStorage driver.Driver `json:"-"`
SrcStorageMp string `json:"src_storage_mp"`
DstStorageMp string `json:"dst_storage_mp"`
SkipExisting bool `json:"skip_existing"`
}

func (t *CopyTask) GetName() string {
Expand Down Expand Up @@ -69,8 +70,18 @@ func _copy(ctx context.Context, srcObjPath, dstDirPath string, lazyCache ...bool
if err != nil {
return nil, errors.WithMessage(err, "failed get dst storage")
}
skipExisting := ctx.Value(conf.SkipExistingKey) != nil
// copy if in the same storage, just call driver.Copy
if srcStorage.GetStorage() == dstStorage.GetStorage() {
if skipExisting {
if srcObj, err := op.Get(ctx, srcStorage, srcObjActualPath); err == nil && !srcObj.IsDir() {
dstFilePath := stdpath.Join(dstDirActualPath, srcObj.GetName())
if dstFile, err := op.Get(ctx, dstStorage, dstFilePath); err == nil &&
!dstFile.IsDir() && dstFile.GetSize() == srcObj.GetSize() {
return nil, nil
}
}
}
err = op.Copy(ctx, srcStorage, srcObjActualPath, dstDirActualPath, lazyCache...)
if !errors.Is(err, errs.NotImplement) && !errors.Is(err, errs.NotSupport) {
return nil, err
Expand Down Expand Up @@ -113,6 +124,7 @@ func _copy(ctx context.Context, srcObjPath, dstDirPath string, lazyCache ...bool
DstDirPath: dstDirActualPath,
SrcStorageMp: srcStorage.GetStorage().MountPath,
DstStorageMp: dstStorage.GetStorage().MountPath,
SkipExisting: skipExisting,
}
CopyTaskManager.Add(t)
return t, nil
Expand Down Expand Up @@ -146,6 +158,7 @@ func copyBetween2Storages(t *CopyTask, srcStorage, dstStorage driver.Driver, src
DstDirPath: dstObjPath,
SrcStorageMp: srcStorage.GetStorage().MountPath,
DstStorageMp: dstStorage.GetStorage().MountPath,
SkipExisting: t.SkipExisting,
})
}
t.Status = "src object is dir, added all copy tasks of objs"
Expand All @@ -160,6 +173,16 @@ func copyFileBetween2Storages(tsk *CopyTask, srcStorage, dstStorage driver.Drive
return errors.WithMessagef(err, "failed get src [%s] file", srcFilePath)
}
tsk.SetTotalBytes(srcFile.GetSize())
if tsk.SkipExisting {
dstFilePath := stdpath.Join(dstDirPath, srcFile.GetName())
// a failed probe falls through to a normal copy, worst case is a redundant transfer
if dstFile, err := op.Get(tsk.Ctx(), dstStorage, dstFilePath); err == nil &&
!dstFile.IsDir() && dstFile.GetSize() == srcFile.GetSize() {
tsk.Status = "skipped: destination file already exists"
tsk.SetProgress(100)
return nil
}
}
link, _, err := op.Link(tsk.Ctx(), srcStorage, srcFilePath, model.LinkArgs{
Header: http.Header{},
})
Expand Down
13 changes: 11 additions & 2 deletions server/handles/fsmanage.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package handles

import (
"context"
"fmt"
"io"
stdpath "path"

"github.com/alist-org/alist/v3/internal/task"

"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/errs"
"github.com/alist-org/alist/v3/internal/fs"
"github.com/alist-org/alist/v3/internal/model"
Expand Down Expand Up @@ -66,6 +68,9 @@ type MoveCopyReq struct {
DstDir string `json:"dst_dir"`
Names []string `json:"names"`
Overwrite bool `json:"overwrite"`
// SkipExisting only takes effect on copy: existing destination files
// with the same size are skipped instead of failing the request
SkipExisting bool `json:"skip_existing"`
}

func FsMove(c *gin.Context) {
Expand Down Expand Up @@ -169,7 +174,7 @@ func FsCopy(c *gin.Context) {
common.ErrorResp(c, errs.PermissionDenied, 403)
return
}
if !req.Overwrite {
if !req.Overwrite && !req.SkipExisting {
for _, name := range req.Names {
dstPath, err := utils.JoinUnderBase(dstDir, name)
if err != nil {
Expand All @@ -182,6 +187,10 @@ func FsCopy(c *gin.Context) {
}
}
}
var ctx context.Context = c
if req.SkipExisting {
ctx = context.WithValue(ctx, conf.SkipExistingKey, struct{}{})
}
var addedTasks []task.TaskExtensionInfo
for i, name := range req.Names {
srcPath, err := utils.JoinUnderBase(srcDir, name)
Expand All @@ -194,7 +203,7 @@ func FsCopy(c *gin.Context) {
common.ErrorResp(c, err, 400)
return
}
t, err := fs.Copy(c, srcPath, dstDir, len(req.Names) > i+1)
t, err := fs.Copy(ctx, srcPath, dstDir, len(req.Names) > i+1)
if t != nil {
addedTasks = append(addedTasks, t)
}
Expand Down
Loading