diff --git a/db/db.go b/db/db.go index 0b9ee4c..0ebc620 100644 --- a/db/db.go +++ b/db/db.go @@ -69,14 +69,33 @@ func (c *client) init() { log.Infof("set default log level") c.SetSetting(SettingLogLevel, "info") } - // if tr := c.GetAllDonloadClients(); len(tr) == 0 { - // log.Warnf("no download client, set default download client") - // c.SaveDownloader(&ent.DownloadClients{ - // Name: "transmission", - // Implementation: downloadclients.ImplementationTransmission, - // URL: "http://transmission:9091", - // }) - // } + c.initBuildinClient() +} + +func (c *client) initBuildinClient() { + hasBuildin := false + tr := c.GetAllDonloadClients() + for _, d := range tr { + if d.Implementation == downloadclients.ImplementationBuildin { + hasBuildin = true + break + } + } + if !hasBuildin { + log.Warnf("no buildin download client, set default download client") + if err := c.SaveDownloader(&ent.DownloadClients{ + Enable: true, + Name: "内建下载器", + Implementation: downloadclients.ImplementationBuildin, + URL: "buildin", + Priority1: 50, + RemoveCompletedDownloads: true, + RemoveFailedDownloads: true, + }); err != nil { + log.Warnf("add buildin client error: %v", err) + } + } + } func (c *client) generateJwtSerectIfNotExist() { @@ -323,26 +342,13 @@ func (c *client) GetAllDonloadClients() []*ent.DownloadClients { cc, err := c.ent.DownloadClients.Query().Order(ent.Asc(downloadclients.FieldPriority1)).All(context.TODO()) if err != nil { log.Errorf("no download client") - return []*ent.DownloadClients{ - { - Implementation: downloadclients.ImplementationBuildin, - Name: "内建下载器", - Priority1: 9999, - Enable: true, - }, - } + return nil } - cc = append(cc, &ent.DownloadClients{ - Implementation: downloadclients.ImplementationBuildin, - Name: "内建下载器", - Priority1: 9999, - Enable: true, - }) return cc } -func (c *client) DeleteDownloadCLient(id int) { - c.ent.DownloadClients.Delete().Where(downloadclients.ID(id)).Exec(context.TODO()) +func (c *client) DeleteDownloadCLient(id int) { //not delete buildin client + c.ent.DownloadClients.Delete().Where(downloadclients.ID(id), downloadclients.ImplementationNEQ(downloadclients.ImplementationBuildin)).Exec(context.TODO()) } // Storage is the model entity for the Storage schema. diff --git a/engine/client.go b/engine/client.go index 9883075..5f4a3e6 100644 --- a/engine/client.go +++ b/engine/client.go @@ -65,6 +65,8 @@ func (c *Engine) reloadUsingBuildinDownloader(h *ent.History) error { if err != nil { return errors.Wrap(err, "download torrent") } + t.Start() + c.tasks.Store(h.ID, &Task{Torrent: t}) return nil } @@ -137,6 +139,14 @@ func (c *Engine) reloadTasks() { } c.tasks.Store(t.ID, &Task{Torrent: to}) } + } else if dl.Implementation == downloadclients.ImplementationBuildin { + err := c.reloadUsingBuildinDownloader(t) + if err != nil { + log.Warnf("buildin downloader error: %v", err) + } else { + log.Infof("success reloading buildin task: %v", t.SourceTitle) + } + } } diff --git a/ui/lib/settings/downloader.dart b/ui/lib/settings/downloader.dart index a75f146..01de8b4 100644 --- a/ui/lib/settings/downloader.dart +++ b/ui/lib/settings/downloader.dart @@ -68,13 +68,13 @@ class _DownloaderState extends ConsumerState { children: [ FormBuilderTextField( name: "name", - enabled: client.idExists(), + enabled: client.implementation != "buildin", decoration: const InputDecoration(labelText: "名称"), validator: FormBuilderValidators.required(), autovalidateMode: AutovalidateMode.onUserInteraction), FormBuilderTextField( name: "url", - enabled: client.idExists(), + enabled: client.implementation != "buildin", decoration: const InputDecoration( labelText: "地址", hintText: "http://127.0.0.1:9091"), autovalidateMode: AutovalidateMode.onUserInteraction, @@ -82,7 +82,6 @@ class _DownloaderState extends ConsumerState { ), FormBuilderTextField( name: "priority", - enabled: client.idExists(), decoration: const InputDecoration( labelText: "优先级", helperText: "1-50, 1最高优先级,50最低优先级"), validator: FormBuilderValidators.integer(), @@ -99,7 +98,7 @@ class _DownloaderState extends ConsumerState { children: [ FormBuilderSwitch( name: "auth", - enabled: client.idExists(), + enabled: client.implementation != "buildin", title: const Text("需要认证"), initialValue: _enableAuth, onChanged: (v) { @@ -166,7 +165,7 @@ class _DownloaderState extends ConsumerState { } return showSettingDialog( - context, title, client.idExists(), body, onSubmit, onDelete); + context, title, client.idExists() && client.implementation != "buildin", body, onSubmit, onDelete); } Future showSelections() {