Skip to content

Commit

Permalink
feat: create task with advanced options (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyWie authored Jul 19, 2023
1 parent eb79dd5 commit 7376e85
Show file tree
Hide file tree
Showing 22 changed files with 357 additions and 136 deletions.
35 changes: 28 additions & 7 deletions internal/protocol/bt/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/GopeedLab/gopeed/internal/controller"
"github.com/GopeedLab/gopeed/internal/fetcher"
"github.com/GopeedLab/gopeed/pkg/base"
"github.com/GopeedLab/gopeed/pkg/protocol/bt"
"github.com/GopeedLab/gopeed/pkg/util"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/metainfo"
Expand Down Expand Up @@ -68,7 +69,11 @@ func (f *Fetcher) initClient() (err error) {
}

func (f *Fetcher) Resolve(req *base.Request) error {
if err := f.addTorrent(req.URL); err != nil {
if err := base.ParseReqExtra[bt.ReqExtra](req); err != nil {
return err
}

if err := f.addTorrent(req); err != nil {
return err
}
go func() {
Expand Down Expand Up @@ -122,7 +127,7 @@ func (f *Fetcher) Create(opts *base.Options) (err error) {

func (f *Fetcher) Start() (err error) {
if !f.torrentReady.Load() {
if err = f.addTorrent(f.meta.Req.URL); err != nil {
if err = f.addTorrent(f.meta.Req); err != nil {
return
}
}
Expand Down Expand Up @@ -211,15 +216,15 @@ func (f *Fetcher) Progress() fetcher.Progress {
return f.progress
}

func (f *Fetcher) addTorrent(url string) (err error) {
func (f *Fetcher) addTorrent(req *base.Request) (err error) {
if err = f.initClient(); err != nil {
return
}
schema := util.ParseSchema(url)
schema := util.ParseSchema(req.URL)
if schema == "MAGNET" {
f.torrent, err = client.AddMagnet(url)
f.torrent, err = client.AddMagnet(req.URL)
} else {
f.torrent, err = client.AddTorrentFromFile(url)
f.torrent, err = client.AddTorrentFromFile(req.URL)
}
if err != nil {
return
Expand All @@ -229,9 +234,25 @@ func (f *Fetcher) addTorrent(url string) (err error) {
if err != nil {
return
}

// use map to deduplicate
trackers := make(map[string]bool)
if req.Extra != nil {
extra := req.Extra.(*bt.ReqExtra)
if len(extra.Trackers) > 0 {
for _, tracker := range extra.Trackers {
trackers[tracker] = true
}
}
}
if exist && len(cfg.Trackers) > 0 {
announceList := make([][]string, 0)
for _, tracker := range cfg.Trackers {
trackers[tracker] = true
}
}
if len(trackers) > 0 {
announceList := make([][]string, 0)
for tracker := range trackers {
announceList = append(announceList, []string{tracker})
}
f.torrent.AddTrackers(announceList)
Expand Down
7 changes: 7 additions & 0 deletions internal/protocol/bt/fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/GopeedLab/gopeed/internal/fetcher"
"github.com/GopeedLab/gopeed/internal/test"
"github.com/GopeedLab/gopeed/pkg/base"
"github.com/GopeedLab/gopeed/pkg/protocol/bt"
"reflect"
"testing"
)
Expand All @@ -21,6 +22,12 @@ func TestFetcher_Config(t *testing.T) {
func doResolve(t *testing.T, fetcher fetcher.Fetcher) {
err := fetcher.Resolve(&base.Request{
URL: "./testdata/ubuntu-22.04-live-server-amd64.iso.torrent",
Extra: bt.ReqExtra{
Trackers: []string{
"udp://tracker.birkenwald.de:6969/announce",
"udp://tracker.bitsearch.to:1337/announce",
},
},
})
if err != nil {
panic(err)
Expand Down
5 changes: 5 additions & 0 deletions pkg/protocol/bt/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package bt

type ReqExtra struct {
Trackers []string `json:"trackers"`
}
2 changes: 1 addition & 1 deletion ui/flutter/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ subprojects {
project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
2 changes: 2 additions & 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",
"advancedOptions": "Advanced Options",
"downloadLink": "Download Link",
"downloadLinkValid": "Please enter the download link",
"downloadLinkHit": "Please enter the download link, HTTP/HTTPS/MAGNET supported@append",
Expand All @@ -20,6 +21,7 @@
"noFileSelected": "Please select at least one file to continue.",
"noStoragePermission": "Storage permission required",
"selectFile": "Select File",
"rename": "Rename",
"basic": "Basic",
"advanced": "Advanced",
"general": "General",
Expand Down
2 changes: 2 additions & 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": "创建任务",
"advancedOptions": "高级选项",
"downloadLink": "下载链接",
"downloadLinkValid": "请输入下载链接",
"downloadLinkHit": "请输入下载链接,支持 HTTP/HTTPS/MAGNET@append",
Expand All @@ -20,6 +21,7 @@
"noFileSelected": "请至少选择一个文件下载",
"noStoragePermission": "需要开启存储权限",
"selectFile": "选择文件",
"rename": "重命名",
"basic": "基础",
"advanced": "高级",
"general": "通用",
Expand Down
5 changes: 3 additions & 2 deletions ui/flutter/lib/api/model/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ import 'package:json_annotation/json_annotation.dart';

part 'options.g.dart';

@JsonSerializable(explicitToJson: true, genericArgumentFactories: true)
@JsonSerializable(explicitToJson: true)
class Options {
String name;
String path;
List<int> selectFiles;
Map<String, dynamic>? extra;
Object? extra;

Options({
required this.name,
required this.path,
required this.selectFiles,
this.extra,
});

factory Options.fromJson(Map<String, dynamic> json) =>
Expand Down
3 changes: 2 additions & 1 deletion ui/flutter/lib/api/model/options.g.dart

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

17 changes: 15 additions & 2 deletions ui/flutter/lib/api/model/request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ part 'request.g.dart';
@JsonSerializable(explicitToJson: true)
class Request {
String url;
Map<String, dynamic>? extra;
Object? extra;

Request({
required this.url,
this.extra,
});

factory Request.fromJson(Map<String, dynamic> json) =>
Expand All @@ -20,7 +21,7 @@ class Request {
@JsonSerializable()
class ReqExtraHttp {
String method = 'GET';
Map<String, String> headers = {};
Map<String, String> header = {};
String body = '';

ReqExtraHttp();
Expand All @@ -30,3 +31,15 @@ class ReqExtraHttp {

Map<String, dynamic> toJson() => _$ReqExtraHttpToJson(this);
}

@JsonSerializable()
class ReqExtraBt {
List<String> trackers = [];

ReqExtraBt();

factory ReqExtraBt.fromJson(Map<String, dynamic> json) =>
_$ReqExtraBtFromJson(json);

Map<String, dynamic> toJson() => _$ReqExtraBtToJson(this);
}
16 changes: 13 additions & 3 deletions ui/flutter/lib/api/model/request.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
@@ -1,6 +1,7 @@
import 'dart:async';
import 'dart:convert';
import 'dart:ui' as ui;
import 'dart:ui';

import 'package:app_links/app_links.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -337,7 +338,7 @@ class AppController extends GetxController with WindowListener, TrayListener {
extra.themeMode = ThemeMode.system.name;
}
if (extra.locale.isEmpty) {
final systemLocale = getLocaleKey(ui.window.locale);
final systemLocale = getLocaleKey(PlatformDispatcher.instance.locale);
extra.locale = AppTranslation.translations.containsKey(systemLocale)
? systemLocale
: getLocaleKey(fallbackLocale);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class CreateController extends GetxController {
class CreateController extends GetxController
with GetSingleTickerProviderStateMixin {
// final files = [].obs;
final RxList fileInfos = [].obs;
final RxList selectedIndexes = [].obs;
final RxList openedFolders = [].obs;
final isResolving = false.obs;
final showAdvanced = false.obs;
late TabController advancedTabController;

@override
void onInit() {
super.onInit();
advancedTabController = TabController(length: 2, vsync: this);
}

@override
void onClose() {
advancedTabController.dispose();
super.onClose();
}
}
Loading

0 comments on commit 7376e85

Please sign in to comment.