feat: add task info to resp of add task api (close #5579)

This commit is contained in:
Andy Hsu
2023-12-03 14:44:20 +08:00
parent 8bdfc7ac8e
commit 026e944cbb
8 changed files with 79 additions and 50 deletions

View File

@ -39,23 +39,23 @@ var CopyTaskManager *tache.Manager[*CopyTask]
// Copy if in the same storage, call move method
// if not, add copy task
func _copy(ctx context.Context, srcObjPath, dstDirPath string, lazyCache ...bool) (bool, error) {
func _copy(ctx context.Context, srcObjPath, dstDirPath string, lazyCache ...bool) (tache.TaskWithInfo, error) {
srcStorage, srcObjActualPath, err := op.GetStorageAndActualPath(srcObjPath)
if err != nil {
return false, errors.WithMessage(err, "failed get src storage")
return nil, errors.WithMessage(err, "failed get src storage")
}
dstStorage, dstDirActualPath, err := op.GetStorageAndActualPath(dstDirPath)
if err != nil {
return false, errors.WithMessage(err, "failed get dst storage")
return nil, errors.WithMessage(err, "failed get dst storage")
}
// copy if in the same storage, just call driver.Copy
if srcStorage.GetStorage() == dstStorage.GetStorage() {
return false, op.Copy(ctx, srcStorage, srcObjActualPath, dstDirActualPath, lazyCache...)
return nil, 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)
return nil, errors.WithMessagef(err, "failed get src [%s] file", srcObjPath)
}
if !srcObj.IsDir() {
// copy file directly
@ -63,7 +63,7 @@ func _copy(ctx context.Context, srcObjPath, dstDirPath string, lazyCache ...bool
Header: http.Header{},
})
if err != nil {
return false, errors.WithMessagef(err, "failed get [%s] link", srcObjPath)
return nil, errors.WithMessagef(err, "failed get [%s] link", srcObjPath)
}
fs := stream.FileStream{
Obj: srcObj,
@ -72,19 +72,20 @@ func _copy(ctx context.Context, srcObjPath, dstDirPath string, lazyCache ...bool
// 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 nil, errors.WithMessagef(err, "failed get [%s] stream", srcObjPath)
}
return false, op.Put(ctx, dstStorage, dstDirActualPath, ss, nil, false)
return nil, op.Put(ctx, dstStorage, dstDirActualPath, ss, nil, false)
}
}
// not in the same storage
CopyTaskManager.Add(&CopyTask{
t := &CopyTask{
srcStorage: srcStorage,
dstStorage: dstStorage,
srcObjPath: srcObjActualPath,
dstDirPath: dstDirActualPath,
})
return true, nil
}
CopyTaskManager.Add(t)
return t, nil
}
func copyBetween2Storages(t *CopyTask, srcStorage, dstStorage driver.Driver, srcObjPath, dstDirPath string) error {

View File

@ -6,6 +6,7 @@ import (
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/op"
log "github.com/sirupsen/logrus"
"github.com/xhofe/tache"
)
// the param named path of functions in this package is a mount path
@ -68,7 +69,7 @@ func Move(ctx context.Context, srcPath, dstDirPath string, lazyCache ...bool) er
return err
}
func Copy(ctx context.Context, srcObjPath, dstDirPath string, lazyCache ...bool) (bool, error) {
func Copy(ctx context.Context, srcObjPath, dstDirPath string, lazyCache ...bool) (tache.TaskWithInfo, error) {
res, err := _copy(ctx, srcObjPath, dstDirPath, lazyCache...)
if err != nil {
log.Errorf("failed copy %s to %s: %+v", srcObjPath, dstDirPath, err)
@ -100,12 +101,12 @@ func PutDirectly(ctx context.Context, dstDirPath string, file model.FileStreamer
return err
}
func PutAsTask(dstDirPath string, file model.FileStreamer) error {
err := putAsTask(dstDirPath, file)
func PutAsTask(dstDirPath string, file model.FileStreamer) (tache.TaskWithInfo, error) {
t, err := putAsTask(dstDirPath, file)
if err != nil {
log.Errorf("failed put %s: %+v", dstDirPath, err)
}
return err
return t, err
}
type GetStoragesArgs struct {

View File

@ -33,28 +33,29 @@ func (t *UploadTask) Run() error {
var UploadTaskManager *tache.Manager[*UploadTask]
// putAsTask add as a put task and return immediately
func putAsTask(dstDirPath string, file model.FileStreamer) error {
func putAsTask(dstDirPath string, file model.FileStreamer) (tache.TaskWithInfo, error) {
storage, dstDirActualPath, err := op.GetStorageAndActualPath(dstDirPath)
if err != nil {
return errors.WithMessage(err, "failed get storage")
return nil, errors.WithMessage(err, "failed get storage")
}
if storage.Config().NoUpload {
return errors.WithStack(errs.UploadNotSupported)
return nil, errors.WithStack(errs.UploadNotSupported)
}
if file.NeedStore() {
_, err := file.CacheFullInTempFile()
if err != nil {
return errors.Wrapf(err, "failed to create temp file")
return nil, errors.Wrapf(err, "failed to create temp file")
}
//file.SetReader(tempFile)
//file.SetTmpFile(tempFile)
}
UploadTaskManager.Add(&UploadTask{
t := &UploadTask{
storage: storage,
dstDirActualPath: dstDirActualPath,
file: file,
})
return nil
}
UploadTaskManager.Add(t)
return t, nil
}
// putDirect put the file and return after finish