86 lines
2.7 KiB
TypeScript
86 lines
2.7 KiB
TypeScript
import fs from 'fs'
|
|
import path from 'path'
|
|
import { NextRequest, NextResponse } from 'next/server'
|
|
import { getLibrary, resolveLibraryRoot, resolveAndJail } from '@/lib/libraries'
|
|
import { scanTvLibrary, scanTvSeasons, scanTvEpisodes } from '@/lib/tv'
|
|
import { removeAllAssignmentsForItem } from '@/lib/tags'
|
|
import { requireLibraryAccess, requireAdmin } from '@/lib/auth'
|
|
|
|
export async function GET(request: NextRequest) {
|
|
const { searchParams } = request.nextUrl
|
|
const libraryId = searchParams.get('libraryId')
|
|
const seriesId = searchParams.get('seriesId')
|
|
const seasonId = searchParams.get('seasonId')
|
|
|
|
if (!libraryId) {
|
|
return NextResponse.json({ error: 'Missing libraryId' }, { status: 400 })
|
|
}
|
|
|
|
const auth = await requireLibraryAccess(request, libraryId)
|
|
if (auth instanceof NextResponse) return auth
|
|
|
|
const library = getLibrary(libraryId)
|
|
if (!library) {
|
|
return NextResponse.json({ error: 'Library not found' }, { status: 404 })
|
|
}
|
|
if (library.type !== 'tv') {
|
|
return NextResponse.json({ error: 'Library is not a TV library' }, { status: 400 })
|
|
}
|
|
|
|
const root = resolveLibraryRoot(library)
|
|
|
|
if (seriesId && seasonId) {
|
|
const episodes = scanTvEpisodes(root, libraryId, seriesId, seasonId)
|
|
return NextResponse.json(episodes)
|
|
}
|
|
|
|
if (seriesId) {
|
|
const seasons = scanTvSeasons(root, libraryId, seriesId)
|
|
return NextResponse.json(seasons)
|
|
}
|
|
|
|
const series = scanTvLibrary(root, libraryId)
|
|
return NextResponse.json(series)
|
|
}
|
|
|
|
export async function DELETE(request: NextRequest) {
|
|
const auth = await requireAdmin(request)
|
|
if (auth instanceof NextResponse) return auth
|
|
|
|
const { searchParams } = request.nextUrl
|
|
const libraryId = searchParams.get('libraryId')
|
|
const seriesId = searchParams.get('seriesId')
|
|
|
|
if (!libraryId || !seriesId) {
|
|
return NextResponse.json({ error: 'Missing libraryId or seriesId' }, { status: 400 })
|
|
}
|
|
|
|
const library = getLibrary(libraryId)
|
|
if (!library) {
|
|
return NextResponse.json({ error: 'Library not found' }, { status: 404 })
|
|
}
|
|
if (library.type !== 'tv') {
|
|
return NextResponse.json({ error: 'Library is not a TV library' }, { status: 400 })
|
|
}
|
|
|
|
const root = resolveLibraryRoot(library)
|
|
const dirName = decodeURIComponent(seriesId)
|
|
|
|
let seriesDir: string
|
|
try {
|
|
seriesDir = resolveAndJail(root, dirName)
|
|
} catch {
|
|
return NextResponse.json({ error: 'Invalid series path' }, { status: 400 })
|
|
}
|
|
|
|
try {
|
|
fs.rmSync(seriesDir, { recursive: true, force: true })
|
|
} catch {
|
|
return NextResponse.json({ error: 'Failed to delete series directory' }, { status: 500 })
|
|
}
|
|
|
|
removeAllAssignmentsForItem(`${libraryId}:${seriesId}`)
|
|
|
|
return new NextResponse(null, { status: 204 })
|
|
}
|