mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-03 12:26:36 +08:00
feat: add STRM output directory presets
This commit is contained in:
@@ -52,6 +52,7 @@ func registerAuthedDLNARoutes(authed *gin.RouterGroup, svc *service.Container) {
|
||||
func registerAuthedSTRMRoutes(authed *gin.RouterGroup, svc *service.Container) {
|
||||
authed.PUT("/media/:id/strm", middleware.AdminRequired(), setSTRMHandler(svc))
|
||||
authed.DELETE("/media/:id/strm", middleware.AdminRequired(), clearSTRMHandler(svc))
|
||||
authed.GET("/strm/output-presets", middleware.AdminRequired(), listSTRMOutputPresetsHandler(svc))
|
||||
authed.POST("/strm/import", middleware.AdminRequired(), importSTRMHandler(svc))
|
||||
authed.POST("/strm/generate", middleware.AdminRequired(), generateSTRMHandler(svc))
|
||||
authed.POST("/strm/generate-from-tree", middleware.AdminRequired(), generateSTRMFromTreeHandler(svc))
|
||||
|
||||
@@ -134,6 +134,17 @@ type repairSTRMReq struct {
|
||||
RefreshLibrary bool `json:"refresh_library"`
|
||||
}
|
||||
|
||||
func listSTRMOutputPresetsHandler(svc *service.Container) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
items, err := service.STRMOutputPresets(c.Request.Context(), svc.Repo)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"items": items})
|
||||
}
|
||||
}
|
||||
|
||||
func generateSTRMHandler(svc *service.Container) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
var req generateSTRMReq
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/ShukeBta/MediaStationGo/internal/model"
|
||||
"github.com/ShukeBta/MediaStationGo/internal/repository"
|
||||
)
|
||||
|
||||
type STRMOutputPreset struct {
|
||||
Label string `json:"label"`
|
||||
Path string `json:"path"`
|
||||
Kind string `json:"kind"`
|
||||
}
|
||||
|
||||
func STRMOutputPresets(ctx context.Context, repo *repository.Container) ([]STRMOutputPreset, error) {
|
||||
presets := defaultSTRMOutputPresets()
|
||||
seen := make(map[string]struct{}, len(presets))
|
||||
for _, preset := range presets {
|
||||
seen[strmOutputPresetKey(preset.Path)] = struct{}{}
|
||||
}
|
||||
if repo == nil || repo.Library == nil {
|
||||
return presets, nil
|
||||
}
|
||||
libraries, err := repo.Library.List(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
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 {
|
||||
roots = []model.LibraryRoot{{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
|
||||
}
|
||||
pathValue := filepath.Clean(resolveMappedDestinationPath(root.Path))
|
||||
if pathValue == "" || pathValue == "." {
|
||||
continue
|
||||
}
|
||||
key := strmOutputPresetKey(pathValue)
|
||||
if _, ok := seen[key]; ok {
|
||||
continue
|
||||
}
|
||||
seen[key] = struct{}{}
|
||||
presets = append(presets, STRMOutputPreset{
|
||||
Label: lib.Name,
|
||||
Path: pathValue,
|
||||
Kind: "library",
|
||||
})
|
||||
}
|
||||
}
|
||||
return presets, nil
|
||||
}
|
||||
|
||||
func defaultSTRMOutputPresets() []STRMOutputPreset {
|
||||
return []STRMOutputPreset{
|
||||
{Label: "STRM 根目录", Path: filepath.Clean("data/strm"), Kind: "default"},
|
||||
{Label: "目录树 STRM", Path: filepath.Clean("data/strm/tree"), Kind: "default"},
|
||||
}
|
||||
}
|
||||
|
||||
func strmOutputPresetKey(pathValue string) string {
|
||||
return strings.ToLower(filepath.Clean(strings.TrimSpace(pathValue)))
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/ShukeBta/MediaStationGo/internal/model"
|
||||
"github.com/ShukeBta/MediaStationGo/internal/repository"
|
||||
)
|
||||
|
||||
func TestSTRMOutputPresetsIncludesDefaultsAndLocalLibraries(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", "电视剧")
|
||||
cloudRoot := BuildCloudLibraryPath("openlist", "/电影", "/电影")
|
||||
disabledRoot := filepath.Join(base, "strm", "动漫")
|
||||
|
||||
libraries := []*model.Library{
|
||||
{Name: "电影 STRM", Path: movieRoot, Type: "movie", Enabled: true},
|
||||
{Name: "电视剧 STRM", Path: tvRoot, Type: "tv", Enabled: true},
|
||||
{Name: "云盘", Path: cloudRoot, Type: "movie", Enabled: true},
|
||||
{Name: "停用", Path: disabledRoot, Type: "tv", Enabled: false},
|
||||
}
|
||||
for _, lib := range libraries {
|
||||
if err := repos.Library.Create(t.Context(), lib); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := repos.DB.Model(&model.Library{}).Where("id = ?", libraries[3].ID).Update("enabled", false).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
presets, err := STRMOutputPresets(t.Context(), repos)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := map[string]STRMOutputPreset{}
|
||||
for _, preset := range presets {
|
||||
got[preset.Path] = preset
|
||||
}
|
||||
|
||||
if got[filepath.Clean("data/strm")].Kind != "default" || got[filepath.Clean("data/strm/tree")].Kind != "default" {
|
||||
t.Fatalf("defaults missing from presets: %#v", presets)
|
||||
}
|
||||
if got[movieRoot].Label != "电影 STRM" || got[movieRoot].Kind != "library" {
|
||||
t.Fatalf("movie preset = %#v, want local library preset", got[movieRoot])
|
||||
}
|
||||
if got[tvRoot].Label != "电视剧 STRM" || got[tvRoot].Kind != "library" {
|
||||
t.Fatalf("tv preset = %#v, want local library preset", got[tvRoot])
|
||||
}
|
||||
if _, ok := got[cloudRoot]; ok {
|
||||
t.Fatalf("cloud library should not be an output preset: %#v", presets)
|
||||
}
|
||||
if _, ok := got[disabledRoot]; ok {
|
||||
t.Fatalf("disabled library should not be an output preset: %#v", presets)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSTRMOutputPresetsDeduplicatesLibraryRoots(t *testing.T) {
|
||||
db := newServiceTestDB(t, &model.Library{}, &model.LibraryRoot{})
|
||||
repos := repository.New(db)
|
||||
root := filepath.Join(t.TempDir(), "strm")
|
||||
lib := model.Library{Name: "STRM", Path: root, Type: "movie", Enabled: true}
|
||||
if err := repos.Library.CreateWithRoots(t.Context(), &lib, []model.LibraryRoot{
|
||||
{Path: root, Enabled: true},
|
||||
{Path: root, Enabled: true},
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
presets, err := STRMOutputPresets(t.Context(), repos)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
count := 0
|
||||
for _, preset := range presets {
|
||||
if preset.Path == root {
|
||||
count++
|
||||
}
|
||||
}
|
||||
if count != 1 {
|
||||
t.Fatalf("root preset count = %d, presets=%#v", count, presets)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user