2022-06-17 21:35:46 +08:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-08-03 14:26:59 +08:00
|
|
|
"sync/atomic"
|
|
|
|
|
2022-06-23 15:57:10 +08:00
|
|
|
"github.com/alist-org/alist/v3/internal/errs"
|
2022-06-17 21:35:46 +08:00
|
|
|
"github.com/alist-org/alist/v3/internal/model"
|
2022-08-31 21:01:15 +08:00
|
|
|
"github.com/alist-org/alist/v3/internal/op"
|
2022-06-17 21:35:46 +08:00
|
|
|
"github.com/alist-org/alist/v3/pkg/task"
|
2022-07-01 15:04:02 +08:00
|
|
|
"github.com/alist-org/alist/v3/pkg/utils"
|
2022-06-17 21:35:46 +08:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2022-08-03 14:26:59 +08:00
|
|
|
var UploadTaskManager = task.NewTaskManager(3, func(tid *uint64) {
|
2022-06-21 16:14:37 +08:00
|
|
|
atomic.AddUint64(tid, 1)
|
|
|
|
})
|
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
|
2022-12-17 19:49:05 +08:00
|
|
|
func putAsTask(dstDirPath string, file *model.FileStream) 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 {
|
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() {
|
|
|
|
tempFile, err := utils.CreateTempFile(file)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to create temp file")
|
|
|
|
}
|
|
|
|
file.SetReadCloser(tempFile)
|
|
|
|
}
|
2022-06-22 19:28:41 +08:00
|
|
|
UploadTaskManager.Submit(task.WithCancelCtx(&task.Task[uint64]{
|
2022-07-12 14:11:37 +08:00
|
|
|
Name: fmt.Sprintf("upload %s to [%s](%s)", file.GetName(), storage.GetStorage().MountPath, dstDirActualPath),
|
2022-06-22 19:28:41 +08:00
|
|
|
Func: func(task *task.Task[uint64]) error {
|
2022-08-31 21:01:15 +08:00
|
|
|
return op.Put(task.Ctx, storage, dstDirActualPath, file, nil)
|
2022-06-21 16:14:37 +08:00
|
|
|
},
|
|
|
|
}))
|
2022-06-17 21:35:46 +08:00
|
|
|
return nil
|
|
|
|
}
|
2022-06-24 14:21:28 +08:00
|
|
|
|
|
|
|
// putDirect put the file and return after finish
|
2022-12-17 19:49:05 +08:00
|
|
|
func putDirectly(ctx context.Context, dstDirPath string, file *model.FileStream) 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 {
|
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 {
|
|
|
|
return errors.WithStack(errs.UploadNotSupported)
|
|
|
|
}
|
2022-08-31 21:01:15 +08:00
|
|
|
return op.Put(ctx, storage, dstDirActualPath, file, nil)
|
2022-06-24 14:21:28 +08:00
|
|
|
}
|