From 6c552a9d6232b3aae36c020d4dfcaff7e2f66207 Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Mon, 20 Jun 2022 20:34:58 +0800 Subject: [PATCH] chore: aria2 related function --- internal/aria2/add.go | 35 +++++++++++++++++++++++++++++++++++ internal/aria2/aria2.go | 25 +++++++++++++++++++++++++ internal/aria2/manage.go | 7 +++++++ internal/aria2/notify.go | 36 ++++++++++++++++++++++++++++++++++++ internal/fs/errors.go | 2 ++ internal/fs/put.go | 2 +- internal/operations/fs.go | 2 +- pkg/generic_sync/map.go | 7 +++++++ pkg/task/manager.go | 6 ++++++ 9 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 internal/aria2/add.go create mode 100644 internal/aria2/aria2.go create mode 100644 internal/aria2/manage.go create mode 100644 internal/aria2/notify.go diff --git a/internal/aria2/add.go b/internal/aria2/add.go new file mode 100644 index 00000000..802120dc --- /dev/null +++ b/internal/aria2/add.go @@ -0,0 +1,35 @@ +package aria2 + +import ( + "context" + "github.com/alist-org/alist/v3/internal/driver" + "github.com/alist-org/alist/v3/internal/fs" + "github.com/alist-org/alist/v3/internal/operations" + "github.com/pkg/errors" +) + +func AddURI(ctx context.Context, uri []string, dstPath string, parentPath string) error { + // check account + account, actualParentPath, err := operations.GetAccountAndActualPath(parentPath) + if err != nil { + return errors.WithMessage(err, "failed get account") + } + // check is it could upload + if account.Config().NoUpload { + return errors.WithStack(fs.ErrUploadNotSupported) + } + // check path is valid + obj, err := operations.Get(ctx, account, actualParentPath) + if err != nil { + if !errors.Is(errors.Cause(err), driver.ErrorObjectNotFound) { + return errors.WithMessage(err, "failed get object") + } + } else { + if !obj.IsDir() { + // can't add to a file + return errors.WithStack(fs.ErrNotFolder) + } + } + // add aria2 task + return nil +} diff --git a/internal/aria2/aria2.go b/internal/aria2/aria2.go new file mode 100644 index 00000000..2710e168 --- /dev/null +++ b/internal/aria2/aria2.go @@ -0,0 +1,25 @@ +package aria2 + +import ( + "context" + "github.com/alist-org/alist/v3/pkg/aria2/rpc" + "github.com/alist-org/alist/v3/pkg/task" + "github.com/pkg/errors" + "time" +) + +var Aria2TaskManager = task.NewTaskManager() +var client rpc.Client + +func InitAria2Client(uri string, secret string, timeout int) error { + c, err := rpc.New(context.Background(), uri, secret, time.Duration(timeout)*time.Second, &Notify{}) + if err != nil { + return errors.Wrap(err, "failed to init aria2 client") + } + client = c + return nil +} + +func IsAria2Ready() bool { + return client != nil +} diff --git a/internal/aria2/manage.go b/internal/aria2/manage.go new file mode 100644 index 00000000..acc39bc3 --- /dev/null +++ b/internal/aria2/manage.go @@ -0,0 +1,7 @@ +package aria2 + +import "context" + +func ListFinished(ctx context.Context) { + +} diff --git a/internal/aria2/notify.go b/internal/aria2/notify.go new file mode 100644 index 00000000..e03e9181 --- /dev/null +++ b/internal/aria2/notify.go @@ -0,0 +1,36 @@ +package aria2 + +import "github.com/alist-org/alist/v3/pkg/aria2/rpc" + +type Notify struct { +} + +func (n Notify) OnDownloadStart(events []rpc.Event) { + //TODO update task status + panic("implement me") +} + +func (n Notify) OnDownloadPause(events []rpc.Event) { + //TODO update task status + panic("implement me") +} + +func (n Notify) OnDownloadStop(events []rpc.Event) { + //TODO update task status + panic("implement me") +} + +func (n Notify) OnDownloadComplete(events []rpc.Event) { + //TODO get files and upload them + panic("implement me") +} + +func (n Notify) OnDownloadError(events []rpc.Event) { + //TODO update task status + panic("implement me") +} + +func (n Notify) OnBtDownloadComplete(events []rpc.Event) { + //TODO get files and upload them + panic("implement me") +} diff --git a/internal/fs/errors.go b/internal/fs/errors.go index 52015118..4b4a5f35 100644 --- a/internal/fs/errors.go +++ b/internal/fs/errors.go @@ -4,4 +4,6 @@ import "errors" var ( ErrMoveBetweenTwoAccounts = errors.New("can't move files between two account, try to copy") + ErrUploadNotSupported = errors.New("upload not supported") + ErrNotFolder = errors.New("not a folder") ) diff --git a/internal/fs/put.go b/internal/fs/put.go index f8f043fb..a6939c43 100644 --- a/internal/fs/put.go +++ b/internal/fs/put.go @@ -16,7 +16,7 @@ var UploadTaskManager = task.NewTaskManager() func Put(ctx context.Context, account driver.Driver, parentPath string, file model.FileStreamer) error { account, actualParentPath, err := operations.GetAccountAndActualPath(parentPath) if account.Config().NoUpload { - return errors.New("upload is not supported") + return errors.WithStack(ErrUploadNotSupported) } if err != nil { return errors.WithMessage(err, "failed get account") diff --git a/internal/operations/fs.go b/internal/operations/fs.go index ac6803aa..7b9f48eb 100644 --- a/internal/operations/fs.go +++ b/internal/operations/fs.go @@ -194,7 +194,7 @@ func Put(ctx context.Context, account driver.Driver, parentPath string, file mod } // if up is nil, set a default to prevent panic if up == nil { - up = func(p float64) {} + up = func(p int) {} } return account.Put(ctx, parentDir, file, up) } diff --git a/pkg/generic_sync/map.go b/pkg/generic_sync/map.go index 74f89d7e..c7be9719 100644 --- a/pkg/generic_sync/map.go +++ b/pkg/generic_sync/map.go @@ -347,6 +347,13 @@ func (m *MapOf[K, V]) Values() []V { return values } +func (m *MapOf[K, V]) Clear() { + m.Range(func(key K, value V) bool { + m.Delete(key) + return true + }) +} + func (m *MapOf[K, V]) missLocked() { m.misses++ if m.misses < len(m.dirty) { diff --git a/pkg/task/manager.go b/pkg/task/manager.go index abad85e5..667fa932 100644 --- a/pkg/task/manager.go +++ b/pkg/task/manager.go @@ -75,6 +75,12 @@ func (tm *Manager) Remove(tid uint64) { tm.tasks.Delete(tid) } +// RemoveAll removes all tasks from the manager, this maybe shouldn't be used +// because the task maybe still running. +func (tm *Manager) RemoveAll() { + tm.tasks.Clear() +} + func (tm *Manager) RemoveFinished() { tasks := tm.GetAll() for _, task := range tasks {