alist/internal/fs/put.go

55 lines
1.7 KiB
Go
Raw Normal View History

2022-06-17 13:35:46 +00:00
package fs
import (
"context"
"fmt"
2022-06-23 07:57:10 +00:00
"github.com/alist-org/alist/v3/internal/errs"
2022-06-17 13:35:46 +00:00
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/operations"
"github.com/alist-org/alist/v3/pkg/task"
2022-07-01 07:04:02 +00:00
"github.com/alist-org/alist/v3/pkg/utils"
2022-06-17 13:35:46 +00:00
"github.com/pkg/errors"
2022-06-21 08:14:37 +00:00
"sync/atomic"
2022-06-17 13:35:46 +00:00
)
2022-06-22 11:28:41 +00:00
var UploadTaskManager = task.NewTaskManager[uint64](3, func(tid *uint64) {
2022-06-21 08:14:37 +00:00
atomic.AddUint64(tid, 1)
})
2022-06-17 13:35:46 +00:00
// putAsTask add as a put task and return immediately
func putAsTask(dstDirPath string, file model.FileStreamer) error {
2022-07-10 06:45:39 +00:00
storage, dstDirActualPath, err := operations.GetStorageAndActualPath(dstDirPath)
if storage.Config().NoUpload {
2022-06-23 08:03:27 +00:00
return errors.WithStack(errs.UploadNotSupported)
}
2022-06-17 13:35:46 +00:00
if err != nil {
2022-07-10 06:45:39 +00:00
return errors.WithMessage(err, "failed get storage")
2022-06-17 13:35:46 +00:00
}
2022-07-01 07:04:02 +00: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 11:28:41 +00:00
UploadTaskManager.Submit(task.WithCancelCtx(&task.Task[uint64]{
Name: fmt.Sprintf("upload %s to [%s](%s)", file.GetName(), storage.GetStorage().MountPath, dstDirActualPath),
2022-06-22 11:28:41 +00:00
Func: func(task *task.Task[uint64]) error {
2022-07-10 06:45:39 +00:00
return operations.Put(task.Ctx, storage, dstDirActualPath, file, nil)
2022-06-21 08:14:37 +00:00
},
}))
2022-06-17 13:35:46 +00:00
return nil
}
// putDirect put the file and return after finish
func putDirectly(ctx context.Context, dstDirPath string, file model.FileStreamer) error {
2022-07-10 06:45:39 +00:00
storage, dstDirActualPath, err := operations.GetStorageAndActualPath(dstDirPath)
if storage.Config().NoUpload {
return errors.WithStack(errs.UploadNotSupported)
}
if err != nil {
2022-07-10 06:45:39 +00:00
return errors.WithMessage(err, "failed get storage")
}
2022-07-10 06:45:39 +00:00
return operations.Put(ctx, storage, dstDirActualPath, file, nil)
}