From 0acb2d60730e0e55018ebad7dd6ba65412d66ff3 Mon Sep 17 00:00:00 2001 From: Andy Hsu Date: Wed, 4 Oct 2023 22:23:45 +0800 Subject: [PATCH] wip: adapt aria2 --- internal/offline_download/add.go | 6 ++- internal/offline_download/all_test.go | 12 +++-- internal/offline_download/aria2/aria2.go | 63 ++++++++++++++++++++---- internal/offline_download/base.go | 2 +- 4 files changed, 67 insertions(+), 16 deletions(-) diff --git a/internal/offline_download/add.go b/internal/offline_download/add.go index 67147f35..253d8a70 100644 --- a/internal/offline_download/add.go +++ b/internal/offline_download/add.go @@ -25,6 +25,10 @@ func AddURI(ctx context.Context, args *AddURIArgs) error { if err != nil { return errors.Wrapf(err, "failed get tool") } + // check tool is ready + if !tool.IsReady() { + return errors.Wrapf(err, "tool %s is not ready", args.Tool) + } // check storage storage, dstDirActualPath, err := op.GetStorageAndActualPath(args.DstDirPath) if err != nil { @@ -57,7 +61,7 @@ func AddURI(ctx context.Context, args *AddURIArgs) error { Signal: signal, }) if err != nil { - return errors.Wrapf(err, "failed to add uri %s", args.URI) + return errors.Wrapf(err, "[%s] failed to add uri %s", args.Tool, args.URI) } DownTaskManager.Submit(task.WithCancelCtx(&task.Task[string]{ ID: gid, diff --git a/internal/offline_download/all_test.go b/internal/offline_download/all_test.go index e8b32e09..2ba6cf27 100644 --- a/internal/offline_download/all_test.go +++ b/internal/offline_download/all_test.go @@ -1,13 +1,17 @@ -package offline_download +package offline_download_test -import "testing" +import ( + "testing" + + "github.com/alist-org/alist/v3/internal/offline_download" +) func TestGetFiles(t *testing.T) { - files, err := GetFiles("..") + files, err := offline_download.GetFiles("..") if err != nil { t.Fatal(err) } for _, file := range files { - t.Log(file.Name, file.Size, file.Path) + t.Log(file.Name, file.Size, file.Path, file.Modified) } } diff --git a/internal/offline_download/aria2/aria2.go b/internal/offline_download/aria2/aria2.go index 947b890c..67d8b9c8 100644 --- a/internal/offline_download/aria2/aria2.go +++ b/internal/offline_download/aria2/aria2.go @@ -3,6 +3,7 @@ package aria2 import ( "context" "fmt" + "strconv" "time" "github.com/alist-org/alist/v3/internal/conf" @@ -46,28 +47,70 @@ func (a *Aria2) Init() (string, error) { } func (a *Aria2) IsReady() bool { - //TODO implement me - panic("implement me") + return a.client != nil } func (a *Aria2) AddURI(args *offline_download.AddUriArgs) (string, error) { - //TODO implement me - panic("implement me") + options := map[string]interface{}{ + "dir": args.TempDir, + } + gid, err := a.client.AddURI([]string{args.Uri}, options) + if err != nil { + return "", err + } + return gid, nil } func (a *Aria2) Remove(tid string) error { - //TODO implement me - panic("implement me") + _, err := a.client.Remove(tid) + return err } func (a *Aria2) Status(tid string) (*offline_download.Status, error) { - //TODO implement me - panic("implement me") + info, err := a.client.TellStatus(tid) + if err != nil { + return nil, err + } + total, err := strconv.ParseUint(info.TotalLength, 10, 64) + if err != nil { + total = 0 + } + downloaded, err := strconv.ParseUint(info.CompletedLength, 10, 64) + if err != nil { + downloaded = 0 + } + s := &offline_download.Status{ + Completed: info.Status == "complete", + Err: err, + } + s.Progress = float64(downloaded) / float64(total) * 100 + if len(info.FollowedBy) != 0 { + s.NewTID = info.FollowedBy[0] + notify.Signals.Delete(tid) + //notify.Signals.Store(gid, m.c) + } + switch info.Status { + case "complete": + s.Completed = true + case "error": + s.Err = errors.Errorf("failed to download %s, error: %s", tid, info.ErrorMessage) + case "active": + s.Status = "aria2: " + info.Status + if info.Seeder == "true" { + s.Completed = true + } + case "waiting", "paused": + s.Status = "aria2: " + info.Status + case "removed": + s.Err = errors.Errorf("failed to download %s, removed", tid) + default: + return nil, errors.Errorf("[aria2] unknown status %s", info.Status) + } + return s, nil } func (a *Aria2) GetFile(tid string) *offline_download.File { - //TODO implement me - panic("implement me") + return nil } var _ offline_download.Tool = (*Aria2)(nil) diff --git a/internal/offline_download/base.go b/internal/offline_download/base.go index 10476bf7..a0768e5a 100644 --- a/internal/offline_download/base.go +++ b/internal/offline_download/base.go @@ -30,7 +30,7 @@ type Tool interface { IsReady() bool // AddURI add an uri to download, return the task id AddURI(args *AddUriArgs) (string, error) - // Remove the task if an error occurred + // 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)