2022-06-17 21:35:46 +08:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2025-07-24 16:15:24 +08:00
|
|
|
stdpath "path"
|
2025-06-12 22:02:46 +08:00
|
|
|
"time"
|
|
|
|
|
2025-07-24 16:15:24 +08:00
|
|
|
"github.com/OpenListTeam/OpenList/v4/server/common"
|
|
|
|
|
2025-07-14 23:55:17 +08:00
|
|
|
"github.com/OpenListTeam/OpenList/v4/internal/conf"
|
2025-07-01 09:54:50 +08:00
|
|
|
"github.com/OpenListTeam/OpenList/v4/internal/driver"
|
|
|
|
"github.com/OpenListTeam/OpenList/v4/internal/errs"
|
|
|
|
"github.com/OpenListTeam/OpenList/v4/internal/model"
|
|
|
|
"github.com/OpenListTeam/OpenList/v4/internal/op"
|
|
|
|
"github.com/OpenListTeam/OpenList/v4/internal/task"
|
2025-07-24 16:15:24 +08:00
|
|
|
"github.com/OpenListTeam/OpenList/v4/internal/task_group"
|
2025-06-30 21:26:42 +08:00
|
|
|
"github.com/OpenListTeam/tache"
|
2025-07-12 17:57:54 +08:00
|
|
|
"github.com/pkg/errors"
|
2022-06-17 21:35:46 +08:00
|
|
|
)
|
|
|
|
|
2023-11-20 18:01:51 +08:00
|
|
|
type UploadTask struct {
|
2024-12-25 21:09:54 +08:00
|
|
|
task.TaskExtension
|
2023-11-20 18:01:51 +08: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 21:09:54 +08:00
|
|
|
t.ClearEndTime()
|
|
|
|
t.SetStartTime(time.Now())
|
|
|
|
defer func() { t.SetEndTime(time.Now()) }()
|
2023-11-20 18:01:51 +08:00
|
|
|
return op.Put(t.Ctx(), t.storage, t.dstDirActualPath, t.file, t.SetProgress, true)
|
|
|
|
}
|
|
|
|
|
2025-07-24 16:15:24 +08:00
|
|
|
func (t *UploadTask) OnSucceeded() {
|
|
|
|
task_group.TransferCoordinator.Done(stdpath.Join(t.storage.GetStorage().MountPath, t.dstDirActualPath), true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *UploadTask) OnFailed() {
|
|
|
|
task_group.TransferCoordinator.Done(stdpath.Join(t.storage.GetStorage().MountPath, t.dstDirActualPath), false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *UploadTask) SetRetry(retry int, maxRetry int) {
|
|
|
|
t.TaskExtension.SetRetry(retry, maxRetry)
|
|
|
|
if retry == 0 &&
|
|
|
|
(t.GetErr() == nil && t.GetState() != tache.StatePending) { // 手动重试
|
|
|
|
task_group.TransferCoordinator.AddTask(stdpath.Join(t.storage.GetStorage().MountPath, t.dstDirActualPath), nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-21 15:51:08 +08:00
|
|
|
var UploadTaskManager *tache.Manager[*UploadTask]
|
2022-06-17 21:35:46 +08:00
|
|
|
|
2022-06-24 14:21:28 +08:00
|
|
|
// putAsTask add as a put task and return immediately
|
2024-12-25 21:09:54 +08:00
|
|
|
func putAsTask(ctx context.Context, dstDirPath string, file model.FileStreamer) (task.TaskExtensionInfo, error) {
|
2022-08-31 21:01:15 +08:00
|
|
|
storage, dstDirActualPath, err := op.GetStorageAndActualPath(dstDirPath)
|
2022-06-17 21:35:46 +08:00
|
|
|
if err != nil {
|
2023-12-03 14:44:20 +08:00
|
|
|
return nil, 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 {
|
2023-12-03 14:44:20 +08:00
|
|
|
return nil, errors.WithStack(errs.UploadNotSupported)
|
2022-08-29 14:18:43 +08:00
|
|
|
}
|
2022-07-01 15:04:02 +08:00
|
|
|
if file.NeedStore() {
|
2025-08-11 23:41:22 +08:00
|
|
|
_, err := file.CacheFullAndWriter(nil, nil)
|
2022-07-01 15:04:02 +08:00
|
|
|
if err != nil {
|
2023-12-03 14:44:20 +08:00
|
|
|
return nil, errors.Wrapf(err, "failed to create temp file")
|
2022-07-01 15:04:02 +08:00
|
|
|
}
|
2023-08-27 21:14:23 +08:00
|
|
|
//file.SetReader(tempFile)
|
|
|
|
//file.SetTmpFile(tempFile)
|
2022-07-01 15:04:02 +08:00
|
|
|
}
|
2025-07-14 23:55:17 +08:00
|
|
|
taskCreator, _ := ctx.Value(conf.UserKey).(*model.User) // taskCreator is nil when convert failed
|
2023-12-03 14:44:20 +08:00
|
|
|
t := &UploadTask{
|
2024-12-25 21:09:54 +08:00
|
|
|
TaskExtension: task.TaskExtension{
|
2024-11-01 23:32:26 +08:00
|
|
|
Creator: taskCreator,
|
2025-07-24 16:15:24 +08:00
|
|
|
ApiUrl: common.GetApiUrl(ctx),
|
2024-11-01 23:32:26 +08:00
|
|
|
},
|
2023-11-20 18:01:51 +08:00
|
|
|
storage: storage,
|
|
|
|
dstDirActualPath: dstDirActualPath,
|
|
|
|
file: file,
|
2023-12-03 14:44:20 +08:00
|
|
|
}
|
2024-12-25 21:09:54 +08:00
|
|
|
t.SetTotalBytes(file.GetSize())
|
2025-07-24 16:15:24 +08:00
|
|
|
task_group.TransferCoordinator.AddTask(dstDirPath, nil)
|
2023-12-03 14:44:20 +08:00
|
|
|
UploadTaskManager.Add(t)
|
|
|
|
return t, nil
|
2022-06-17 21:35:46 +08:00
|
|
|
}
|
2022-06-24 14:21:28 +08:00
|
|
|
|
|
|
|
// putDirect put the file and return after finish
|
2023-08-27 21:14:23 +08:00
|
|
|
func putDirectly(ctx context.Context, dstDirPath string, file model.FileStreamer, lazyCache ...bool) error {
|
2022-08-31 21:01:15 +08:00
|
|
|
storage, dstDirActualPath, err := op.GetStorageAndActualPath(dstDirPath)
|
2022-06-24 14:21:28 +08:00
|
|
|
if err != nil {
|
2025-07-12 17:57:54 +08:00
|
|
|
_ = file.Close()
|
2022-07-10 14:45:39 +08:00
|
|
|
return errors.WithMessage(err, "failed get storage")
|
2022-06-24 14:21:28 +08:00
|
|
|
}
|
2022-08-29 14:18:43 +08:00
|
|
|
if storage.Config().NoUpload {
|
2025-07-12 17:57:54 +08:00
|
|
|
_ = file.Close()
|
2022-08-29 14:18:43 +08:00
|
|
|
return errors.WithStack(errs.UploadNotSupported)
|
|
|
|
}
|
2022-12-20 15:02:40 +08:00
|
|
|
return op.Put(ctx, storage, dstDirActualPath, file, nil, lazyCache...)
|
2022-06-24 14:21:28 +08:00
|
|
|
}
|