Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion internal/offline_download/115/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,10 @@ func (p *Cloud115) Status(task *tool.DownloadTask) (*tool.Status, error) {
s.Status = t.GetStatus()
s.Completed = t.IsDone()
s.TotalBytes = t.Size
s.FileName = t.Name
s.FileSize = t.Size
if t.IsFailed() {
s.Err = fmt.Errorf(t.GetStatus())
s.Err = fmt.Errorf("%s", t.GetStatus())
}
return s, nil
}
Expand Down
4 changes: 3 additions & 1 deletion internal/offline_download/115_open/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,10 @@ func (o *Open115) Status(task *tool.DownloadTask) (*tool.Status, error) {
s.Status = t.GetStatus()
s.Completed = t.IsDone()
s.TotalBytes = t.Size
s.FileName = t.Name
s.FileSize = t.Size
if t.IsFailed() {
s.Err = fmt.Errorf(t.GetStatus())
s.Err = fmt.Errorf("%s", t.GetStatus())
}
return s, nil
}
Expand Down
9 changes: 9 additions & 0 deletions internal/offline_download/123/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,22 @@ func (*Pan123) Status(task *tool.DownloadTask) (*tool.Status, error) {

return &tool.Status{
TotalBytes: t.Size,
FileName: offlineTaskFileName(t.Name, t.UploadName),
FileSize: t.Size,
Progress: t.Progress,
Completed: completed,
Status: statusStr,
Err: taskErr,
}, nil
}

func offlineTaskFileName(name, uploadName string) string {
if uploadName != "" {
return uploadName
}
return name
}

var _ tool.Tool = (*Pan123)(nil)

func init() {
Expand Down
20 changes: 19 additions & 1 deletion internal/offline_download/aria2/aria2.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aria2
import (
"context"
"fmt"
"path"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -99,8 +100,12 @@ func (a *Aria2) Status(task *tool.DownloadTask) (*tool.Status, error) {
Completed: info.Status == "complete",
Err: err,
TotalBytes: total,
FileName: fileNameFromStatus(info),
FileSize: total,
}
if total > 0 {
s.Progress = float64(downloaded) / float64(total) * 100
}
s.Progress = float64(downloaded) / float64(total) * 100
if len(info.FollowedBy) != 0 {
s.NewGID = info.FollowedBy[0]
notify.Signals.Delete(task.GID)
Expand All @@ -126,6 +131,19 @@ func (a *Aria2) Status(task *tool.DownloadTask) (*tool.Status, error) {
return s, nil
}

func fileNameFromStatus(info rpc.StatusInfo) string {
if info.BitTorrent.Info.Name != "" {
return info.BitTorrent.Info.Name
}
for _, file := range info.Files {
if file.Selected == "false" || file.Path == "" {
continue
}
return path.Base(file.Path)
}
return ""
}

var _ tool.Tool = (*Aria2)(nil)

func init() {
Expand Down
2 changes: 2 additions & 0 deletions internal/offline_download/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,13 @@ func (s SimpleHttp) Run(task *tool.DownloadTask) error {
filename = fmt.Sprintf("%s-%d-%x", filename, time.Now().UnixMilli(), rand.Uint32())
}
fileSize := resp.ContentLength
task.SetFileInfo(filename, fileSize)
if streamPut {
if fileSize == 0 {
start, end, _ := http_range.ParseContentRange(resp.Header.Get("Content-Range"))
fileSize = start + end
}
task.SetFileInfo(filename, fileSize)
task.SetTotalBytes(fileSize)
task.TempDir = filename
return nil
Expand Down
4 changes: 3 additions & 1 deletion internal/offline_download/pikpak/pikpak.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,15 @@ func (p *PikPak) Status(task *tool.DownloadTask) (*tool.Status, error) {
if t.ID == task.GID {
s.Progress = float64(t.Progress)
s.Status = t.Message
s.FileName = t.FileName
s.Completed = (t.Phase == "PHASE_TYPE_COMPLETE")
s.TotalBytes, err = strconv.ParseInt(t.FileSize, 10, 64)
if err != nil {
s.TotalBytes = 0
}
s.FileSize = s.TotalBytes
if t.Phase == "PHASE_TYPE_ERROR" {
s.Err = fmt.Errorf(t.Message)
s.Err = fmt.Errorf("%s", t.Message)
}
return s, nil
}
Expand Down
8 changes: 7 additions & 1 deletion internal/offline_download/qbit/qbit.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ func (a *QBittorrent) Status(task *tool.DownloadTask) (*tool.Status, error) {
}
s := &tool.Status{}
s.TotalBytes = info.Size
s.Progress = float64(info.Completed) / float64(info.Size) * 100
s.FileName = info.Name
s.FileSize = info.Size
if info.Size > 0 {
s.Progress = float64(info.Completed) / float64(info.Size) * 100
} else {
s.Progress = info.Progress * 100
}
switch info.State {
case qbittorrent.UPLOADING, qbittorrent.PAUSEDUP, qbittorrent.QUEUEDUP, qbittorrent.STALLEDUP, qbittorrent.FORCEDUP, qbittorrent.CHECKINGUP:
s.Completed = true
Expand Down
2 changes: 2 additions & 0 deletions internal/offline_download/thunder/thunder.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,13 @@ func (t *Thunder) Status(task *tool.DownloadTask) (*tool.Status, error) {
if t.ID == task.GID {
s.Progress = float64(t.Progress)
s.Status = t.Message
s.FileName = t.FileName
s.Completed = (t.Phase == "PHASE_TYPE_COMPLETE")
s.TotalBytes, err = strconv.ParseInt(t.FileSize, 10, 64)
if err != nil {
s.TotalBytes = 0
}
s.FileSize = s.TotalBytes
if t.Phase == "PHASE_TYPE_ERROR" {
s.Err = errors.New(t.Message)
}
Expand Down
2 changes: 2 additions & 0 deletions internal/offline_download/thunder_browser/thunder_browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,13 @@ func (t *ThunderBrowser) Status(task *tool.DownloadTask) (*tool.Status, error) {
if t.ID == task.GID {
s.Progress = float64(t.Progress)
s.Status = t.Message
s.FileName = t.FileName
s.Completed = t.Phase == "PHASE_TYPE_COMPLETE"
s.TotalBytes, err = strconv.ParseInt(t.FileSize, 10, 64)
if err != nil {
s.TotalBytes = 0
}
s.FileSize = s.TotalBytes
if t.Phase == "PHASE_TYPE_ERROR" {
s.Err = errors.New(t.Message)
}
Expand Down
2 changes: 2 additions & 0 deletions internal/offline_download/thunderx/thunderx.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,13 @@ func (t *ThunderX) Status(task *tool.DownloadTask) (*tool.Status, error) {
if t.ID == task.GID {
s.Progress = float64(t.Progress)
s.Status = t.Message
s.FileName = t.FileName
s.Completed = t.Phase == "PHASE_TYPE_COMPLETE"
s.TotalBytes, err = strconv.ParseInt(t.FileSize, 10, 64)
if err != nil {
s.TotalBytes = 0
}
s.FileSize = s.TotalBytes
if t.Phase == "PHASE_TYPE_ERROR" {
s.Err = errors.New(t.Message)
}
Expand Down
23 changes: 18 additions & 5 deletions internal/offline_download/tool/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,23 +175,36 @@ func AddURL(ctx context.Context, args *AddURLArgs) (task.TaskExtensionInfo, erro
TempDir: tempDir,
DeletePolicy: deletePolicy,
Toolname: args.Tool,
FileName: fileNameFromURL(args.URL),
tool: tool,
}
DownloadTaskManager.Add(t)
return t, nil
}

func tryPutUrl(ctx context.Context, path, urlStr string) error {
var dstName string
u, err := url.Parse(urlStr)
if err == nil {
dstName = stdpath.Base(u.Path)
} else {
dstName := fileNameFromURL(urlStr)
if dstName == "" {
dstName = "UnnamedURL"
}
return fs.PutURL(ctx, path, dstName, urlStr)
}

func fileNameFromURL(urlStr string) string {
u, err := url.Parse(urlStr)
if err != nil {
return ""
}
name := stdpath.Base(u.Path)
if name == "." || name == "/" {
return ""
}
if decodedName, err := url.PathUnescape(name); err == nil {
name = decodedName
}
return name
}

func isSimpleHttpSchemeUnsupported(urlStr string) bool {
u, err := url.Parse(strings.TrimSpace(urlStr))
if err != nil || u.Scheme == "" {
Expand Down
2 changes: 2 additions & 0 deletions internal/offline_download/tool/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type AddUrlArgs struct {

type Status struct {
TotalBytes int64
FileName string
FileSize int64
Progress float64
NewGID string
Completed bool
Expand Down
28 changes: 27 additions & 1 deletion internal/offline_download/tool/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type DownloadTask struct {
TempDir string `json:"temp_dir"`
DeletePolicy DeletePolicy `json:"delete_policy"`
Toolname string `json:"toolname"`
FileName string `json:"file_name,omitempty"`
FileSize int64 `json:"file_size,omitempty"`
Status string `json:"-"`
Signal chan int `json:"-"`
GID string `json:"-"`
Expand Down Expand Up @@ -153,8 +155,12 @@ func (t *DownloadTask) Update() (bool, error) {
return false, nil
}
t.callStatusRetried = 0
totalBytes := max(info.FileSize, info.TotalBytes)
t.SetFileInfo(info.FileName, totalBytes)
if totalBytes > 0 {
t.SetTotalBytes(totalBytes)
}
t.SetProgress(info.Progress)
t.SetTotalBytes(info.TotalBytes)
t.Status = fmt.Sprintf("[%s]: %s", t.tool.Name(), info.Status)
if info.NewGID != "" {
log.Debugf("followen by: %+v", info.NewGID)
Expand Down Expand Up @@ -215,6 +221,26 @@ func (t *DownloadTask) GetName() string {
return fmt.Sprintf("download %s to (%s)", t.Url, t.DstDirPath)
}

func (t *DownloadTask) SetFileInfo(fileName string, fileSize int64) {
if fileName != "" {
t.FileName = fileName
}
if fileSize > 0 {
t.FileSize = fileSize
}
}

func (t *DownloadTask) GetFileName() string {
return t.FileName
}

func (t *DownloadTask) GetFileSize() int64 {
if t.FileSize > 0 {
return t.FileSize
}
return t.GetTotalBytes()
}

func (t *DownloadTask) GetStatus() string {
return t.Status
}
Expand Down
8 changes: 7 additions & 1 deletion internal/offline_download/transmission/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,13 @@ func (t *Transmission) Status(task *tool.DownloadTask) (*tool.Status, error) {
Err: err,
}
s.Progress = *info.PercentDone * 100
s.TotalBytes = int64(*info.SizeWhenDone / 8)
if info.Name != nil {
s.FileName = *info.Name
}
if info.SizeWhenDone != nil {
s.TotalBytes = int64(*info.SizeWhenDone / 8)
s.FileSize = s.TotalBytes
}

switch *info.Status {
case transmissionrpc.TorrentStatusCheckWait,
Expand Down
15 changes: 15 additions & 0 deletions server/handles/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@ type TaskInfo struct {
StartTime *time.Time `json:"start_time"`
EndTime *time.Time `json:"end_time"`
TotalBytes int64 `json:"total_bytes"`
FileName string `json:"file_name,omitempty"`
FileSize int64 `json:"file_size,omitempty"`
Error string `json:"error"`
}

type taskFileInfo interface {
GetFileName() string
GetFileSize() int64
}

func getTaskInfo[T task.TaskExtensionInfo](task T) TaskInfo {
errMsg := ""
if task.GetErr() != nil {
Expand All @@ -46,6 +53,12 @@ func getTaskInfo[T task.TaskExtensionInfo](task T) TaskInfo {
creatorName = task.GetCreator().Username
creatorRole = task.GetCreator().Role
}
fileName := ""
fileSize := int64(0)
if taskWithFileInfo, ok := any(task).(taskFileInfo); ok {
fileName = taskWithFileInfo.GetFileName()
fileSize = taskWithFileInfo.GetFileSize()
}
return TaskInfo{
ID: task.GetID(),
Name: task.GetName(),
Expand All @@ -57,6 +70,8 @@ func getTaskInfo[T task.TaskExtensionInfo](task T) TaskInfo {
StartTime: task.GetStartTime(),
EndTime: task.GetEndTime(),
TotalBytes: task.GetTotalBytes(),
FileName: fileName,
FileSize: fileSize,
Error: errMsg,
}
}
Expand Down