chore: aria2 notifier
This commit is contained in:
@ -2,13 +2,16 @@ package aria2
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/alist-org/alist/v3/conf"
|
||||
"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/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func AddURI(ctx context.Context, uri []string, dstPath string, parentPath string) error {
|
||||
func AddURI(ctx context.Context, uri string, dstPath string, parentPath string) error {
|
||||
// check account
|
||||
account, actualParentPath, err := operations.GetAccountAndActualPath(parentPath)
|
||||
if err != nil {
|
||||
@ -30,6 +33,14 @@ func AddURI(ctx context.Context, uri []string, dstPath string, parentPath string
|
||||
return errors.WithStack(fs.ErrNotFolder)
|
||||
}
|
||||
}
|
||||
// add aria2 task
|
||||
// call aria2 rpc
|
||||
options := map[string]interface{}{
|
||||
"dir": filepath.Join(conf.Conf.TempDir, "aria2", uuid.NewString()),
|
||||
}
|
||||
gid, err := client.AddURI([]string{uri}, options)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to add uri %s", uri)
|
||||
}
|
||||
// TODO add to task manager
|
||||
return nil
|
||||
}
|
||||
|
@ -9,10 +9,11 @@ import (
|
||||
)
|
||||
|
||||
var Aria2TaskManager = task.NewTaskManager()
|
||||
var notify = NewNotify()
|
||||
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{})
|
||||
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")
|
||||
}
|
||||
|
@ -1,36 +1,70 @@
|
||||
package aria2
|
||||
|
||||
import "github.com/alist-org/alist/v3/pkg/aria2/rpc"
|
||||
import (
|
||||
"github.com/alist-org/alist/v3/pkg/aria2/rpc"
|
||||
"github.com/alist-org/alist/v3/pkg/generic_sync"
|
||||
)
|
||||
|
||||
const (
|
||||
Downloading = iota
|
||||
Paused
|
||||
Stopped
|
||||
Completed
|
||||
Errored
|
||||
)
|
||||
|
||||
type Notify struct {
|
||||
Signals generic_sync.MapOf[string, chan int]
|
||||
}
|
||||
|
||||
func (n Notify) OnDownloadStart(events []rpc.Event) {
|
||||
//TODO update task status
|
||||
panic("implement me")
|
||||
func NewNotify() *Notify {
|
||||
return &Notify{Signals: generic_sync.MapOf[string, chan int]{}}
|
||||
}
|
||||
|
||||
func (n Notify) OnDownloadPause(events []rpc.Event) {
|
||||
//TODO update task status
|
||||
panic("implement me")
|
||||
func (n *Notify) OnDownloadStart(events []rpc.Event) {
|
||||
for _, e := range events {
|
||||
if signal, ok := n.Signals.Load(e.Gid); ok {
|
||||
signal <- Downloading
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (n Notify) OnDownloadStop(events []rpc.Event) {
|
||||
//TODO update task status
|
||||
panic("implement me")
|
||||
func (n *Notify) OnDownloadPause(events []rpc.Event) {
|
||||
for _, e := range events {
|
||||
if signal, ok := n.Signals.Load(e.Gid); ok {
|
||||
signal <- Paused
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (n Notify) OnDownloadComplete(events []rpc.Event) {
|
||||
//TODO get files and upload them
|
||||
panic("implement me")
|
||||
func (n *Notify) OnDownloadStop(events []rpc.Event) {
|
||||
for _, e := range events {
|
||||
if signal, ok := n.Signals.Load(e.Gid); ok {
|
||||
signal <- Stopped
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (n Notify) OnDownloadError(events []rpc.Event) {
|
||||
//TODO update task status
|
||||
panic("implement me")
|
||||
func (n *Notify) OnDownloadComplete(events []rpc.Event) {
|
||||
for _, e := range events {
|
||||
if signal, ok := n.Signals.Load(e.Gid); ok {
|
||||
signal <- Completed
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (n Notify) OnBtDownloadComplete(events []rpc.Event) {
|
||||
//TODO get files and upload them
|
||||
panic("implement me")
|
||||
func (n *Notify) OnDownloadError(events []rpc.Event) {
|
||||
for _, e := range events {
|
||||
if signal, ok := n.Signals.Load(e.Gid); ok {
|
||||
signal <- Errored
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Notify) OnBtDownloadComplete(events []rpc.Event) {
|
||||
for _, e := range events {
|
||||
if signal, ok := n.Signals.Load(e.Gid); ok {
|
||||
signal <- Completed
|
||||
}
|
||||
}
|
||||
}
|
||||
|
12
internal/aria2/task.go
Normal file
12
internal/aria2/task.go
Normal file
@ -0,0 +1,12 @@
|
||||
package aria2
|
||||
|
||||
import (
|
||||
"github.com/alist-org/alist/v3/internal/driver"
|
||||
"github.com/alist-org/alist/v3/pkg/task"
|
||||
)
|
||||
|
||||
type Task struct {
|
||||
Account driver.Driver
|
||||
ParentDir string
|
||||
T task.Task
|
||||
}
|
Reference in New Issue
Block a user