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
+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 {