mirror of https://github.com/Xhofe/alist
chore: aria2 related function
parent
4db25605e7
commit
6c552a9d62
|
@ -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
|
||||||
|
}
|
|
@ -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
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
package aria2
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
|
||||||
|
func ListFinished(ctx context.Context) {
|
||||||
|
|
||||||
|
}
|
|
@ -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")
|
||||||
|
}
|
|
@ -4,4 +4,6 @@ import "errors"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrMoveBetweenTwoAccounts = errors.New("can't move files between two account, try to copy")
|
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")
|
||||||
)
|
)
|
||||||
|
|
|
@ -16,7 +16,7 @@ var UploadTaskManager = task.NewTaskManager()
|
||||||
func Put(ctx context.Context, account driver.Driver, parentPath string, file model.FileStreamer) error {
|
func Put(ctx context.Context, account driver.Driver, parentPath string, file model.FileStreamer) error {
|
||||||
account, actualParentPath, err := operations.GetAccountAndActualPath(parentPath)
|
account, actualParentPath, err := operations.GetAccountAndActualPath(parentPath)
|
||||||
if account.Config().NoUpload {
|
if account.Config().NoUpload {
|
||||||
return errors.New("upload is not supported")
|
return errors.WithStack(ErrUploadNotSupported)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.WithMessage(err, "failed get account")
|
return errors.WithMessage(err, "failed get account")
|
||||||
|
|
|
@ -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 is nil, set a default to prevent panic
|
||||||
if up == nil {
|
if up == nil {
|
||||||
up = func(p float64) {}
|
up = func(p int) {}
|
||||||
}
|
}
|
||||||
return account.Put(ctx, parentDir, file, up)
|
return account.Put(ctx, parentDir, file, up)
|
||||||
}
|
}
|
||||||
|
|
|
@ -347,6 +347,13 @@ func (m *MapOf[K, V]) Values() []V {
|
||||||
return values
|
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() {
|
func (m *MapOf[K, V]) missLocked() {
|
||||||
m.misses++
|
m.misses++
|
||||||
if m.misses < len(m.dirty) {
|
if m.misses < len(m.dirty) {
|
||||||
|
|
|
@ -75,6 +75,12 @@ func (tm *Manager) Remove(tid uint64) {
|
||||||
tm.tasks.Delete(tid)
|
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() {
|
func (tm *Manager) RemoveFinished() {
|
||||||
tasks := tm.GetAll()
|
tasks := tm.GetAll()
|
||||||
for _, task := range tasks {
|
for _, task := range tasks {
|
||||||
|
|
Loading…
Reference in New Issue