alist/internal/fs/put.go

82 lines
2.4 KiB
Go
Raw Normal View History

2022-06-17 13:35:46 +00:00
package fs
import (
"context"
"fmt"
2023-11-20 10:01:51 +00:00
"github.com/alist-org/alist/v3/internal/driver"
2022-06-23 07:57:10 +00:00
"github.com/alist-org/alist/v3/internal/errs"
2023-11-20 10:01:51 +00:00
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/op"
"github.com/alist-org/alist/v3/internal/task"
2022-06-17 13:35:46 +00:00
"github.com/pkg/errors"
2023-11-20 10:01:51 +00:00
"github.com/xhofe/tache"
2024-12-25 13:09:54 +00:00
"time"
2022-06-17 13:35:46 +00:00
)
2023-11-20 10:01:51 +00:00
type UploadTask struct {
2024-12-25 13:09:54 +00:00
task.TaskExtension
2023-11-20 10:01:51 +00:00
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 {
2024-12-25 13:09:54 +00:00
t.ClearEndTime()
t.SetStartTime(time.Now())
defer func() { t.SetEndTime(time.Now()) }()
2023-11-20 10:01:51 +00:00
return op.Put(t.Ctx(), t.storage, t.dstDirActualPath, t.file, t.SetProgress, true)
}
var UploadTaskManager *tache.Manager[*UploadTask]
2022-06-17 13:35:46 +00:00
// putAsTask add as a put task and return immediately
2024-12-25 13:09:54 +00:00
func putAsTask(ctx context.Context, dstDirPath string, file model.FileStreamer) (task.TaskExtensionInfo, error) {
storage, dstDirActualPath, err := op.GetStorageAndActualPath(dstDirPath)
2022-06-17 13:35:46 +00:00
if err != nil {
return nil, errors.WithMessage(err, "failed get storage")
2022-06-17 13:35:46 +00:00
}
2022-08-29 06:18:43 +00:00
if storage.Config().NoUpload {
return nil, errors.WithStack(errs.UploadNotSupported)
2022-08-29 06:18:43 +00:00
}
2022-07-01 07:04:02 +00:00
if file.NeedStore() {
_, err := file.CacheFullInTempFile()
2022-07-01 07:04:02 +00:00
if err != nil {
return nil, errors.Wrapf(err, "failed to create temp file")
2022-07-01 07:04:02 +00:00
}
//file.SetReader(tempFile)
//file.SetTmpFile(tempFile)
2022-07-01 07:04:02 +00:00
}
taskCreator, _ := ctx.Value("user").(*model.User) // taskCreator is nil when convert failed
t := &UploadTask{
2024-12-25 13:09:54 +00:00
TaskExtension: task.TaskExtension{
Creator: taskCreator,
},
2023-11-20 10:01:51 +00:00
storage: storage,
dstDirActualPath: dstDirActualPath,
file: file,
}
2024-12-25 13:09:54 +00:00
t.SetTotalBytes(file.GetSize())
UploadTaskManager.Add(t)
return t, nil
2022-06-17 13:35:46 +00:00
}
// 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 06:45:39 +00:00
return errors.WithMessage(err, "failed get storage")
}
2022-08-29 06:18:43 +00:00
if storage.Config().NoUpload {
return errors.WithStack(errs.UploadNotSupported)
}
return op.Put(ctx, storage, dstDirActualPath, file, nil, lazyCache...)
}