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

@ -1,16 +0,0 @@
package bootstrap
import (
"github.com/alist-org/alist/v3/internal/aria2"
"github.com/alist-org/alist/v3/pkg/utils"
)
func InitAria2() {
go func() {
_, err := aria2.InitClient(2)
if err != nil {
//utils.Log.Errorf("failed to init aria2 client: %+v", err)
utils.Log.Infof("Aria2 not ready.")
}
}()
}

View File

@ -0,0 +1,17 @@
package bootstrap
import (
"github.com/alist-org/alist/v3/internal/offline_download/tool"
"github.com/alist-org/alist/v3/pkg/utils"
)
func InitOfflineDownloadTools() {
for k, v := range tool.Tools {
res, err := v.Init()
if err != nil {
utils.Log.Warnf("init tool %s failed: %s", k, err)
} else {
utils.Log.Infof("init tool %s success: %s", k, res)
}
}
}

View File

@ -1,15 +0,0 @@
package bootstrap
import (
"github.com/alist-org/alist/v3/internal/qbittorrent"
"github.com/alist-org/alist/v3/pkg/utils"
)
func InitQbittorrent() {
go func() {
err := qbittorrent.InitClient()
if err != nil {
utils.Log.Infof("qbittorrent not ready.")
}
}()
}

View File

@ -32,7 +32,7 @@ type User struct {
// Determine permissions by bit
// 0: can see hidden files
// 1: can access without password
// 2: can add aria2 tasks
// 2: can add offline download tasks
// 3: can mkdir and upload
// 4: can rename
// 5: can move
@ -40,7 +40,6 @@ type User struct {
// 7: can remove
// 8: webdav read
// 9: webdav write
// 10: can add qbittorrent tasks
Permission int32 `json:"permission"`
OtpSecret string `json:"-"`
SsoID string `json:"sso_id"` // unique by sso platform
@ -83,7 +82,7 @@ func (u *User) CanAccessWithoutPassword() bool {
return u.IsAdmin() || (u.Permission>>1)&1 == 1
}
func (u *User) CanAddAria2Tasks() bool {
func (u *User) CanAddOfflineDownloadTasks() bool {
return u.IsAdmin() || (u.Permission>>2)&1 == 1
}
@ -115,10 +114,6 @@ func (u *User) CanWebdavManage() bool {
return u.IsAdmin() || (u.Permission>>9)&1 == 1
}
func (u *User) CanAddQbittorrentTasks() bool {
return u.IsAdmin() || (u.Permission>>10)&1 == 1
}
func (u *User) JoinPath(reqPath string) (string, error) {
return utils.JoinBasePath(u.BasePath, reqPath)
}

View File

@ -50,11 +50,11 @@ func (a *Aria2) IsReady() bool {
return a.client != nil
}
func (a *Aria2) AddURI(args *tool.AddUriArgs) (string, error) {
func (a *Aria2) AddURL(args *tool.AddUrlArgs) (string, error) {
options := map[string]interface{}{
"dir": args.TempDir,
}
gid, err := a.client.AddURI([]string{args.Uri}, options)
gid, err := a.client.AddURI([]string{args.Url}, options)
if err != nil {
return "", err
}
@ -109,7 +109,20 @@ func (a *Aria2) Status(tid string) (*tool.Status, error) {
return s, nil
}
func (a *Aria2) GetFile(tid string) *tool.File {
func (a *Aria2) GetFiles(tid string) []tool.File {
//files, err := a.client.GetFiles(tid)
//if err != nil {
// return nil
//}
//return utils.MustSliceConvert(files, func(f rpc.FileInfo) tool.File {
// return tool.File{
// //ReadCloser: nil,
// Name: path.Base(f.Path),
// Size: f.Length,
// Path: "",
// Modified: time.Time{},
// }
//})
return nil
}

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,