alist/internal/fs/put.go

71 lines
1.9 KiB
Go
Raw Normal View History

2022-06-17 21:35:46 +08:00
package fs
import (
"context"
"fmt"
2023-11-20 18:01:51 +08:00
"github.com/alist-org/alist/v3/internal/driver"
2022-06-23 15:57:10 +08:00
"github.com/alist-org/alist/v3/internal/errs"
2023-11-20 18:01:51 +08:00
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/op"
2022-06-17 21:35:46 +08:00
"github.com/pkg/errors"
2023-11-20 18:01:51 +08:00
"github.com/xhofe/tache"
2022-06-17 21:35:46 +08:00
)
2023-11-20 18:01:51 +08:00
type UploadTask struct {
tache.Base
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.NewManager[*UploadTask]()
2022-06-17 21:35:46 +08:00
// putAsTask add as a put task and return immediately
func putAsTask(dstDirPath string, file model.FileStreamer) error {
storage, dstDirActualPath, err := op.GetStorageAndActualPath(dstDirPath)
2022-06-17 21:35:46 +08:00
if err != nil {
2022-07-10 14:45:39 +08:00
return errors.WithMessage(err, "failed get storage")
2022-06-17 21:35:46 +08:00
}
2022-08-29 14:18:43 +08:00
if storage.Config().NoUpload {
return errors.WithStack(errs.UploadNotSupported)
}
2022-07-01 15:04:02 +08:00
if file.NeedStore() {
_, err := file.CacheFullInTempFile()
2022-07-01 15:04:02 +08:00
if err != nil {
return errors.Wrapf(err, "failed to create temp file")
}
//file.SetReader(tempFile)
//file.SetTmpFile(tempFile)
2022-07-01 15:04:02 +08:00
}
2023-11-20 18:01:51 +08:00
UploadTaskManager.Add(&UploadTask{
storage: storage,
dstDirActualPath: dstDirActualPath,
file: file,
})
2022-06-17 21:35:46 +08:00
return 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 {
2022-07-10 14:45:39 +08:00
return errors.WithMessage(err, "failed get storage")
}
2022-08-29 14:18:43 +08:00
if storage.Config().NoUpload {
return errors.WithStack(errs.UploadNotSupported)
}
return op.Put(ctx, storage, dstDirActualPath, file, nil, lazyCache...)
}