Skip to content

Commit 9f22022

Browse files
gengjiawencodex
andcommitted
feat(task): expose offline download file info
Co-authored-by: Codex <codex@openai.com>
1 parent 054db9f commit 9f22022

15 files changed

Lines changed: 121 additions & 12 deletions

File tree

internal/offline_download/115/client.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,10 @@ func (p *Cloud115) Status(task *tool.DownloadTask) (*tool.Status, error) {
126126
s.Status = t.GetStatus()
127127
s.Completed = t.IsDone()
128128
s.TotalBytes = t.Size
129+
s.FileName = t.Name
130+
s.FileSize = t.Size
129131
if t.IsFailed() {
130-
s.Err = fmt.Errorf(t.GetStatus())
132+
s.Err = fmt.Errorf("%s", t.GetStatus())
131133
}
132134
return s, nil
133135
}

internal/offline_download/115_open/client.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,10 @@ func (o *Open115) Status(task *tool.DownloadTask) (*tool.Status, error) {
123123
s.Status = t.GetStatus()
124124
s.Completed = t.IsDone()
125125
s.TotalBytes = t.Size
126+
s.FileName = t.Name
127+
s.FileSize = t.Size
126128
if t.IsFailed() {
127-
s.Err = fmt.Errorf(t.GetStatus())
129+
s.Err = fmt.Errorf("%s", t.GetStatus())
128130
}
129131
return s, nil
130132
}

internal/offline_download/123/client.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,22 @@ func (*Pan123) Status(task *tool.DownloadTask) (*tool.Status, error) {
128128

129129
return &tool.Status{
130130
TotalBytes: t.Size,
131+
FileName: offlineTaskFileName(t.Name, t.UploadName),
132+
FileSize: t.Size,
131133
Progress: t.Progress,
132134
Completed: completed,
133135
Status: statusStr,
134136
Err: taskErr,
135137
}, nil
136138
}
137139

140+
func offlineTaskFileName(name, uploadName string) string {
141+
if uploadName != "" {
142+
return uploadName
143+
}
144+
return name
145+
}
146+
138147
var _ tool.Tool = (*Pan123)(nil)
139148

140149
func init() {

internal/offline_download/aria2/aria2.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package aria2
33
import (
44
"context"
55
"fmt"
6+
"path"
67
"strconv"
78
"strings"
89
"time"
@@ -99,8 +100,12 @@ func (a *Aria2) Status(task *tool.DownloadTask) (*tool.Status, error) {
99100
Completed: info.Status == "complete",
100101
Err: err,
101102
TotalBytes: total,
103+
FileName: fileNameFromStatus(info),
104+
FileSize: total,
105+
}
106+
if total > 0 {
107+
s.Progress = float64(downloaded) / float64(total) * 100
102108
}
103-
s.Progress = float64(downloaded) / float64(total) * 100
104109
if len(info.FollowedBy) != 0 {
105110
s.NewGID = info.FollowedBy[0]
106111
notify.Signals.Delete(task.GID)
@@ -126,6 +131,19 @@ func (a *Aria2) Status(task *tool.DownloadTask) (*tool.Status, error) {
126131
return s, nil
127132
}
128133

134+
func fileNameFromStatus(info rpc.StatusInfo) string {
135+
if info.BitTorrent.Info.Name != "" {
136+
return info.BitTorrent.Info.Name
137+
}
138+
for _, file := range info.Files {
139+
if file.Selected == "false" || file.Path == "" {
140+
continue
141+
}
142+
return path.Base(file.Path)
143+
}
144+
return ""
145+
}
146+
129147
var _ tool.Tool = (*Aria2)(nil)
130148

131149
func init() {

internal/offline_download/http/client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,13 @@ func (s SimpleHttp) Run(task *tool.DownloadTask) error {
7979
filename = fmt.Sprintf("%s-%d-%x", filename, time.Now().UnixMilli(), rand.Uint32())
8080
}
8181
fileSize := resp.ContentLength
82+
task.SetFileInfo(filename, fileSize)
8283
if streamPut {
8384
if fileSize == 0 {
8485
start, end, _ := http_range.ParseContentRange(resp.Header.Get("Content-Range"))
8586
fileSize = start + end
8687
}
88+
task.SetFileInfo(filename, fileSize)
8789
task.SetTotalBytes(fileSize)
8890
task.TempDir = filename
8991
return nil

internal/offline_download/pikpak/pikpak.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,15 @@ func (p *PikPak) Status(task *tool.DownloadTask) (*tool.Status, error) {
123123
if t.ID == task.GID {
124124
s.Progress = float64(t.Progress)
125125
s.Status = t.Message
126+
s.FileName = t.FileName
126127
s.Completed = (t.Phase == "PHASE_TYPE_COMPLETE")
127128
s.TotalBytes, err = strconv.ParseInt(t.FileSize, 10, 64)
128129
if err != nil {
129130
s.TotalBytes = 0
130131
}
132+
s.FileSize = s.TotalBytes
131133
if t.Phase == "PHASE_TYPE_ERROR" {
132-
s.Err = fmt.Errorf(t.Message)
134+
s.Err = fmt.Errorf("%s", t.Message)
133135
}
134136
return s, nil
135137
}

internal/offline_download/qbit/qbit.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,13 @@ func (a *QBittorrent) Status(task *tool.DownloadTask) (*tool.Status, error) {
6565
}
6666
s := &tool.Status{}
6767
s.TotalBytes = info.Size
68-
s.Progress = float64(info.Completed) / float64(info.Size) * 100
68+
s.FileName = info.Name
69+
s.FileSize = info.Size
70+
if info.Size > 0 {
71+
s.Progress = float64(info.Completed) / float64(info.Size) * 100
72+
} else {
73+
s.Progress = info.Progress * 100
74+
}
6975
switch info.State {
7076
case qbittorrent.UPLOADING, qbittorrent.PAUSEDUP, qbittorrent.QUEUEDUP, qbittorrent.STALLEDUP, qbittorrent.FORCEDUP, qbittorrent.CHECKINGUP:
7177
s.Completed = true

internal/offline_download/thunder/thunder.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,13 @@ func (t *Thunder) Status(task *tool.DownloadTask) (*tool.Status, error) {
124124
if t.ID == task.GID {
125125
s.Progress = float64(t.Progress)
126126
s.Status = t.Message
127+
s.FileName = t.FileName
127128
s.Completed = (t.Phase == "PHASE_TYPE_COMPLETE")
128129
s.TotalBytes, err = strconv.ParseInt(t.FileSize, 10, 64)
129130
if err != nil {
130131
s.TotalBytes = 0
131132
}
133+
s.FileSize = s.TotalBytes
132134
if t.Phase == "PHASE_TYPE_ERROR" {
133135
s.Err = errors.New(t.Message)
134136
}

internal/offline_download/thunder_browser/thunder_browser.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,13 @@ func (t *ThunderBrowser) Status(task *tool.DownloadTask) (*tool.Status, error) {
151151
if t.ID == task.GID {
152152
s.Progress = float64(t.Progress)
153153
s.Status = t.Message
154+
s.FileName = t.FileName
154155
s.Completed = t.Phase == "PHASE_TYPE_COMPLETE"
155156
s.TotalBytes, err = strconv.ParseInt(t.FileSize, 10, 64)
156157
if err != nil {
157158
s.TotalBytes = 0
158159
}
160+
s.FileSize = s.TotalBytes
159161
if t.Phase == "PHASE_TYPE_ERROR" {
160162
s.Err = errors.New(t.Message)
161163
}

internal/offline_download/thunderx/thunderx.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,13 @@ func (t *ThunderX) Status(task *tool.DownloadTask) (*tool.Status, error) {
118118
if t.ID == task.GID {
119119
s.Progress = float64(t.Progress)
120120
s.Status = t.Message
121+
s.FileName = t.FileName
121122
s.Completed = t.Phase == "PHASE_TYPE_COMPLETE"
122123
s.TotalBytes, err = strconv.ParseInt(t.FileSize, 10, 64)
123124
if err != nil {
124125
s.TotalBytes = 0
125126
}
127+
s.FileSize = s.TotalBytes
126128
if t.Phase == "PHASE_TYPE_ERROR" {
127129
s.Err = errors.New(t.Message)
128130
}

0 commit comments

Comments
 (0)