alist/internal/fs/copy.go

109 lines
3.9 KiB
Go
Raw Normal View History

2022-06-15 10:58:26 +00:00
package fs
import (
"context"
2022-06-17 13:23:44 +00:00
"fmt"
"net/http"
stdpath "path"
2022-06-21 08:14:37 +00:00
"sync/atomic"
2022-06-15 10:58:26 +00:00
"github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/op"
"github.com/alist-org/alist/v3/internal/stream"
2022-08-03 06:26:59 +00:00
"github.com/alist-org/alist/v3/pkg/task"
"github.com/alist-org/alist/v3/pkg/utils"
2022-06-15 10:58:26 +00:00
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
2022-06-15 10:58:26 +00:00
)
2022-06-28 10:12:53 +00:00
var CopyTaskManager = task.NewTaskManager(3, func(tid *uint64) {
2022-06-21 08:14:37 +00:00
atomic.AddUint64(tid, 1)
})
2022-06-17 07:57:16 +00:00
2022-07-10 06:45:39 +00:00
// Copy if in the same storage, call move method
2022-06-17 13:35:46 +00:00
// if not, add copy task
func _copy(ctx context.Context, srcObjPath, dstDirPath string, lazyCache ...bool) (bool, error) {
srcStorage, srcObjActualPath, err := op.GetStorageAndActualPath(srcObjPath)
2022-06-17 13:35:46 +00:00
if err != nil {
2022-07-10 06:45:39 +00:00
return false, errors.WithMessage(err, "failed get src storage")
2022-06-17 13:35:46 +00:00
}
dstStorage, dstDirActualPath, err := op.GetStorageAndActualPath(dstDirPath)
2022-06-17 13:35:46 +00:00
if err != nil {
2022-07-10 06:45:39 +00:00
return false, errors.WithMessage(err, "failed get dst storage")
2022-06-17 13:35:46 +00:00
}
2022-07-10 06:45:39 +00:00
// copy if in the same storage, just call driver.Copy
if srcStorage.GetStorage() == dstStorage.GetStorage() {
return false, op.Copy(ctx, srcStorage, srcObjActualPath, dstDirActualPath, lazyCache...)
2022-06-17 13:35:46 +00:00
}
2022-07-10 06:45:39 +00:00
// not in the same storage
2022-06-22 11:28:41 +00:00
CopyTaskManager.Submit(task.WithCancelCtx(&task.Task[uint64]{
Name: fmt.Sprintf("copy [%s](%s) to [%s](%s)", srcStorage.GetStorage().MountPath, srcObjActualPath, dstStorage.GetStorage().MountPath, dstDirActualPath),
2022-06-22 11:28:41 +00:00
Func: func(task *task.Task[uint64]) error {
2022-07-10 06:45:39 +00:00
return copyBetween2Storages(task, srcStorage, dstStorage, srcObjActualPath, dstDirActualPath)
2022-06-21 08:14:37 +00:00
},
}))
2022-06-17 13:35:46 +00:00
return true, nil
}
2022-07-10 06:45:39 +00:00
func copyBetween2Storages(t *task.Task[uint64], srcStorage, dstStorage driver.Driver, srcObjPath, dstDirPath string) error {
2022-06-18 12:06:45 +00:00
t.SetStatus("getting src object")
srcObj, err := op.Get(t.Ctx, srcStorage, srcObjPath)
2022-06-15 10:58:26 +00:00
if err != nil {
2022-06-21 08:37:51 +00:00
return errors.WithMessagef(err, "failed get src [%s] file", srcObjPath)
2022-06-15 10:58:26 +00:00
}
2022-06-17 13:23:44 +00:00
if srcObj.IsDir() {
2022-06-18 12:06:45 +00:00
t.SetStatus("src object is dir, listing objs")
objs, err := op.List(t.Ctx, srcStorage, srcObjPath, model.ListArgs{})
2022-06-15 10:58:26 +00:00
if err != nil {
2022-06-21 08:37:51 +00:00
return errors.WithMessagef(err, "failed list src [%s] objs", srcObjPath)
2022-06-15 10:58:26 +00:00
}
2022-06-17 13:30:16 +00:00
for _, obj := range objs {
2022-06-18 12:06:45 +00:00
if utils.IsCanceled(t.Ctx) {
2022-06-17 13:23:44 +00:00
return nil
}
2022-06-21 08:37:51 +00:00
srcObjPath := stdpath.Join(srcObjPath, obj.GetName())
dstObjPath := stdpath.Join(dstDirPath, srcObj.GetName())
2022-06-22 11:28:41 +00:00
CopyTaskManager.Submit(task.WithCancelCtx(&task.Task[uint64]{
Name: fmt.Sprintf("copy [%s](%s) to [%s](%s)", srcStorage.GetStorage().MountPath, srcObjPath, dstStorage.GetStorage().MountPath, dstObjPath),
2022-06-22 11:28:41 +00:00
Func: func(t *task.Task[uint64]) error {
2022-07-10 06:45:39 +00:00
return copyBetween2Storages(t, srcStorage, dstStorage, srcObjPath, dstObjPath)
2022-06-21 08:14:37 +00:00
},
}))
2022-06-15 10:58:26 +00:00
}
2022-06-17 13:23:44 +00:00
} else {
2022-06-22 11:28:41 +00:00
CopyTaskManager.Submit(task.WithCancelCtx(&task.Task[uint64]{
Name: fmt.Sprintf("copy [%s](%s) to [%s](%s)", srcStorage.GetStorage().MountPath, srcObjPath, dstStorage.GetStorage().MountPath, dstDirPath),
2022-06-22 11:28:41 +00:00
Func: func(t *task.Task[uint64]) error {
err := copyFileBetween2Storages(t, srcStorage, dstStorage, srcObjPath, dstDirPath)
log.Debugf("copy file between storages: %+v", err)
return err
2022-06-21 08:14:37 +00:00
},
}))
2022-06-17 13:23:44 +00:00
}
return nil
}
2022-07-10 06:45:39 +00:00
func copyFileBetween2Storages(tsk *task.Task[uint64], srcStorage, dstStorage driver.Driver, srcFilePath, dstDirPath string) error {
srcFile, err := op.Get(tsk.Ctx, srcStorage, srcFilePath)
2022-06-17 13:23:44 +00:00
if err != nil {
2022-06-21 08:37:51 +00:00
return errors.WithMessagef(err, "failed get src [%s] file", srcFilePath)
2022-06-15 10:58:26 +00:00
}
link, _, err := op.Link(tsk.Ctx, srcStorage, srcFilePath, model.LinkArgs{
Header: http.Header{},
})
2022-06-15 10:58:26 +00:00
if err != nil {
2022-06-21 08:37:51 +00:00
return errors.WithMessagef(err, "failed get [%s] link", srcFilePath)
2022-06-15 10:58:26 +00:00
}
fs := stream.FileStream{
Obj: srcFile,
Ctx: tsk.Ctx,
}
// any link provided is seekable
ss, err := stream.NewSeekableStream(fs, link)
2022-06-15 10:58:26 +00:00
if err != nil {
2022-06-21 08:37:51 +00:00
return errors.WithMessagef(err, "failed get [%s] stream", srcFilePath)
2022-06-15 10:58:26 +00:00
}
return op.Put(tsk.Ctx, dstStorage, dstDirPath, ss, tsk.SetProgress, true)
2022-06-15 10:58:26 +00:00
}