mirror of
https://github.com/truewhile/MeBox.git
synced 2026-09-29 11:36:36 +08:00
778bf77437
Complete the port of the remaining MediaStation features:
Backend services:
- service/backup.go: SQLite hot backup via VACUUM INTO, list, delete,
restore. Backup files stored under {data_dir}/backups/ with timestamps.
- service/notifier.go: Multi-channel push notifications (Telegram /
Bark / WeChat Server酱 / Webhook). Configuration via settings table
keys notify.telegram.*, notify.bark.*, etc.
- service/organizer.go: Auto-rename + move media files into library
directory structure. Movie: {Title} ({Year})/{Title} ({Year}).ext,
TV: {Title}/Season XX/{Title} - SxxExx.ext. Both per-media and
per-library batch endpoints.
- service/douban.go: Douban (豆瓣) metadata provider using the
unofficial subject_suggest API. Returns Chinese titles + posters.
Requires douban_cookie in secrets config.
Handlers:
- handler/backup.go: GET/POST/DELETE /admin/backups, POST restore.
- handler/organizer.go: POST /admin/media/:id/organize,
POST /admin/libraries/:id/organize.
- handler/notify.go: POST /admin/notify/test.
Frontend pages:
- WatchHistoryPage.tsx: full paginated history with progress bars,
resume buttons, poster thumbnails and timestamps.
- PosterWallPage.tsx: dense 8-column poster grid of all media
(matches the original MediaStation PosterWallView.vue).
Sidebar additions: 观看历史 + 海报墙 links in the main nav group.
Verified: go build, go vet, go test all pass; tsc -b && vite build
emits 30 lazy chunks; main bundle 254 KB / 85 KB gzipped.
181 lines
5.4 KiB
Go
181 lines
5.4 KiB
Go
// Package service contains the business logic of MediaStationGo. Handlers
|
|
// deserialize the HTTP request, call into a Service method, then serialize
|
|
// the response. Services own all cross-cutting policy (auth, scanning,
|
|
// transcoding, etc.) and never deal with HTTP types directly.
|
|
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"github.com/ShukeBta/MediaStationGo/internal/config"
|
|
"github.com/ShukeBta/MediaStationGo/internal/repository"
|
|
)
|
|
|
|
// Container holds every service initialized at startup. Handlers receive a
|
|
// pointer to it and pick the relevant fields.
|
|
type Container struct {
|
|
Cfg *config.Config
|
|
Log *zap.Logger
|
|
Repo *repository.Container
|
|
WSHub *Hub
|
|
Auth *AuthService
|
|
Media *MediaService
|
|
Scan *ScannerService
|
|
Stream *StreamService
|
|
Transcoder *TranscoderService
|
|
FFprobe *FFprobeService
|
|
TMDb *TMDbProvider
|
|
Bangumi *BangumiProvider
|
|
TheTVDB *TheTVDBProvider
|
|
Fanart *FanartProvider
|
|
Scraper *ScraperService
|
|
Discover *DiscoverService
|
|
Playback *PlaybackService
|
|
ImageProxy *ImageProxy
|
|
Watcher *WatcherService
|
|
Downloads *DownloadService
|
|
Subscription *SubscriptionService
|
|
Subtitle *SubtitleService
|
|
Stats *StatsService
|
|
Profile *ProfileService
|
|
Audit *AuditService
|
|
NFO *NFOService
|
|
AI *AIService
|
|
APIConfig *APIConfigService
|
|
Crypto *CryptoService
|
|
Duplicate *DuplicateService
|
|
FileManager *FileManagerService
|
|
DLNA *DLNAService
|
|
Scheduler *SchedulerService
|
|
Storage *StorageService
|
|
Emby *EmbyService
|
|
Backup *BackupService
|
|
Notifier *NotifierService
|
|
Organizer *OrganizerService
|
|
Douban *DoubanProvider
|
|
|
|
stopCtx context.Context
|
|
stopCancel context.CancelFunc
|
|
}
|
|
|
|
// New builds the service container.
|
|
func New(cfg *config.Config, log *zap.Logger, repos *repository.Container) *Container {
|
|
hub := NewHub(log)
|
|
go hub.Run()
|
|
|
|
probe := NewFFprobeService(cfg, log)
|
|
tmdb := NewTMDbProvider(cfg, log)
|
|
bangumi := NewBangumiProvider(cfg, log)
|
|
thetvdb := NewTheTVDBProvider(cfg, log)
|
|
fanart := NewFanartProvider(cfg, log)
|
|
scraper := NewScraperService(cfg, log, repos, tmdb, bangumi, thetvdb, fanart, hub)
|
|
discover := NewDiscoverService(log, tmdb)
|
|
transcoder := NewTranscoderService(cfg, log, repos, hub)
|
|
scanner := NewScannerService(cfg, log, repos, hub, probe, scraper)
|
|
downloads := NewDownloadService(log, repos, hub)
|
|
subscription := NewSubscriptionService(log, repos, downloads, hub)
|
|
watcher := NewWatcherService(log, repos, scanner)
|
|
nfo := NewNFOService(log, repos)
|
|
ai := NewAIService(cfg, log)
|
|
crypto := NewCryptoService(cfg.Secrets.JWTSecret, log)
|
|
apiConfig := NewAPIConfigService(log, repos, crypto)
|
|
duplicate := NewDuplicateService(log, repos, hub)
|
|
filemanager := NewFileManagerService(cfg, log, repos)
|
|
dlna := NewDLNAService(log)
|
|
storage := NewStorageService(log, repos)
|
|
emby := NewEmbyService(cfg, log, repos)
|
|
backup := NewBackupService(cfg, log, repos.DB)
|
|
notifier := NewNotifierService(log, repos)
|
|
organizer := NewOrganizerService(cfg, log, repos)
|
|
douban := NewDoubanProvider(cfg, log)
|
|
scheduler := NewSchedulerService(log, repos, scanner, transcoder, hub, cfg.Cache.CacheDir)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
return &Container{
|
|
Cfg: cfg,
|
|
Log: log,
|
|
Repo: repos,
|
|
WSHub: hub,
|
|
Auth: NewAuthService(cfg, log, repos),
|
|
Media: NewMediaService(cfg, log, repos),
|
|
Scan: scanner,
|
|
Stream: NewStreamService(cfg, log, repos, transcoder),
|
|
Transcoder: transcoder,
|
|
FFprobe: probe,
|
|
TMDb: tmdb,
|
|
Bangumi: bangumi,
|
|
TheTVDB: thetvdb,
|
|
Fanart: fanart,
|
|
Scraper: scraper,
|
|
Discover: discover,
|
|
Playback: NewPlaybackService(log, repos),
|
|
ImageProxy: NewImageProxy(cfg, log),
|
|
Watcher: watcher,
|
|
Downloads: downloads,
|
|
Subscription: subscription,
|
|
Subtitle: NewSubtitleService(log, repos),
|
|
Stats: NewStatsService(log, repos),
|
|
Profile: NewProfileService(log, repos),
|
|
Audit: NewAuditService(log, repos),
|
|
NFO: nfo,
|
|
AI: ai,
|
|
APIConfig: apiConfig,
|
|
Crypto: crypto,
|
|
Duplicate: duplicate,
|
|
FileManager: filemanager,
|
|
DLNA: dlna,
|
|
Scheduler: scheduler,
|
|
Storage: storage,
|
|
Emby: emby,
|
|
Backup: backup,
|
|
Notifier: notifier,
|
|
Organizer: organizer,
|
|
Douban: douban,
|
|
stopCtx: ctx,
|
|
stopCancel: cancel,
|
|
}
|
|
}
|
|
|
|
// Boot kicks off background workers (watcher, downloads poller,
|
|
// subscription scheduler). Called once after AutoMigrate.
|
|
func (c *Container) Boot() {
|
|
if err := c.Watcher.Start(c.stopCtx); err != nil {
|
|
c.Log.Warn("watcher start failed", zap.Error(err))
|
|
}
|
|
c.Downloads.Start(c.stopCtx)
|
|
c.Subscription.Start(c.stopCtx)
|
|
if err := c.APIConfig.SeedDefaults(c.stopCtx); err != nil {
|
|
c.Log.Warn("api config seed failed", zap.Error(err))
|
|
}
|
|
c.Scheduler.Start(c.stopCtx)
|
|
}
|
|
|
|
// Close releases any resources held by services (websocket hub, ffmpeg
|
|
// transcodes, fsnotify, background pollers).
|
|
func (c *Container) Close() {
|
|
if c.stopCancel != nil {
|
|
c.stopCancel()
|
|
}
|
|
if c.Scheduler != nil {
|
|
c.Scheduler.Stop()
|
|
}
|
|
if c.Watcher != nil {
|
|
c.Watcher.Stop()
|
|
}
|
|
if c.Subscription != nil {
|
|
c.Subscription.Stop()
|
|
}
|
|
if c.Downloads != nil {
|
|
c.Downloads.Stop()
|
|
}
|
|
if c.Transcoder != nil {
|
|
c.Transcoder.StopAll()
|
|
}
|
|
if c.WSHub != nil {
|
|
c.WSHub.Stop()
|
|
}
|
|
}
|