feat: refresh libraries after STRM updates

This commit is contained in:
ShukeBta
2026-07-02 23:20:08 +08:00
parent 2df7c34cf7
commit e700962f2a
14 changed files with 301 additions and 10 deletions
+77 -3
View File
@@ -7,6 +7,7 @@
package handler
import (
"context"
"net/http"
"strings"
@@ -106,6 +107,7 @@ type generateSTRMReq struct {
Overwrite bool `json:"overwrite"`
IncludeLocal *bool `json:"include_local"`
PreserveTree bool `json:"preserve_tree"`
Refresh bool `json:"refresh_library"`
}
type generateSTRMTreeReq struct {
@@ -121,12 +123,14 @@ type generateSTRMTreeReq struct {
DryRun bool `json:"dry_run"`
BatchLimit int `json:"batch_limit"`
TransferSubtitles bool `json:"transfer_subtitles"`
RefreshLibrary bool `json:"refresh_library"`
}
type repairSTRMReq struct {
OutputDir string `json:"output_dir" binding:"required"`
BaseURL string `json:"base_url"`
DryRun bool `json:"dry_run"`
OutputDir string `json:"output_dir" binding:"required"`
BaseURL string `json:"base_url"`
DryRun bool `json:"dry_run"`
RefreshLibrary bool `json:"refresh_library"`
}
func generateSTRMHandler(svc *service.Container) gin.HandlerFunc {
@@ -169,6 +173,9 @@ func generateSTRMHandler(svc *service.Container) gin.HandlerFunc {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if req.Refresh {
res.Refresh = queueSTRMRefreshAfterChanges(c.Request.Context(), svc, res.OutputDir, "STRM 生成后刷新媒体库", strmGenerationChanged(res))
}
c.JSON(http.StatusOK, res)
}
}
@@ -206,6 +213,9 @@ func generateSTRMFromTreeHandler(svc *service.Container) gin.HandlerFunc {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if req.RefreshLibrary && !req.DryRun {
res.Refresh = queueSTRMRefreshAfterChanges(c.Request.Context(), svc, res.OutputDir, "STRM 目录树生成后刷新媒体库", strmGenerationChanged(res))
}
c.JSON(http.StatusOK, res)
}
}
@@ -234,10 +244,74 @@ func repairSTRMHandler(svc *service.Container) gin.HandlerFunc {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if req.RefreshLibrary && !req.DryRun {
res.Refresh = queueSTRMRefreshAfterChanges(c.Request.Context(), svc, res.OutputDir, "STRM 修复后刷新媒体库", res.Repaired > 0)
}
c.JSON(http.StatusOK, res)
}
}
func strmGenerationChanged(res *service.GenerateSTRMResult) bool {
return res != nil && (res.Generated > 0 || res.Updated > 0 || res.Cleaned > 0)
}
func queueSTRMRefreshAfterChanges(ctx context.Context, svc *service.Container, outputDir, taskName string, changed bool) *service.STRMRefreshResult {
refresh := &service.STRMRefreshResult{Requested: true}
if !changed {
refresh.Reason = "no strm changes"
return refresh
}
if svc == nil || svc.Scan == nil {
refresh.Reason = "scanner unavailable"
return refresh
}
targets, err := service.FindSTRMRefreshTargets(ctx, svc.Repo, outputDir)
if err != nil {
refresh.Reason = err.Error()
return refresh
}
if len(targets) == 0 {
refresh.Reason = "no matching local library"
return refresh
}
refresh.Targets = targets
for _, target := range targets {
key := target.LibraryID
if target.RootID != "" {
key += ":" + target.RootID
}
finishScan, ok := svc.Scan.TryBeginLocalScan(key)
if !ok {
continue
}
refresh.Queued = true
task := startScanHTTPTask(svc, taskName, target.Name, target.Path)
go runSTRMRefreshScan(svc, target, task, finishScan)
}
if !refresh.Queued {
refresh.Reason = "matching library already scanning"
}
return refresh
}
func runSTRMRefreshScan(svc *service.Container, target service.STRMRefreshTarget, task *service.TaskHandle, finish func()) {
defer finish()
var (
res *service.ScanResult
err error
)
if target.RootID != "" {
res, err = svc.Scan.ScanLibraryRoot(context.Background(), target.LibraryID, target.RootID)
} else {
res, err = svc.Scan.ScanLibrary(context.Background(), target.LibraryID)
}
if err != nil {
finishHTTPTask(task, err, "scan", "STRM 刷新媒体库失败", scanTaskMetrics(res), scanTaskDetails(res, 20))
return
}
finishHTTPTask(task, nil, "completed", "STRM 刷新媒体库结束", scanTaskMetrics(res), scanTaskDetails(res, 20))
}
func strmPlaybackTokenForRequest(c *gin.Context, svc *service.Container) string {
if svc == nil || svc.Auth == nil || svc.Repo == nil || svc.Repo.User == nil {
return ""
+1
View File
@@ -41,6 +41,7 @@ type GenerateSTRMResult struct {
Errors []string `json:"errors,omitempty"`
IgnoredItems []string `json:"ignored_items,omitempty"`
Items []GenerateSTRMItem `json:"items,omitempty"`
Refresh *STRMRefreshResult `json:"refresh,omitempty"`
}
type GenerateSTRMItem struct {
+86
View File
@@ -0,0 +1,86 @@
package service
import (
"context"
"path/filepath"
"strings"
"github.com/ShukeBta/MediaStationGo/internal/model"
"github.com/ShukeBta/MediaStationGo/internal/repository"
)
type STRMRefreshResult struct {
Requested bool `json:"requested"`
Queued bool `json:"queued"`
Reason string `json:"reason,omitempty"`
Targets []STRMRefreshTarget `json:"targets,omitempty"`
}
type STRMRefreshTarget struct {
LibraryID string `json:"library_id"`
RootID string `json:"root_id,omitempty"`
Name string `json:"name"`
Path string `json:"path"`
}
func FindSTRMRefreshTargets(ctx context.Context, repo *repository.Container, outputDir string) ([]STRMRefreshTarget, error) {
if repo == nil || repo.Library == nil {
return nil, nil
}
outputDir = resolveMappedDestinationPath(strings.TrimSpace(outputDir))
if outputDir == "" || outputDir == "." {
return nil, nil
}
libraries, err := repo.Library.List(ctx)
if err != nil {
return nil, err
}
targets := make([]STRMRefreshTarget, 0)
seen := map[string]struct{}{}
for i := range libraries {
lib := libraries[i]
if !lib.Enabled {
continue
}
roots, err := repo.Library.ListRoots(ctx, lib.ID)
if err != nil {
return nil, err
}
if len(roots) == 0 && strings.TrimSpace(lib.Path) != "" {
roots = []model.LibraryRoot{{LibraryID: lib.ID, Path: lib.Path, Enabled: lib.Enabled}}
}
for j := range roots {
root := roots[j]
if !root.Enabled || strings.TrimSpace(root.Path) == "" {
continue
}
if _, ok := ParseCloudLibraryMount(root.Path); ok {
continue
}
if !strmRefreshPathMatches(outputDir, root.Path) {
continue
}
key := lib.ID + "\x00" + root.ID + "\x00" + strings.ToLower(filepath.Clean(root.Path))
if _, ok := seen[key]; ok {
continue
}
seen[key] = struct{}{}
targets = append(targets, STRMRefreshTarget{
LibraryID: lib.ID,
RootID: root.ID,
Name: lib.Name,
Path: filepath.Clean(root.Path),
})
}
}
return targets, nil
}
func strmRefreshPathMatches(outputDir, libraryRoot string) bool {
outputDir = filepath.Clean(strings.TrimSpace(outputDir))
libraryRoot = filepath.Clean(strings.TrimSpace(libraryRoot))
if outputDir == "" || outputDir == "." || libraryRoot == "" || libraryRoot == "." {
return false
}
return sameLibraryPath(outputDir, libraryRoot) || pathWithin(outputDir, libraryRoot) || pathWithin(libraryRoot, outputDir)
}
+65
View File
@@ -0,0 +1,65 @@
package service
import (
"path/filepath"
"testing"
"github.com/ShukeBta/MediaStationGo/internal/model"
"github.com/ShukeBta/MediaStationGo/internal/repository"
)
func TestFindSTRMRefreshTargetsMatchesNestedLocalRoots(t *testing.T) {
db := newServiceTestDB(t, &model.Library{}, &model.LibraryRoot{})
repos := repository.New(db)
base := t.TempDir()
movieRoot := filepath.Join(base, "strm", "电影")
tvRoot := filepath.Join(base, "strm", "电视剧")
otherRoot := filepath.Join(base, "other")
cloudRoot := "cloud://openlist/电影"
movie := model.Library{Name: "电影 STRM", Path: movieRoot, Type: "movie", Enabled: true}
tv := model.Library{Name: "电视剧 STRM", Path: tvRoot, Type: "tv", Enabled: true}
other := model.Library{Name: "其他", Path: otherRoot, Type: "movie", Enabled: true}
cloud := model.Library{Name: "云盘", Path: cloudRoot, Type: "movie", Enabled: true}
disabled := model.Library{Name: "停用", Path: filepath.Join(base, "strm", "动漫"), Type: "tv", Enabled: false}
for _, lib := range []*model.Library{&movie, &tv, &other, &cloud, &disabled} {
if err := repos.Library.Create(t.Context(), lib); err != nil {
t.Fatal(err)
}
}
if err := repos.DB.Model(&model.Library{}).Where("id = ?", disabled.ID).Update("enabled", false).Error; err != nil {
t.Fatal(err)
}
targets, err := FindSTRMRefreshTargets(t.Context(), repos, filepath.Join(base, "strm"))
if err != nil {
t.Fatal(err)
}
if len(targets) != 2 {
t.Fatalf("targets = %#v, want movie and tv only", targets)
}
got := map[string]bool{}
for _, target := range targets {
got[target.LibraryID] = true
}
if !got[movie.ID] || !got[tv.ID] || got[other.ID] || got[cloud.ID] || got[disabled.ID] {
t.Fatalf("target libraries = %#v", targets)
}
}
func TestFindSTRMRefreshTargetsDoesNotFallbackToAllLibraries(t *testing.T) {
db := newServiceTestDB(t, &model.Library{}, &model.LibraryRoot{})
repos := repository.New(db)
lib := model.Library{Name: "电影", Path: filepath.Join(t.TempDir(), "movies"), Type: "movie", Enabled: true}
if err := repos.Library.Create(t.Context(), &lib); err != nil {
t.Fatal(err)
}
targets, err := FindSTRMRefreshTargets(t.Context(), repos, filepath.Join(t.TempDir(), "strm"))
if err != nil {
t.Fatal(err)
}
if len(targets) != 0 {
t.Fatalf("targets = %#v, want no fallback target", targets)
}
}
+7 -6
View File
@@ -17,12 +17,13 @@ type RepairSTRMOptions struct {
}
type RepairSTRMResult struct {
OutputDir string `json:"output_dir"`
Repaired int `json:"repaired"`
Previewed int `json:"previewed,omitempty"`
Skipped int `json:"skipped"`
Errors []string `json:"errors,omitempty"`
Items []RepairSTRMItem `json:"items,omitempty"`
OutputDir string `json:"output_dir"`
Repaired int `json:"repaired"`
Previewed int `json:"previewed,omitempty"`
Skipped int `json:"skipped"`
Errors []string `json:"errors,omitempty"`
Items []RepairSTRMItem `json:"items,omitempty"`
Refresh *STRMRefreshResult `json:"refresh,omitempty"`
}
type RepairSTRMItem struct {