feat: add STRM repair workflow

This commit is contained in:
ShukeBta
2026-07-02 23:01:36 +08:00
parent e7d988ebf8
commit 2df7c34cf7
9 changed files with 428 additions and 0 deletions
+25
View File
@@ -49,6 +49,27 @@ export type GenerateSTRMTreeInput = {
transfer_subtitles?: boolean
}
export type RepairSTRMInput = {
output_dir: string
base_url?: string
dry_run?: boolean
}
export type RepairSTRMResult = {
output_dir: string
repaired: number
previewed?: number
skipped: number
errors?: string[]
items?: Array<{
file_path: string
before?: string
after?: string
action: string
reason?: string
}>
}
export const strmAPI = {
set: (mediaID: string, url: string) =>
api.put(`/media/${mediaID}/strm`, { url }).then((r) => r.data),
@@ -63,4 +84,8 @@ export const strmAPI = {
api
.post<GenerateSTRMResult>('/strm/generate-from-tree', input, { timeout: BATCH_REQUEST_TIMEOUT })
.then((r) => r.data),
repair: (input: RepairSTRMInput) =>
api
.post<RepairSTRMResult>('/strm/repair', input, { timeout: BATCH_REQUEST_TIMEOUT })
.then((r) => r.data),
}
+3
View File
@@ -2,6 +2,7 @@ import { StrmAttachSection } from './StrmAttachSection'
import { StrmGenerateSection } from './StrmGenerateSection'
import { StrmImportSection } from './StrmImportSection'
import { StrmPageHeader } from './StrmPageHeader'
import { StrmRepairSection } from './StrmRepairSection'
import { StrmTreeGenerateSection } from './StrmTreeGenerateSection'
import { useStrmPage } from './useStrmPage'
@@ -50,6 +51,8 @@ export function StrmPage() {
<StrmTreeGenerateSection {...strm.treeGenerate} />
<StrmRepairSection {...strm.repair} />
<StrmImportSection
libraries={strm.libraries}
libraryID={strm.importForm.libraryID}
+76
View File
@@ -0,0 +1,76 @@
import { Loader2, RotateCcw, Wrench } from 'lucide-react'
import { currentOrigin } from './strmPageModel'
import type { useStrmRepairForm } from './useStrmRepairForm'
type StrmRepairSectionProps = ReturnType<typeof useStrmRepairForm>
export function StrmRepairSection({
baseURL,
outputDir,
repairing,
result,
runningMode,
onPreview,
onRepair,
setBaseURL,
setOutputDir,
}: StrmRepairSectionProps) {
return (
<section className="glass-panel space-y-4">
<div>
<h2 className="font-display text-lg font-semibold text-ink-600">STRM 修复</h2>
<p className="text-sm text-ink-50">批量修复已存在 .strm 文件中的本服务播放地址。</p>
</div>
<form onSubmit={onRepair} className="grid gap-3 md:grid-cols-4">
<input
required
className="input-base md:col-span-2"
placeholder="STRM 输出目录"
value={outputDir}
onChange={(e) => setOutputDir(e.target.value)}
/>
<input
className="input-base"
placeholder="http://NAS-IP:18080 或 https://media.example.com"
value={baseURL}
onChange={(e) => setBaseURL(e.target.value)}
/>
<button
type="button"
className="rounded-2xl border border-primary-400/40 px-3 py-2 text-sm text-brand-500 transition hover:bg-primary-400/10"
onClick={() => setBaseURL(currentOrigin())}
>
使用当前访问地址
</button>
<button
type="button"
disabled={repairing || !outputDir.trim()}
className="inline-flex min-h-10 items-center justify-center gap-2 rounded-2xl border border-primary-400/40 px-3 py-2 text-sm font-medium text-brand-500 transition hover:bg-primary-400/10 disabled:cursor-not-allowed disabled:opacity-50 md:col-span-2"
onClick={onPreview}
>
{runningMode === 'preview' ? <Loader2 size={16} className="animate-spin" /> : <RotateCcw size={16} />}
{runningMode === 'preview' ? '预检中...' : '预检修复'}
</button>
<button type="submit" disabled={repairing || !outputDir.trim()} className="neon-button md:col-span-2">
{runningMode === 'repair' ? <Loader2 size={16} className="animate-spin" /> : <Wrench size={16} />}
{runningMode === 'repair' ? '修复中...' : '修复 STRM'}
</button>
</form>
{result && (
<div className="rounded-2xl border border-gray-200 bg-gray-50 p-4 text-sm text-ink-50">
<div className="font-semibold text-ink-600">输出目录:{result.output_dir}</div>
<div className="mt-1">
{result.previewed ? `预检 ${result.previewed} · ` : ''}
修复 {result.repaired} · 跳过 {result.skipped}
</div>
{result.errors && result.errors.length > 0 && (
<div className="mt-2 text-red-500">
失败 {result.errors.length} 条:{result.errors.slice(0, 3).join(';')}
</div>
)}
</div>
)}
</section>
)
}
+3
View File
@@ -5,12 +5,14 @@ import type { Library } from '../types'
import { useStrmAttachForm } from './useStrmAttachForm'
import { useStrmGenerateForm } from './useStrmGenerateForm'
import { useStrmImportForm } from './useStrmImportForm'
import { useStrmRepairForm } from './useStrmRepairForm'
import { useStrmTreeGenerateForm } from './useStrmTreeGenerateForm'
export function useStrmPage() {
const [libraries, setLibraries] = useState<Library[]>([])
const generate = useStrmGenerateForm(libraries)
const treeGenerate = useStrmTreeGenerateForm()
const repair = useStrmRepairForm()
const importForm = useStrmImportForm(libraries)
const attach = useStrmAttachForm()
@@ -23,6 +25,7 @@ export function useStrmPage() {
generate,
importForm,
libraries,
repair,
treeGenerate,
}
}
+65
View File
@@ -0,0 +1,65 @@
import type { FormEvent } from 'react'
import { useState } from 'react'
import toast from 'react-hot-toast'
import { strmAPI, type RepairSTRMResult } from '../api/strm'
import { currentOrigin } from './strmPageModel'
import { apiErrorMessage, isHTTPURL } from './strmPageUtils'
export function useStrmRepairForm() {
const [baseURL, setBaseURL] = useState(currentOrigin())
const [outputDir, setOutputDir] = useState('data/strm/tree')
const [runningMode, setRunningMode] = useState<'repair' | 'preview' | null>(null)
const [result, setResult] = useState<RepairSTRMResult | null>(null)
const runRepair = async (dryRun: boolean) => {
const trimmedBaseURL = baseURL.trim()
if (!outputDir.trim()) {
toast.error('请填写 STRM 输出目录')
return
}
if (trimmedBaseURL && !isHTTPURL(trimmedBaseURL)) {
toast.error('域名必须以 http:// 或 https:// 开头')
return
}
setRunningMode(dryRun ? 'preview' : 'repair')
try {
const next = await strmAPI.repair({
output_dir: outputDir.trim(),
base_url: trimmedBaseURL.replace(/\/+$/, '') || undefined,
dry_run: dryRun,
})
setResult(next)
if (dryRun) {
toast.success(`预检完成:可修复 ${next.previewed ?? 0} 个 · 跳过 ${next.skipped}`)
} else {
toast.success(`修复完成:修复 ${next.repaired} 个 · 跳过 ${next.skipped}`)
}
} catch (err: unknown) {
toast.error(apiErrorMessage(err, dryRun ? 'STRM 修复预检失败' : 'STRM 修复失败'))
} finally {
setRunningMode(null)
}
}
const onRepair = async (event: FormEvent) => {
event.preventDefault()
await runRepair(false)
}
const onPreview = async () => {
await runRepair(true)
}
return {
baseURL,
outputDir,
repairing: runningMode !== null,
result,
runningMode,
onPreview,
onRepair,
setBaseURL,
setOutputDir,
}
}