Skip to content

Commit

Permalink
feat(webdav): support copy directly without task (close #5206)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Sep 13, 2023
1 parent 1090155 commit 352a6a7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
7 changes: 6 additions & 1 deletion internal/conf/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const (
OcrApi = "ocr_api"
FilenameCharMapping = "filename_char_mapping"
ForwardDirectLinkParams = "forward_direct_link_params"
WebauthnLoginEnabled = "webauthn_login_enabled"
WebauthnLoginEnabled = "webauthn_login_enabled"

// index
SearchIndex = "search_index"
Expand Down Expand Up @@ -84,3 +84,8 @@ const (
TEXT
IMAGE
)

// ContextKey is the type of context keys.
const (
NoTaskKey = "no_task"
)
26 changes: 26 additions & 0 deletions internal/fs/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
stdpath "path"
"sync/atomic"

"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/op"
Expand Down Expand Up @@ -36,6 +37,31 @@ func _copy(ctx context.Context, srcObjPath, dstDirPath string, lazyCache ...bool
if srcStorage.GetStorage() == dstStorage.GetStorage() {
return false, op.Copy(ctx, srcStorage, srcObjActualPath, dstDirActualPath, lazyCache...)
}
if ctx.Value(conf.NoTaskKey) != nil {
srcObj, err := op.Get(ctx, srcStorage, srcObjActualPath)
if err != nil {
return false, errors.WithMessagef(err, "failed get src [%s] file", srcObjPath)
}
if !srcObj.IsDir() {
// copy file directly
link, _, err := op.Link(ctx, srcStorage, srcObjActualPath, model.LinkArgs{
Header: http.Header{},
})
if err != nil {
return false, errors.WithMessagef(err, "failed get [%s] link", srcObjPath)
}
fs := stream.FileStream{
Obj: srcObj,
Ctx: ctx,
}
// any link provided is seekable
ss, err := stream.NewSeekableStream(fs, link)
if err != nil {
return false, errors.WithMessagef(err, "failed get [%s] stream", srcObjPath)
}
return false, op.Put(ctx, dstStorage, dstDirActualPath, ss, nil, false)
}
}
// not in the same storage
CopyTaskManager.Submit(task.WithCancelCtx(&task.Task[uint64]{
Name: fmt.Sprintf("copy [%s](%s) to [%s](%s)", srcStorage.GetStorage().MountPath, srcObjActualPath, dstStorage.GetStorage().MountPath, dstDirActualPath),
Expand Down
4 changes: 3 additions & 1 deletion server/webdav/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path"
"path/filepath"

"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/fs"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/op"
Expand Down Expand Up @@ -54,7 +55,8 @@ func moveFiles(ctx context.Context, src, dst string, overwrite bool) (status int
//
// See section 9.8.5 for when various HTTP status codes apply.
func copyFiles(ctx context.Context, src, dst string, overwrite bool) (status int, err error) {
_, err = fs.Copy(ctx, src, dst)
dstDir := path.Dir(dst)
_, err = fs.Copy(context.WithValue(ctx, conf.NoTaskKey, struct{}{}), src, dstDir)
if err != nil {
return http.StatusInternalServerError, err
}
Expand Down

0 comments on commit 352a6a7

Please sign in to comment.