Skip to content

Commit 2267b04

Browse files
committed
refactor: buildin client
1 parent 8973fe9 commit 2267b04

File tree

3 files changed

+44
-29
lines changed

3 files changed

+44
-29
lines changed

db/db.go

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,33 @@ func (c *client) init() {
6969
log.Infof("set default log level")
7070
c.SetSetting(SettingLogLevel, "info")
7171
}
72-
// if tr := c.GetAllDonloadClients(); len(tr) == 0 {
73-
// log.Warnf("no download client, set default download client")
74-
// c.SaveDownloader(&ent.DownloadClients{
75-
// Name: "transmission",
76-
// Implementation: downloadclients.ImplementationTransmission,
77-
// URL: "http://transmission:9091",
78-
// })
79-
// }
72+
c.initBuildinClient()
73+
}
74+
75+
func (c *client) initBuildinClient() {
76+
hasBuildin := false
77+
tr := c.GetAllDonloadClients()
78+
for _, d := range tr {
79+
if d.Implementation == downloadclients.ImplementationBuildin {
80+
hasBuildin = true
81+
break
82+
}
83+
}
84+
if !hasBuildin {
85+
log.Warnf("no buildin download client, set default download client")
86+
if err := c.SaveDownloader(&ent.DownloadClients{
87+
Enable: true,
88+
Name: "内建下载器",
89+
Implementation: downloadclients.ImplementationBuildin,
90+
URL: "buildin",
91+
Priority1: 50,
92+
RemoveCompletedDownloads: true,
93+
RemoveFailedDownloads: true,
94+
}); err != nil {
95+
log.Warnf("add buildin client error: %v", err)
96+
}
97+
}
98+
8099
}
81100

82101
func (c *client) generateJwtSerectIfNotExist() {
@@ -323,26 +342,13 @@ func (c *client) GetAllDonloadClients() []*ent.DownloadClients {
323342
cc, err := c.ent.DownloadClients.Query().Order(ent.Asc(downloadclients.FieldPriority1)).All(context.TODO())
324343
if err != nil {
325344
log.Errorf("no download client")
326-
return []*ent.DownloadClients{
327-
{
328-
Implementation: downloadclients.ImplementationBuildin,
329-
Name: "内建下载器",
330-
Priority1: 9999,
331-
Enable: true,
332-
},
333-
}
345+
return nil
334346
}
335-
cc = append(cc, &ent.DownloadClients{
336-
Implementation: downloadclients.ImplementationBuildin,
337-
Name: "内建下载器",
338-
Priority1: 9999,
339-
Enable: true,
340-
})
341347
return cc
342348
}
343349

344-
func (c *client) DeleteDownloadCLient(id int) {
345-
c.ent.DownloadClients.Delete().Where(downloadclients.ID(id)).Exec(context.TODO())
350+
func (c *client) DeleteDownloadCLient(id int) { //not delete buildin client
351+
c.ent.DownloadClients.Delete().Where(downloadclients.ID(id), downloadclients.ImplementationNEQ(downloadclients.ImplementationBuildin)).Exec(context.TODO())
346352
}
347353

348354
// Storage is the model entity for the Storage schema.

engine/client.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ func (c *Engine) reloadUsingBuildinDownloader(h *ent.History) error {
6565
if err != nil {
6666
return errors.Wrap(err, "download torrent")
6767
}
68+
t.Start()
69+
6870
c.tasks.Store(h.ID, &Task{Torrent: t})
6971
return nil
7072
}
@@ -137,6 +139,14 @@ func (c *Engine) reloadTasks() {
137139
}
138140
c.tasks.Store(t.ID, &Task{Torrent: to})
139141
}
142+
} else if dl.Implementation == downloadclients.ImplementationBuildin {
143+
err := c.reloadUsingBuildinDownloader(t)
144+
if err != nil {
145+
log.Warnf("buildin downloader error: %v", err)
146+
} else {
147+
log.Infof("success reloading buildin task: %v", t.SourceTitle)
148+
}
149+
140150
}
141151

142152
}

ui/lib/settings/downloader.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,20 @@ class _DownloaderState extends ConsumerState<DownloaderSettings> {
6868
children: [
6969
FormBuilderTextField(
7070
name: "name",
71-
enabled: client.idExists(),
71+
enabled: client.implementation != "buildin",
7272
decoration: const InputDecoration(labelText: "名称"),
7373
validator: FormBuilderValidators.required(),
7474
autovalidateMode: AutovalidateMode.onUserInteraction),
7575
FormBuilderTextField(
7676
name: "url",
77-
enabled: client.idExists(),
77+
enabled: client.implementation != "buildin",
7878
decoration: const InputDecoration(
7979
labelText: "地址", hintText: "http://127.0.0.1:9091"),
8080
autovalidateMode: AutovalidateMode.onUserInteraction,
8181
validator: FormBuilderValidators.required(),
8282
),
8383
FormBuilderTextField(
8484
name: "priority",
85-
enabled: client.idExists(),
8685
decoration: const InputDecoration(
8786
labelText: "优先级", helperText: "1-50, 1最高优先级,50最低优先级"),
8887
validator: FormBuilderValidators.integer(),
@@ -99,7 +98,7 @@ class _DownloaderState extends ConsumerState<DownloaderSettings> {
9998
children: [
10099
FormBuilderSwitch(
101100
name: "auth",
102-
enabled: client.idExists(),
101+
enabled: client.implementation != "buildin",
103102
title: const Text("需要认证"),
104103
initialValue: _enableAuth,
105104
onChanged: (v) {
@@ -166,7 +165,7 @@ class _DownloaderState extends ConsumerState<DownloaderSettings> {
166165
}
167166

168167
return showSettingDialog(
169-
context, title, client.idExists(), body, onSubmit, onDelete);
168+
context, title, client.idExists() && client.implementation != "buildin", body, onSubmit, onDelete);
170169
}
171170

172171
Future<void> showSelections() {

0 commit comments

Comments
 (0)