* feat: support general users view and cancel own tasks Add a creator attribute to the upload, copy and offline download tasks, so that a GENERAL task creator can view and cancel them. BREAKING CHANGE: 1. A new internal package `task` including the struct `TaskWithCreator` which embeds `tache.Base` is created, and the past dependence on `tache.Task` will all be transferred to dependence on this package. 2. The API `/admin/task` can now also be accessed via `/task`, and the old endpoint is retained to ensure compatibility with legacy automation scripts. Closes #7398 * fix(deps): update github.com/xhofe/tache to v0.1.3
77 lines
2.2 KiB
Go
77 lines
2.2 KiB
Go
package fs
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"github.com/alist-org/alist/v3/internal/driver"
|
|
"github.com/alist-org/alist/v3/internal/errs"
|
|
"github.com/alist-org/alist/v3/internal/model"
|
|
"github.com/alist-org/alist/v3/internal/op"
|
|
"github.com/alist-org/alist/v3/internal/task"
|
|
"github.com/pkg/errors"
|
|
"github.com/xhofe/tache"
|
|
)
|
|
|
|
type UploadTask struct {
|
|
task.TaskWithCreator
|
|
storage driver.Driver
|
|
dstDirActualPath string
|
|
file model.FileStreamer
|
|
}
|
|
|
|
func (t *UploadTask) GetName() string {
|
|
return fmt.Sprintf("upload %s to [%s](%s)", t.file.GetName(), t.storage.GetStorage().MountPath, t.dstDirActualPath)
|
|
}
|
|
|
|
func (t *UploadTask) GetStatus() string {
|
|
return "uploading"
|
|
}
|
|
|
|
func (t *UploadTask) Run() error {
|
|
return op.Put(t.Ctx(), t.storage, t.dstDirActualPath, t.file, t.SetProgress, true)
|
|
}
|
|
|
|
var UploadTaskManager *tache.Manager[*UploadTask]
|
|
|
|
// putAsTask add as a put task and return immediately
|
|
func putAsTask(ctx context.Context, dstDirPath string, file model.FileStreamer) (task.TaskInfoWithCreator, error) {
|
|
storage, dstDirActualPath, err := op.GetStorageAndActualPath(dstDirPath)
|
|
if err != nil {
|
|
return nil, errors.WithMessage(err, "failed get storage")
|
|
}
|
|
if storage.Config().NoUpload {
|
|
return nil, errors.WithStack(errs.UploadNotSupported)
|
|
}
|
|
if file.NeedStore() {
|
|
_, err := file.CacheFullInTempFile()
|
|
if err != nil {
|
|
return nil, errors.Wrapf(err, "failed to create temp file")
|
|
}
|
|
//file.SetReader(tempFile)
|
|
//file.SetTmpFile(tempFile)
|
|
}
|
|
taskCreator, _ := ctx.Value("user").(*model.User) // taskCreator is nil when convert failed
|
|
t := &UploadTask{
|
|
TaskWithCreator: task.TaskWithCreator{
|
|
Creator: taskCreator,
|
|
},
|
|
storage: storage,
|
|
dstDirActualPath: dstDirActualPath,
|
|
file: file,
|
|
}
|
|
UploadTaskManager.Add(t)
|
|
return t, nil
|
|
}
|
|
|
|
// putDirect put the file and return after finish
|
|
func putDirectly(ctx context.Context, dstDirPath string, file model.FileStreamer, lazyCache ...bool) error {
|
|
storage, dstDirActualPath, err := op.GetStorageAndActualPath(dstDirPath)
|
|
if err != nil {
|
|
return errors.WithMessage(err, "failed get storage")
|
|
}
|
|
if storage.Config().NoUpload {
|
|
return errors.WithStack(errs.UploadNotSupported)
|
|
}
|
|
return op.Put(ctx, storage, dstDirActualPath, file, nil, lazyCache...)
|
|
}
|