wip: use tool manager

This commit is contained in:
Andy Hsu
2023-10-05 22:13:02 +08:00
parent 0380d7fff9
commit 12dfb60a66
14 changed files with 117 additions and 168 deletions

View File

@ -13,13 +13,13 @@ import (
"github.com/pkg/errors"
)
type AddURIArgs struct {
URI string
type AddURLArgs struct {
URL string
DstDirPath string
Tool string
}
func AddURI(ctx context.Context, args *AddURIArgs) error {
func AddURL(ctx context.Context, args *AddURLArgs) error {
// get tool
tool, err := Tools.Get(args.Tool)
if err != nil {
@ -27,7 +27,10 @@ func AddURI(ctx context.Context, args *AddURIArgs) error {
}
// check tool is ready
if !tool.IsReady() {
return errors.Wrapf(err, "tool %s is not ready", args.Tool)
// try to init tool
if _, err := tool.Init(); err != nil {
return errors.Wrapf(err, "failed init tool %s", args.Tool)
}
}
// check storage
storage, dstDirActualPath, err := op.GetStorageAndActualPath(args.DstDirPath)
@ -54,20 +57,21 @@ func AddURI(ctx context.Context, args *AddURIArgs) error {
uid := uuid.NewString()
tempDir := filepath.Join(conf.Conf.TempDir, args.Tool, uid)
signal := make(chan int)
gid, err := tool.AddURI(&AddUriArgs{
Uri: args.URI,
gid, err := tool.AddURL(&AddUrlArgs{
Url: args.URL,
UID: uid,
TempDir: tempDir,
Signal: signal,
})
if err != nil {
return errors.Wrapf(err, "[%s] failed to add uri %s", args.Tool, args.URI)
return errors.Wrapf(err, "[%s] failed to add uri %s", args.Tool, args.URL)
}
DownTaskManager.Submit(task.WithCancelCtx(&task.Task[string]{
ID: gid,
Name: fmt.Sprintf("download %s to [%s](%s)", args.URI, storage.GetStorage().MountPath, dstDirActualPath),
Name: fmt.Sprintf("download %s to [%s](%s)", args.URL, storage.GetStorage().MountPath, dstDirActualPath),
Func: func(tsk *task.Task[string]) error {
m := &Monitor{
tool: tool,
tsk: tsk,
tempDir: tempDir,
dstDirPath: args.DstDirPath,

View File

@ -8,8 +8,8 @@ import (
"github.com/alist-org/alist/v3/internal/model"
)
type AddUriArgs struct {
Uri string
type AddUrlArgs struct {
Url string
UID string
TempDir string
Signal chan int
@ -28,17 +28,18 @@ type Tool interface {
Items() []model.SettingItem
Init() (string, error)
IsReady() bool
// AddURI add an uri to download, return the task id
AddURI(args *AddUriArgs) (string, error)
// AddURL add an uri to download, return the task id
AddURL(args *AddUrlArgs) (string, error)
// Remove the download if task been canceled
Remove(tid string) error
// Status return the status of the download task, if an error occurred, return the error in Status.Err
Status(tid string) (*Status, error)
// GetFile return an io.ReadCloser as the download file, if nil, means walk the temp dir to get the files
GetFile(tid string) *File
// GetFiles return the files of the download task, if nil, means walk the temp dir to get the files
GetFiles(tid string) []File
}
type File struct {
// ReadCloser for http client
io.ReadCloser
Name string
Size int64

View File

@ -3,7 +3,6 @@ package tool
import (
"fmt"
"os"
"path"
"path/filepath"
"sync"
"sync/atomic"
@ -104,9 +103,9 @@ func (m *Monitor) Complete() error {
if err != nil {
return errors.WithMessage(err, "failed get storage")
}
var files []*File
if f := m.tool.GetFile(m.tsk.ID); f != nil {
files = append(files, f)
var files []File
if f := m.tool.GetFiles(m.tsk.ID); f != nil {
files = f
} else {
files, err = GetFiles(m.tempDir)
if err != nil {
@ -138,7 +137,7 @@ func (m *Monitor) Complete() error {
s := &stream.FileStream{
Ctx: nil,
Obj: &model.Object{
Name: path.Base(file.Path),
Name: filepath.Base(file.Path),
Size: file.Size,
Modified: file.Modified,
IsFolder: false,

View File

@ -5,14 +5,14 @@ import (
"path/filepath"
)
func GetFiles(dir string) ([]*File, error) {
var files []*File
func GetFiles(dir string) ([]File, error) {
var files []File
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
files = append(files, &File{
files = append(files, File{
Name: info.Name(),
Size: info.Size(),
Path: path,