From 674263c0d8d7baea8359df87b6b4bfd66d8e7631 Mon Sep 17 00:00:00 2001 From: ShukeBta <272197458+ShukeBta@users.noreply.github.com> Date: Sat, 27 Jun 2026 11:31:23 +0800 Subject: [PATCH] split download runtime config helpers --- internal/service/downloads.go | 77 ------------------ internal/service/downloads_config_runtime.go | 85 ++++++++++++++++++++ 2 files changed, 85 insertions(+), 77 deletions(-) create mode 100644 internal/service/downloads_config_runtime.go diff --git a/internal/service/downloads.go b/internal/service/downloads.go index 800c8d3..05f0d98 100644 --- a/internal/service/downloads.go +++ b/internal/service/downloads.go @@ -70,8 +70,6 @@ func (d *DownloadService) SetNotifyChannels(notify *NotifyChannelService) { d.notify = notify } -const settingDownloadClientsManaged = "download_clients.managed" - // ErrDownloadAlreadyExists tells callers that the requested resource is already // tracked locally or present in qBittorrent. Subscriptions treat this as a // successful dedup hit, not as a retryable enqueue failure. @@ -121,81 +119,6 @@ func (d *DownloadService) Stop() { close(d.stopCh) } -// ReloadConfig rebuilds the qBittorrent client from the configured -// download clients (preferred) or the legacy Setting table (fallback). -// -// 配置来源优先级: -// -// 1. download_clients 表中 type=qbittorrent 且 is_default=true 且 enabled=true -// 的行(侧边栏「下载器」页面写入的数据)。 -// 2. system Setting 表中的 qbittorrent.url / username / password -// (旧版「系统设置」表单写入的数据;保留作向后兼容)。 -// -// 这避免了两套配置各跑各的:之前操作员明明已经在「下载器」页面填好 -// 默认 qb,但实际下载链路读的还是 Setting 表,导致一直连不上。 -func (d *DownloadService) ReloadConfig(ctx context.Context) error { - cfg := QBitConfig{} - hasConfiguredClients := false - managedByDownloadClients := false - - // Path 1: download_clients 表 - if d.repo.DownloadClient != nil { - hasConfiguredClients, _ = d.repo.DownloadClient.HasAnyIncludingDeleted(ctx) - if c, err := d.repo.DownloadClient.FindDefault(ctx); err == nil && c != nil && c.Type == "qbittorrent" { - cfg.BaseURL = strings.TrimRight(c.Host, "/") - cfg.Username = c.Username - cfg.Password = c.Password - } else if c, err := d.soleEnabledQBitClient(ctx); err == nil && c != nil { - cfg.BaseURL = strings.TrimRight(c.Host, "/") - cfg.Username = c.Username - cfg.Password = c.Password - } - } - if d.repo.Setting != nil { - managedRaw, _ := d.repo.Setting.Get(ctx, settingDownloadClientsManaged) - managedByDownloadClients = strings.EqualFold(strings.TrimSpace(managedRaw), "true") - } - - // Path 2: legacy Setting 表。 - // 仅在旧部署“从未使用过 download_clients 表”时回退。只要操作员曾经 - // 配置过下载器,删除/禁用全部下载器就表示应停止投递,不能再偷偷用 - // qbittorrent.* 旧设置继续往下载器添加任务。 - if cfg.BaseURL == "" && !hasConfiguredClients && !managedByDownloadClients { - get := func(k string) string { - v, _ := d.repo.Setting.Get(ctx, k) - return v - } - cfg.BaseURL = get("qbittorrent.url") - cfg.Username = get("qbittorrent.username") - cfg.Password = get("qbittorrent.password") - } - - d.qb.Configure(cfg) - return nil -} - -func (d *DownloadService) soleEnabledQBitClient(ctx context.Context) (*model.DownloadClient, error) { - if d == nil || d.repo == nil || d.repo.DownloadClient == nil { - return nil, nil - } - rows, err := d.repo.DownloadClient.ListEnabled(ctx) - if err != nil { - return nil, err - } - var selected *model.DownloadClient - for i := range rows { - if rows[i].Type != "qbittorrent" { - continue - } - if selected != nil { - return nil, nil - } - row := rows[i] - selected = &row - } - return selected, nil -} - func (d *DownloadService) TorrentExistsByName(ctx context.Context, name string) bool { query := normalizeTorrentName(name) if query == "" { diff --git a/internal/service/downloads_config_runtime.go b/internal/service/downloads_config_runtime.go new file mode 100644 index 0000000..aa34910 --- /dev/null +++ b/internal/service/downloads_config_runtime.go @@ -0,0 +1,85 @@ +package service + +import ( + "context" + "strings" + + "github.com/ShukeBta/MediaStationGo/internal/model" +) + +const settingDownloadClientsManaged = "download_clients.managed" + +// ReloadConfig rebuilds the qBittorrent client from the configured +// download clients (preferred) or the legacy Setting table (fallback). +// +// 配置来源优先级: +// +// 1. download_clients 表中 type=qbittorrent 且 is_default=true 且 enabled=true +// 的行(侧边栏「下载器」页面写入的数据)。 +// 2. system Setting 表中的 qbittorrent.url / username / password +// (旧版「系统设置」表单写入的数据;保留作向后兼容)。 +// +// 这避免了两套配置各跑各的:之前操作员明明已经在「下载器」页面填好 +// 默认 qb,但实际下载链路读的还是 Setting 表,导致一直连不上。 +func (d *DownloadService) ReloadConfig(ctx context.Context) error { + cfg := QBitConfig{} + hasConfiguredClients := false + managedByDownloadClients := false + + // Path 1: download_clients 表 + if d.repo.DownloadClient != nil { + hasConfiguredClients, _ = d.repo.DownloadClient.HasAnyIncludingDeleted(ctx) + if c, err := d.repo.DownloadClient.FindDefault(ctx); err == nil && c != nil && c.Type == "qbittorrent" { + cfg.BaseURL = strings.TrimRight(c.Host, "/") + cfg.Username = c.Username + cfg.Password = c.Password + } else if c, err := d.soleEnabledQBitClient(ctx); err == nil && c != nil { + cfg.BaseURL = strings.TrimRight(c.Host, "/") + cfg.Username = c.Username + cfg.Password = c.Password + } + } + if d.repo.Setting != nil { + managedRaw, _ := d.repo.Setting.Get(ctx, settingDownloadClientsManaged) + managedByDownloadClients = strings.EqualFold(strings.TrimSpace(managedRaw), "true") + } + + // Path 2: legacy Setting 表。 + // 仅在旧部署“从未使用过 download_clients 表”时回退。只要操作员曾经 + // 配置过下载器,删除/禁用全部下载器就表示应停止投递,不能再偷偷用 + // qbittorrent.* 旧设置继续往下载器添加任务。 + if cfg.BaseURL == "" && !hasConfiguredClients && !managedByDownloadClients { + get := func(k string) string { + v, _ := d.repo.Setting.Get(ctx, k) + return v + } + cfg.BaseURL = get("qbittorrent.url") + cfg.Username = get("qbittorrent.username") + cfg.Password = get("qbittorrent.password") + } + + d.qb.Configure(cfg) + return nil +} + +func (d *DownloadService) soleEnabledQBitClient(ctx context.Context) (*model.DownloadClient, error) { + if d == nil || d.repo == nil || d.repo.DownloadClient == nil { + return nil, nil + } + rows, err := d.repo.DownloadClient.ListEnabled(ctx) + if err != nil { + return nil, err + } + var selected *model.DownloadClient + for i := range rows { + if rows[i].Type != "qbittorrent" { + continue + } + if selected != nil { + return nil, nil + } + row := rows[i] + selected = &row + } + return selected, nil +}