2022-06-17 13:35:46 +00:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"github.com/alist-org/alist/v3/internal/driver"
|
|
|
|
"github.com/alist-org/alist/v3/internal/model"
|
|
|
|
"github.com/alist-org/alist/v3/internal/operations"
|
|
|
|
"github.com/alist-org/alist/v3/pkg/task"
|
|
|
|
"github.com/pkg/errors"
|
2022-06-21 08:14:37 +00:00
|
|
|
"sync/atomic"
|
2022-06-17 13:35:46 +00:00
|
|
|
)
|
|
|
|
|
2022-06-21 08:14:37 +00:00
|
|
|
var UploadTaskManager = task.NewTaskManager[uint64, struct{}](3, func(tid *uint64) {
|
|
|
|
atomic.AddUint64(tid, 1)
|
|
|
|
})
|
2022-06-17 13:35:46 +00:00
|
|
|
|
|
|
|
// Put add as a put task
|
2022-06-22 07:03:27 +00:00
|
|
|
func Put(ctx context.Context, account driver.Driver, dstDirPath string, file model.FileStreamer) error {
|
|
|
|
account, dstDirActualPath, err := operations.GetAccountAndActualPath(dstDirPath)
|
2022-06-20 09:14:08 +00:00
|
|
|
if account.Config().NoUpload {
|
2022-06-20 12:34:58 +00:00
|
|
|
return errors.WithStack(ErrUploadNotSupported)
|
2022-06-20 09:14:08 +00:00
|
|
|
}
|
2022-06-17 13:35:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithMessage(err, "failed get account")
|
|
|
|
}
|
2022-06-21 08:14:37 +00:00
|
|
|
UploadTaskManager.Submit(task.WithCancelCtx(&task.Task[uint64, struct{}]{
|
2022-06-22 07:03:27 +00:00
|
|
|
Name: fmt.Sprintf("upload %s to [%s](%s)", file.GetName(), account.GetAccount().VirtualPath, dstDirActualPath),
|
2022-06-21 08:14:37 +00:00
|
|
|
Func: func(task *task.Task[uint64, struct{}]) error {
|
2022-06-22 07:03:27 +00:00
|
|
|
return operations.Put(task.Ctx, account, dstDirActualPath, file, nil)
|
2022-06-21 08:14:37 +00:00
|
|
|
},
|
|
|
|
}))
|
2022-06-17 13:35:46 +00:00
|
|
|
return nil
|
|
|
|
}
|