alist/internal/fs/copy.go

179 lines
5.6 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"
"github.com/alist-org/alist/v3/internal/errs"
"net/http"
stdpath "path"
2024-12-25 13:09:54 +00:00
"time"
"github.com/alist-org/alist/v3/internal/conf"
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"
"github.com/alist-org/alist/v3/internal/task"
2022-08-03 06:26:59 +00:00
"github.com/alist-org/alist/v3/pkg/utils"
2022-06-15 10:58:26 +00:00
"github.com/pkg/errors"
2023-11-20 10:01:51 +00:00
"github.com/xhofe/tache"
2022-06-15 10:58:26 +00:00
)
2023-11-20 10:01:51 +00:00
type CopyTask struct {
2024-12-25 13:09:54 +00:00
task.TaskExtension
Status string `json:"-"` //don't save status to save space
SrcObjPath string `json:"src_path"`
DstDirPath string `json:"dst_path"`
srcStorage driver.Driver `json:"-"`
dstStorage driver.Driver `json:"-"`
SrcStorageMp string `json:"src_storage_mp"`
DstStorageMp string `json:"dst_storage_mp"`
2023-11-20 10:01:51 +00:00
}
func (t *CopyTask) GetName() string {
return fmt.Sprintf("copy [%s](%s) to [%s](%s)", t.SrcStorageMp, t.SrcObjPath, t.DstStorageMp, t.DstDirPath)
2023-11-20 10:01:51 +00:00
}
func (t *CopyTask) GetStatus() string {
return t.Status
}
func (t *CopyTask) Run() error {
2024-12-25 13:09:54 +00:00
t.ClearEndTime()
t.SetStartTime(time.Now())
defer func() { t.SetEndTime(time.Now()) }()
var err error
if t.srcStorage == nil {
t.srcStorage, err = op.GetStorageByMountPath(t.SrcStorageMp)
}
if t.dstStorage == nil {
t.dstStorage, err = op.GetStorageByMountPath(t.DstStorageMp)
}
if err != nil {
return errors.WithMessage(err, "failed get storage")
}
return copyBetween2Storages(t, t.srcStorage, t.dstStorage, t.SrcObjPath, t.DstDirPath)
2023-11-20 10:01:51 +00:00
}
var CopyTaskManager *tache.Manager[*CopyTask]
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
2024-12-25 13:09:54 +00:00
func _copy(ctx context.Context, srcObjPath, dstDirPath string, lazyCache ...bool) (task.TaskExtensionInfo, error) {
srcStorage, srcObjActualPath, err := op.GetStorageAndActualPath(srcObjPath)
2022-06-17 13:35:46 +00:00
if err != nil {
return nil, 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 {
return nil, 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() {
err = op.Copy(ctx, srcStorage, srcObjActualPath, dstDirActualPath, lazyCache...)
if !errors.Is(err, errs.NotImplement) && !errors.Is(err, errs.NotSupport) {
return nil, err
}
2022-06-17 13:35:46 +00:00
}
if ctx.Value(conf.NoTaskKey) != nil {
srcObj, err := op.Get(ctx, srcStorage, srcObjActualPath)
if err != nil {
return nil, 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 nil, 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 nil, errors.WithMessagef(err, "failed get [%s] stream", srcObjPath)
}
return nil, op.Put(ctx, dstStorage, dstDirActualPath, ss, nil, false)
}
}
2022-07-10 06:45:39 +00:00
// not in the same storage
2024-12-25 13:09:54 +00:00
taskCreator, _ := ctx.Value("user").(*model.User)
t := &CopyTask{
2024-12-25 13:09:54 +00:00
TaskExtension: task.TaskExtension{
Creator: taskCreator,
},
srcStorage: srcStorage,
dstStorage: dstStorage,
SrcObjPath: srcObjActualPath,
DstDirPath: dstDirActualPath,
SrcStorageMp: srcStorage.GetStorage().MountPath,
DstStorageMp: dstStorage.GetStorage().MountPath,
}
CopyTaskManager.Add(t)
return t, nil
2022-06-17 13:35:46 +00:00
}
2023-11-20 10:01:51 +00:00
func copyBetween2Storages(t *CopyTask, srcStorage, dstStorage driver.Driver, srcObjPath, dstDirPath string) error {
t.Status = "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() {
2023-11-20 10:01:51 +00:00
t.Status = "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 {
2023-11-20 10:01:51 +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())
2023-11-20 10:01:51 +00:00
CopyTaskManager.Add(&CopyTask{
2024-12-25 13:09:54 +00:00
TaskExtension: task.TaskExtension{
Creator: t.GetCreator(),
},
srcStorage: srcStorage,
dstStorage: dstStorage,
SrcObjPath: srcObjPath,
DstDirPath: dstObjPath,
SrcStorageMp: srcStorage.GetStorage().MountPath,
DstStorageMp: dstStorage.GetStorage().MountPath,
2023-11-20 10:01:51 +00:00
})
2022-06-15 10:58:26 +00:00
}
2023-11-20 10:01:51 +00:00
t.Status = "src object is dir, added all copy tasks of objs"
return nil
2022-06-17 13:23:44 +00:00
}
2023-11-20 10:01:51 +00:00
return copyFileBetween2Storages(t, srcStorage, dstStorage, srcObjPath, dstDirPath)
2022-06-17 13:23:44 +00:00
}
2023-11-20 10:01:51 +00:00
func copyFileBetween2Storages(tsk *CopyTask, 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
}
2024-12-25 13:09:54 +00:00
tsk.SetTotalBytes(srcFile.GetSize())
2023-11-20 10:01:51 +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,
2023-11-20 10:01:51 +00:00
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
}
2023-11-20 10:01:51 +00:00
return op.Put(tsk.Ctx(), dstStorage, dstDirPath, ss, tsk.SetProgress, true)
2022-06-15 10:58:26 +00:00
}