feat: support create task directly (#262)

This commit is contained in:
Levi authored and GitHub committed 2023-11-09 16:52:02 +08:00
1 parent aae79c2449
commit af52c4ce7e
12 files changed
+343 -297

No files matched your search

+6 -3
View File
@@ -3,6 +3,7 @@ package base
import (
"fmt"
"github.com/GopeedLab/gopeed/pkg/util"
"golang.org/x/exp/slices"
)
// Request download request
@@ -47,10 +48,12 @@ func (r *Resource) Validate() error {
return nil
}
func (r *Resource) CalcSize() {
func (r *Resource) CalcSize(selectFiles []int) {
var size int64
for _, file := range r.Files {
size += file.Size
for i, file := range r.Files {
if len(selectFiles) == 0 || slices.Contains(selectFiles, i) {
size += file.Size
}
}
r.Size = size
}
+15 -7
View File
@@ -540,11 +540,17 @@ func (d *Downloader) GetTask(id string) *Task {
}
func (d *Downloader) GetTasks() []*Task {
// reverse tasks, so that the latest task is in the front
tasks := make([]*Task, len(d.tasks))
for i := 0; i < len(d.tasks); i++ {
tasks[i] = d.tasks[len(d.tasks)-i-1]
tasks[i] = d.tasks[i]
}
// sort tasks by status, order by Status(if(running,1,0)) desc, CreatedAt desc
sort.Slice(tasks, func(i, j int) bool {
if tasks[i].Status != tasks[j].Status {
return tasks[i].Status == base.DownloadStatusRunning
}
return tasks[i].CreatedAt.After(tasks[j].CreatedAt)
})
return tasks
}
@@ -579,16 +585,16 @@ func (d *Downloader) watch(task *Task) {
d.emit(EventKeyError, task, err)
} else {
task.Progress.Used = task.timer.Used()
if task.Size == 0 {
task.Size = task.fetcher.Progress().TotalDownloaded()
task.Meta.Res.Size = task.Size
if task.Meta.Res.Size == 0 {
task.Meta.Res.Size = task.fetcher.Progress().TotalDownloaded()
}
used := task.Progress.Used / int64(time.Second)
if used == 0 {
used = 1
}
task.Progress.Speed = task.Size / used
task.Progress.Downloaded = task.Size
totalSize := task.Meta.Res.Size
task.Progress.Speed = totalSize / used
task.Progress.Downloaded = totalSize
task.Status = base.DownloadStatusDone
d.storage.Put(bucketTask, task.ID, task.clone())
d.emit(EventKeyDone, task)
@@ -741,6 +747,8 @@ func (d *Downloader) doStart(task *Task) (err error) {
}
task.Meta.Opts.Name = newName
}
task.Meta.Res.CalcSize(task.Meta.Opts.SelectFiles)
}
task.Progress.Speed = 0
+1 -1
View File
@@ -243,7 +243,7 @@ func (d *Downloader) triggerOnResolve(req *base.Request) (res *base.Resource) {
gopeed.Logger.logger.Warn().Err(err).Msgf("[%s] resource invalid", ext.buildIdentity())
return
}
ctx.Res.CalcSize()
ctx.Res.CalcSize(nil)
}
res = ctx.Res
},
-2
View File
@@ -22,7 +22,6 @@ type Task struct {
Meta *fetcher.FetcherMeta `json:"meta"`
Status base.Status `json:"status"`
Progress *Progress `json:"progress"`
Size int64 `json:"size"`
CreatedAt time.Time `json:"createdAt"`
fetcherBuilder fetcher.FetcherBuilder
@@ -50,7 +49,6 @@ func (t *Task) clone() *Task {
Meta: t.Meta,
Status: t.Status,
Progress: t.Progress,
Size: t.Size,
CreatedAt: t.CreatedAt,
}
}