chore: aria2 related function

refactor/fs
Noah Hsu 2022-06-20 20:34:58 +08:00
parent 4db25605e7
commit 6c552a9d62
9 changed files with 120 additions and 2 deletions

35
internal/aria2/add.go Normal file
View File

@ -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
}

25
internal/aria2/aria2.go Normal file
View File

@ -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
}

7
internal/aria2/manage.go Normal file
View File

@ -0,0 +1,7 @@
package aria2
import "context"
func ListFinished(ctx context.Context) {
}

36
internal/aria2/notify.go Normal file
View File

@ -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")
}

View File

@ -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")
)

View File

@ -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")

View File

@ -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)
}

View File

@ -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) {

View File

@ -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 {