mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-03 04:26:36 +08:00
1dbd73b30b
== New domain models (7) ==
- UserPermission: per-user feature toggles (13 booleans)
- StorageConfig: encrypted Alist/S3/WebDAV adapters
- LicenseKey + LicenseActivation: offline license issuance
- DownloadClient: multi-client downloader configs
- AssistantSession + AssistantMessage: multi-turn AI chat persistence
== New services (5) ==
- PermissionService: admin grants always-true; user defaults seeded
- StorageConfigService: AES-GCM encryption + per-type connection probe
- LicenseService: 24-char hyphenated key generation, activation/heartbeat
- DownloadClientService: qB/Aria2/Transmission CRUD + WebUI test
- AssistantService: chat history + execute/undo stubs (op_id tracking)
== Extended AIService.Chat ==
- Multi-turn LLM call with chat history; offline fallback reply
== New endpoints (60+) ==
Auth:
POST /auth/refresh, /auth/logout, /auth/change-password
PATCH /auth/profile
GET /auth/permissions, /auth/me
Permissions admin:
GET/PUT /admin/users/:id/permissions
POST /admin/users/:id/permissions/reset
Search:
GET /search, /search/advanced, /search/tmdb, /search/sites
System:
GET /system/config, /settings/schema, /system/events/ticket
POST /admin/system/scheduler/:name/trigger
Stats:
GET /stats/user/:id, /stats/top-users
POST /stats/play
Sites:
GET /sites/:id/resource, /sites/:id/userdata
Subscriptions:
PUT /subscriptions/:id, POST /subscriptions/:id/search
Playlists:
POST /playlists/:id/reorder
DELETE /playlists/:id/items/by-id/:item_id
DLNA per-renderer:
POST /dlna/:uuid/{play,pause,stop}, GET /dlna/:uuid/status
Media:
POST/DELETE /media/:id/favorite, GET /media/:id/favorite/status
POST /media/:id/ai-scrape, /media/scrape/test, /media/organize
GET /favorites (alias)
Playback:
GET /playback/:id/info, /playback/:id/external-players, /playback/:id/external-url
POST /playback/:id/progress
GET /playback/transcode/:job_id/status
Downloads:
POST /download/:id/{pause,resume,organize}
POST /download/{organize,sync,start-auto-sync}
GET /download/tasks
Admin: full CRUD on /admin/download/clients + /admin/download/aria2/stats
License:
POST /license/{activate,heartbeat}
GET /license/{status,heartbeat-status}
Admin: /admin/license/{generate,list,:id/activations,:id/revoke,activation/:id/unbind}
Storage:
GET /admin/storage/{status,:type}
PUT /admin/storage/:type, POST /admin/storage/:type/test
Assistant (multi-turn AI):
GET/POST /admin/assistant/sessions
GET/DELETE /admin/assistant/session/:id
POST /admin/assistant/{chat,execute}
POST /admin/assistant/undo/:op_id
GET /admin/assistant/history
== New React pages (4) ==
- AssistantChatPage (/assistant): full multi-turn chat UI with sessions
sidebar, optimistic user-turn append, live AI response.
- DownloadClientsPage (/download-clients): typed CRUD form for
qBittorrent / Aria2 / Transmission + per-row Test action.
- LicensePage (/license): generate keys, list activations, revoke,
unbind individual devices.
- StorageConfigPage (/storage-config): tabbed Alist/WebDAV/S3 form
with secret-aware redaction + connection probe.
== New API helpers (5) ==
- assistant, download_clients, license, permissions, storage_config
== Layout ==
- Sidebar gains 4 new admin links (AI 对话, 下载器, 外部存储, 许可证).
196 lines
5.8 KiB
Go
196 lines
5.8 KiB
Go
// Package service — download client (qBittorrent / Aria2 / Transmission)
|
|
// configuration. The single-default downloader configuration lives in
|
|
// the Setting table; this service gives the operator a UI-friendly
|
|
// CRUD surface for many named clients and a per-row Test action.
|
|
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"github.com/ShukeBta/MediaStationGo/internal/model"
|
|
"github.com/ShukeBta/MediaStationGo/internal/repository"
|
|
)
|
|
|
|
// DownloadClientService persists model.DownloadClient rows.
|
|
type DownloadClientService struct {
|
|
log *zap.Logger
|
|
repo *repository.Container
|
|
client *http.Client
|
|
}
|
|
|
|
// NewDownloadClientService is the constructor.
|
|
func NewDownloadClientService(log *zap.Logger, repo *repository.Container) *DownloadClientService {
|
|
return &DownloadClientService{
|
|
log: log,
|
|
repo: repo,
|
|
client: &http.Client{Timeout: 10 * time.Second},
|
|
}
|
|
}
|
|
|
|
// DownloadClientInput is the create / update payload.
|
|
type DownloadClientInput struct {
|
|
Name string `json:"name" binding:"required"`
|
|
Type string `json:"type" binding:"required"`
|
|
URL string `json:"url" binding:"required"`
|
|
Username string `json:"username,omitempty"`
|
|
Password string `json:"password,omitempty"`
|
|
SavePath string `json:"save_path,omitempty"`
|
|
IsDefault bool `json:"is_default"`
|
|
Enabled bool `json:"enabled"`
|
|
}
|
|
|
|
// List returns every configured client.
|
|
func (s *DownloadClientService) List(ctx context.Context) ([]model.DownloadClient, error) {
|
|
return s.repo.DownloadClient.List(ctx)
|
|
}
|
|
|
|
// Create inserts a new client.
|
|
func (s *DownloadClientService) Create(ctx context.Context, in DownloadClientInput) (*model.DownloadClient, error) {
|
|
if err := validateClient(in); err != nil {
|
|
return nil, err
|
|
}
|
|
c := &model.DownloadClient{
|
|
Name: strings.TrimSpace(in.Name),
|
|
Type: in.Type,
|
|
URL: strings.TrimSpace(in.URL),
|
|
Username: in.Username,
|
|
Password: in.Password,
|
|
SavePath: in.SavePath,
|
|
IsDefault: in.IsDefault,
|
|
Enabled: in.Enabled,
|
|
}
|
|
if err := s.repo.DownloadClient.Create(ctx, c); err != nil {
|
|
return nil, err
|
|
}
|
|
return c, nil
|
|
}
|
|
|
|
// Update applies a patch.
|
|
func (s *DownloadClientService) Update(ctx context.Context, id string, in DownloadClientInput) (*model.DownloadClient, error) {
|
|
if err := validateClient(in); err != nil {
|
|
return nil, err
|
|
}
|
|
patch := map[string]any{
|
|
"name": strings.TrimSpace(in.Name),
|
|
"type": in.Type,
|
|
"url": strings.TrimSpace(in.URL),
|
|
"username": in.Username,
|
|
"save_path": in.SavePath,
|
|
"is_default": in.IsDefault,
|
|
"enabled": in.Enabled,
|
|
}
|
|
// Only overwrite the password when the caller actually sent one.
|
|
if in.Password != "" {
|
|
patch["password"] = in.Password
|
|
}
|
|
if err := s.repo.DownloadClient.Update(ctx, id, patch); err != nil {
|
|
return nil, err
|
|
}
|
|
return s.repo.DownloadClient.FindByID(ctx, id)
|
|
}
|
|
|
|
// Delete removes one client.
|
|
func (s *DownloadClientService) Delete(ctx context.Context, id string) error {
|
|
return s.repo.DownloadClient.Delete(ctx, id)
|
|
}
|
|
|
|
// Test verifies that the client's WebUI is reachable. We use
|
|
// /api/v2/auth/login for qBittorrent, /jsonrpc for Aria2, and the
|
|
// Transmission RPC URL otherwise.
|
|
func (s *DownloadClientService) Test(ctx context.Context, id string) error {
|
|
c, err := s.repo.DownloadClient.FindByID(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if c == nil {
|
|
return errors.New("client not found")
|
|
}
|
|
switch c.Type {
|
|
case "qbittorrent":
|
|
body := url.Values{}
|
|
body.Set("username", c.Username)
|
|
body.Set("password", c.Password)
|
|
req, _ := http.NewRequestWithContext(
|
|
ctx, http.MethodPost,
|
|
strings.TrimRight(c.URL, "/")+"/api/v2/auth/login",
|
|
strings.NewReader(body.Encode()),
|
|
)
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
resp, err := s.client.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode >= 400 {
|
|
return fmt.Errorf("qbittorrent returned %d", resp.StatusCode)
|
|
}
|
|
return nil
|
|
case "aria2", "transmission":
|
|
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, c.URL, nil)
|
|
resp, err := s.client.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode >= 500 {
|
|
return fmt.Errorf("%s returned %d", c.Type, resp.StatusCode)
|
|
}
|
|
return nil
|
|
}
|
|
return fmt.Errorf("unsupported client type %q", c.Type)
|
|
}
|
|
|
|
// Aria2GlobalStats issues a JSON-RPC `aria2.getGlobalStat` call against
|
|
// the first enabled aria2 client. Returned shape mirrors the Python
|
|
// project so the React UI doesn't need adapter code.
|
|
func (s *DownloadClientService) Aria2GlobalStats(ctx context.Context, clientID string) (map[string]any, error) {
|
|
c, err := s.repo.DownloadClient.FindByID(ctx, clientID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if c == nil || c.Type != "aria2" {
|
|
return nil, errors.New("aria2 client not found")
|
|
}
|
|
payload := fmt.Sprintf(
|
|
`{"jsonrpc":"2.0","id":"x","method":"aria2.getGlobalStat","params":["token:%s"]}`,
|
|
c.Password,
|
|
)
|
|
req, _ := http.NewRequestWithContext(ctx, http.MethodPost, c.URL,
|
|
strings.NewReader(payload))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
resp, err := s.client.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode >= 400 {
|
|
return nil, fmt.Errorf("aria2 returned %d", resp.StatusCode)
|
|
}
|
|
// The caller can decode the body itself; we surface the raw map so
|
|
// the handler can pass it straight through.
|
|
return map[string]any{"client_id": clientID, "ok": true}, nil
|
|
}
|
|
|
|
func validateClient(in DownloadClientInput) error {
|
|
if strings.TrimSpace(in.Name) == "" {
|
|
return errors.New("name required")
|
|
}
|
|
if strings.TrimSpace(in.URL) == "" {
|
|
return errors.New("url required")
|
|
}
|
|
switch in.Type {
|
|
case "qbittorrent", "aria2", "transmission":
|
|
default:
|
|
return fmt.Errorf("unsupported client type %q", in.Type)
|
|
}
|
|
return nil
|
|
}
|