mirror of
https://github.com/truewhile/MeBox.git
synced 2026-09-29 19:36: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)
|
||||
}
|
||||
}
|
||||
@@ -88,10 +88,18 @@ export type STRMRefreshResult = {
|
||||
}>
|
||||
}
|
||||
|
||||
export type STRMOutputPreset = {
|
||||
label: string
|
||||
path: string
|
||||
kind: 'default' | 'library' | string
|
||||
}
|
||||
|
||||
export const strmAPI = {
|
||||
set: (mediaID: string, url: string) =>
|
||||
api.put(`/media/${mediaID}/strm`, { url }).then((r) => r.data),
|
||||
clear: (mediaID: string) => api.delete(`/media/${mediaID}/strm`).then((r) => r.data),
|
||||
outputPresets: () =>
|
||||
api.get<{ items: STRMOutputPreset[] }>('/strm/output-presets').then((r) => r.data.items),
|
||||
importURL: (libraryID: string, title: string, url: string) =>
|
||||
api.post('/strm/import', { library_id: libraryID, title, url }).then((r) => r.data),
|
||||
generate: (input: GenerateSTRMInput) =>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { FormEvent } from 'react'
|
||||
|
||||
import type { GenerateSTRMResult } from '../api/strm'
|
||||
import type { GenerateSTRMResult, STRMOutputPreset } from '../api/strm'
|
||||
import type { Library } from '../types'
|
||||
import type { CloudPlaybackMode } from './strmPageModel'
|
||||
import {
|
||||
@@ -17,6 +17,7 @@ export type StrmGenerateSectionProps = {
|
||||
generateLibraryID: string
|
||||
baseURL: string
|
||||
outputDir: string
|
||||
outputPresets: STRMOutputPreset[]
|
||||
cloudPlaybackMode: CloudPlaybackMode
|
||||
strmPlaybackEnabled: boolean
|
||||
redirectProxyEnabled: boolean
|
||||
|
||||
@@ -4,6 +4,7 @@ import type { GenerateSTRMResult } from '../api/strm'
|
||||
import type { Library } from '../types'
|
||||
import { currentOrigin, type CloudPlaybackMode } from './strmPageModel'
|
||||
import type { StrmGenerateSectionProps } from './StrmGenerateSection'
|
||||
import { StrmOutputDirPicker } from './StrmOutputDirPicker'
|
||||
|
||||
type PlaybackStatusProps = Pick<
|
||||
StrmGenerateSectionProps,
|
||||
@@ -141,6 +142,7 @@ type StrmGenerateFormProps = Pick<
|
||||
| 'generateLibraryID'
|
||||
| 'baseURL'
|
||||
| 'outputDir'
|
||||
| 'outputPresets'
|
||||
| 'overwrite'
|
||||
| 'includeLocal'
|
||||
| 'preserveTree'
|
||||
@@ -161,6 +163,7 @@ export function StrmGenerateForm({
|
||||
generateLibraryID,
|
||||
baseURL,
|
||||
outputDir,
|
||||
outputPresets,
|
||||
overwrite,
|
||||
includeLocal,
|
||||
preserveTree,
|
||||
@@ -198,11 +201,12 @@ export function StrmGenerateForm({
|
||||
<CompactOption checked={preserveTree} label="保留目录树" onChange={setPreserveTree} />
|
||||
<CompactOption checked={refreshLibrary} label="生成后刷新媒体库" onChange={setRefreshLibrary} />
|
||||
</div>
|
||||
<input
|
||||
className="input-base md:col-span-4"
|
||||
<StrmOutputDirPicker
|
||||
className="md:col-span-4"
|
||||
presets={outputPresets}
|
||||
placeholder="输出目录可留空,默认写入 data/strm/分类/子分类"
|
||||
value={outputDir}
|
||||
onChange={(e) => setOutputDir(e.target.value)}
|
||||
onChange={setOutputDir}
|
||||
/>
|
||||
<button type="submit" disabled={generating || !generateLibraryID || !baseURL.trim()} className="neon-button md:col-span-4">
|
||||
{generating ? <Loader2 size={16} className="animate-spin" /> : <Wand2 size={16} />}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
import type { STRMOutputPreset } from '../api/strm'
|
||||
|
||||
type StrmOutputDirPickerProps = {
|
||||
className?: string
|
||||
inputClassName?: string
|
||||
presets: STRMOutputPreset[]
|
||||
placeholder: string
|
||||
value: string
|
||||
required?: boolean
|
||||
onChange: (value: string) => void
|
||||
}
|
||||
|
||||
export function StrmOutputDirPicker({
|
||||
className = '',
|
||||
inputClassName = '',
|
||||
presets,
|
||||
placeholder,
|
||||
value,
|
||||
required = false,
|
||||
onChange,
|
||||
}: StrmOutputDirPickerProps) {
|
||||
const normalizedValue = value.trim()
|
||||
const selectedPreset = presets.some((preset) => preset.path === normalizedValue) ? normalizedValue : ''
|
||||
|
||||
return (
|
||||
<div className={`grid gap-2 md:grid-cols-[minmax(0,1fr)_minmax(10rem,16rem)] ${className}`}>
|
||||
<input
|
||||
required={required}
|
||||
className={`input-base ${inputClassName}`}
|
||||
placeholder={placeholder}
|
||||
value={value}
|
||||
onChange={(e) => onChange(e.target.value)}
|
||||
/>
|
||||
<select
|
||||
className="input-base"
|
||||
value={selectedPreset}
|
||||
onChange={(e) => {
|
||||
if (e.target.value) onChange(e.target.value)
|
||||
}}
|
||||
>
|
||||
<option value="">选择输出目录</option>
|
||||
{presets.map((preset) => (
|
||||
<option key={`${preset.kind}:${preset.path}`} value={preset.path}>
|
||||
{preset.kind === 'library' ? `${preset.label} - ${preset.path}` : preset.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -24,6 +24,7 @@ export function StrmPage() {
|
||||
generateLibraryID={strm.generate.generateLibraryID}
|
||||
baseURL={strm.generate.baseURL}
|
||||
outputDir={strm.generate.outputDir}
|
||||
outputPresets={strm.outputPresets}
|
||||
cloudPlaybackMode={strm.generate.cloudPlaybackMode}
|
||||
strmPlaybackEnabled={strm.generate.strmPlaybackEnabled}
|
||||
redirectProxyEnabled={strm.generate.redirectProxyEnabled}
|
||||
@@ -51,9 +52,9 @@ export function StrmPage() {
|
||||
setRefreshLibrary={strm.generate.setRefreshLibrary}
|
||||
/>
|
||||
|
||||
<StrmTreeGenerateSection {...strm.treeGenerate} />
|
||||
<StrmTreeGenerateSection {...strm.treeGenerate} outputPresets={strm.outputPresets} />
|
||||
|
||||
<StrmRepairSection {...strm.repair} />
|
||||
<StrmRepairSection {...strm.repair} outputPresets={strm.outputPresets} />
|
||||
|
||||
<StrmImportSection
|
||||
libraries={strm.libraries}
|
||||
|
||||
@@ -1,13 +1,18 @@
|
||||
import { Loader2, RotateCcw, Wrench } from 'lucide-react'
|
||||
|
||||
import type { STRMOutputPreset } from '../api/strm'
|
||||
import { currentOrigin } from './strmPageModel'
|
||||
import { StrmOutputDirPicker } from './StrmOutputDirPicker'
|
||||
import type { useStrmRepairForm } from './useStrmRepairForm'
|
||||
|
||||
type StrmRepairSectionProps = ReturnType<typeof useStrmRepairForm>
|
||||
type StrmRepairSectionProps = ReturnType<typeof useStrmRepairForm> & {
|
||||
outputPresets: STRMOutputPreset[]
|
||||
}
|
||||
|
||||
export function StrmRepairSection({
|
||||
baseURL,
|
||||
outputDir,
|
||||
outputPresets,
|
||||
repairing,
|
||||
refreshLibrary,
|
||||
result,
|
||||
@@ -25,12 +30,13 @@ export function StrmRepairSection({
|
||||
<p className="text-sm text-ink-50">批量修复已存在 .strm 文件中的本服务播放地址。</p>
|
||||
</div>
|
||||
<form onSubmit={onRepair} className="grid gap-3 md:grid-cols-4">
|
||||
<input
|
||||
<StrmOutputDirPicker
|
||||
className="md:col-span-2"
|
||||
required
|
||||
className="input-base md:col-span-2"
|
||||
presets={outputPresets}
|
||||
placeholder="STRM 输出目录"
|
||||
value={outputDir}
|
||||
onChange={(e) => setOutputDir(e.target.value)}
|
||||
onChange={setOutputDir}
|
||||
/>
|
||||
<input
|
||||
className="input-base"
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
import { FolderTree, Loader2, Upload, X } from 'lucide-react'
|
||||
|
||||
import type { STRMOutputPreset } from '../api/strm'
|
||||
import { currentOrigin } from './strmPageModel'
|
||||
import { StrmGenerateResultPanel } from './StrmGenerateSectionParts'
|
||||
import { StrmOutputDirPicker } from './StrmOutputDirPicker'
|
||||
import type { useStrmTreeGenerateForm } from './useStrmTreeGenerateForm'
|
||||
|
||||
type StrmTreeGenerateSectionProps = ReturnType<typeof useStrmTreeGenerateForm>
|
||||
type StrmTreeGenerateSectionProps = ReturnType<typeof useStrmTreeGenerateForm> & {
|
||||
outputPresets: STRMOutputPreset[]
|
||||
}
|
||||
|
||||
const outputPrefixPresets = [
|
||||
'电影/演唱会',
|
||||
@@ -37,6 +41,7 @@ export function StrmTreeGenerateSection({
|
||||
onPreview,
|
||||
onImportTreeFile,
|
||||
outputDir,
|
||||
outputPresets,
|
||||
outputPrefix,
|
||||
overwrite,
|
||||
pathsText,
|
||||
@@ -83,12 +88,13 @@ export function StrmTreeGenerateSection({
|
||||
value={sourceRoot}
|
||||
onChange={(e) => setSourceRoot(e.target.value)}
|
||||
/>
|
||||
<input
|
||||
<StrmOutputDirPicker
|
||||
className="md:col-span-2"
|
||||
required
|
||||
className="input-base"
|
||||
presets={outputPresets}
|
||||
placeholder="输出目录"
|
||||
value={outputDir}
|
||||
onChange={(e) => setOutputDir(e.target.value)}
|
||||
onChange={setOutputDir}
|
||||
/>
|
||||
<select
|
||||
className="input-base"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
|
||||
import { libraryAPI } from '../api/library'
|
||||
import { strmAPI, type STRMOutputPreset } from '../api/strm'
|
||||
import type { Library } from '../types'
|
||||
import { useStrmAttachForm } from './useStrmAttachForm'
|
||||
import { useStrmGenerateForm } from './useStrmGenerateForm'
|
||||
@@ -10,6 +11,7 @@ import { useStrmTreeGenerateForm } from './useStrmTreeGenerateForm'
|
||||
|
||||
export function useStrmPage() {
|
||||
const [libraries, setLibraries] = useState<Library[]>([])
|
||||
const [outputPresets, setOutputPresets] = useState<STRMOutputPreset[]>([])
|
||||
const generate = useStrmGenerateForm(libraries)
|
||||
const treeGenerate = useStrmTreeGenerateForm()
|
||||
const repair = useStrmRepairForm()
|
||||
@@ -18,6 +20,7 @@ export function useStrmPage() {
|
||||
|
||||
useEffect(() => {
|
||||
libraryAPI.list().then(setLibraries).catch(() => undefined)
|
||||
strmAPI.outputPresets().then(setOutputPresets).catch(() => undefined)
|
||||
}, [])
|
||||
|
||||
return {
|
||||
@@ -25,6 +28,7 @@ export function useStrmPage() {
|
||||
generate,
|
||||
importForm,
|
||||
libraries,
|
||||
outputPresets,
|
||||
repair,
|
||||
treeGenerate,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user