mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-03 04:26:36 +08:00
7095d9ebec
Backend additions:
- New GORM models: NotifyChannel, PlayProfile (with PIN hashing).
- NotifyChannelService: multi-channel CRUD + unified dispatcher
supporting Telegram / Bark / WeChat / Webhook with optional
per-channel event filtering.
- PlayProfileService: per-user 'viewing personas' with
content-rating gates, library scoping, PIN protection, and
player defaults (autoplay, skip-intro, audio/subtitle prefs).
- New endpoints:
GET /admin/notify/channels list
POST /admin/notify/channels create
PUT /admin/notify/channels/:id update
DELETE /admin/notify/channels/:id delete
POST /admin/notify/channels/:id/test send test
GET /play-profiles list (admin: ?all=true)
POST /play-profiles create
PUT /play-profiles/:id update
DELETE /play-profiles/:id delete
GET /media/recent home page rail
GET /media/stats library composition
GET /watch-history list
GET /watch-history/stats aggregate
GET /watch-history/continue continue-watching rail
DELETE /watch-history[?media_id=] clear
DELETE /watch-history/:id remove one
GET /discover/sections available rails
GET /discover/feed?sections=a,b multi-section TMDb fetch
GET /system/info name/version/runtime
GET /system/status uptime/cpu/mem/disk
GET /system/scheduler read-only scheduler view
GET /stats/overview counts only
GET /stats/trend daily plays
GET /stats/top-content most played
GET /stats/libraries per-library size
GET /stats/monitor live hardware
Frontend additions:
- New API helpers: notify_channels, play_profiles, history,
stats_extra, media_extra, discover_extra, system.
- ProfileManagementPage (/play-profiles): full CRUD form with
PIN management, library scoping, language preferences.
- NotifyChannelsPage (/notify-channels, admin): typed config
forms per channel type + per-channel test action.
- SettingsPage (/settings, admin): grouped key/value editor
covering General, Organize/Scrape, Adult, qBittorrent.
- Layout sidebar links and App.tsx routes wired for all three.
190 lines
5.7 KiB
Go
190 lines
5.7 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
|
|
NotifyChannels *NotifyChannelService
|
|
PlayProfiles *PlayProfileService
|
|
Organizer *OrganizerService
|
|
Douban *DoubanProvider
|
|
Site *SiteService
|
|
|
|
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)
|
|
notifyChannels := NewNotifyChannelService(log, repos)
|
|
playProfiles := NewPlayProfileService(log, repos)
|
|
organizer := NewOrganizerService(cfg, log, repos)
|
|
douban := NewDoubanProvider(cfg, log)
|
|
siteService := NewSiteService(log, repos)
|
|
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,
|
|
NotifyChannels: notifyChannels,
|
|
PlayProfiles: playProfiles,
|
|
Organizer: organizer,
|
|
Douban: douban,
|
|
Site: siteService,
|
|
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()
|
|
}
|
|
}
|