Skip to content

Commit

Permalink
feat: support create task directly (GopeedLab#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyWie authored Nov 9, 2023
1 parent aae79c2 commit af52c4c
Show file tree
Hide file tree
Showing 12 changed files with 337 additions and 291 deletions.
4 changes: 2 additions & 2 deletions internal/protocol/bt/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (f *Fetcher) Wait() (err error) {
}
}
if !selected {
util.SafeRemove(filepath.Join(f.meta.Opts.Path, file.Path()))
util.SafeRemove(filepath.Join(f.meta.Opts.Path, f.meta.Res.Name, file.Path()))
}
}
break
Expand Down Expand Up @@ -202,7 +202,7 @@ func (f *Fetcher) updateRes() {
Size: file.Length(),
}
}
res.CalcSize()
res.CalcSize(nil)
f.meta.Res = res
if f.meta.Opts != nil {
f.meta.Opts.InitSelectFiles(len(res.Files))
Expand Down
9 changes: 6 additions & 3 deletions pkg/base/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package base
import (
"fmt"
"github.com/GopeedLab/gopeed/pkg/util"
"golang.org/x/exp/slices"
)

// Request download request
Expand Down Expand Up @@ -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
}
Expand Down
22 changes: 15 additions & 7 deletions pkg/download/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/download/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
2 changes: 0 additions & 2 deletions pkg/download/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -50,7 +49,6 @@ func (t *Task) clone() *Task {
Meta: t.Meta,
Status: t.Status,
Progress: t.Progress,
Size: t.Size,
CreatedAt: t.CreatedAt,
}
}
Expand Down
1 change: 1 addition & 0 deletions ui/flutter/assets/locales/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"donate": "Donate",
"exit": "Exit",
"create": "Create Task",
"directDownload": "Direct Download",
"advancedOptions": "Advanced Options",
"downloadLink": "Download Link",
"downloadLinkValid": "Please enter the download link",
Expand Down
1 change: 1 addition & 0 deletions ui/flutter/assets/locales/zh_CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"donate": "打赏",
"exit": "退出",
"create": "创建任务",
"directDownload": "直接下载",
"advancedOptions": "高级选项",
"downloadLink": "下载链接",
"downloadLinkValid": "请输入下载链接",
Expand Down
2 changes: 0 additions & 2 deletions ui/flutter/lib/api/model/task.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ class Task {
Meta meta;
Status status;
Progress progress;
int size;
DateTime createdAt;

Task({
required this.id,
required this.meta,
required this.status,
required this.progress,
required this.size,
required this.createdAt,
});

Expand Down
2 changes: 0 additions & 2 deletions ui/flutter/lib/api/model/task.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ class CreateController extends GetxController
final RxList fileInfos = [].obs;
final RxList openedFolders = [].obs;
final selectedIndexes = <int>[].obs;
final isResolving = false.obs;
final isConfirming = false.obs;
final showAdvanced = false.obs;
final directDownload = false.obs;
late TabController advancedTabController;
final oldUrl = "".obs;
final fileDataUri = "".obs;
Expand Down
Loading

0 comments on commit af52c4c

Please sign in to comment.