115 lines
3.7 KiB
TypeScript
115 lines
3.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 { tvSeriesFromDb, tvSeasonsFromDb, tvEpisodesFromDb } from '@/lib/tv'
|
|
import { removeAllAssignmentsForItem } from '@/lib/tags'
|
|
import { requireLibraryAccess, requireAdmin } from '@/lib/auth'
|
|
import { getDb } from '@/lib/db'
|
|
|
|
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 })
|
|
}
|
|
|
|
if (seriesId && seasonId) {
|
|
return NextResponse.json(tvEpisodesFromDb(libraryId, seriesId, seasonId))
|
|
}
|
|
|
|
if (seriesId) {
|
|
return NextResponse.json(tvSeasonsFromDb(libraryId, seriesId))
|
|
}
|
|
|
|
return NextResponse.json(tvSeriesFromDb(libraryId))
|
|
}
|
|
|
|
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')
|
|
const episodeKey = searchParams.get('episodeKey')
|
|
|
|
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)
|
|
|
|
// Episode-level delete
|
|
if (episodeKey) {
|
|
const db = getDb()
|
|
const row = db.prepare('SELECT file_path FROM media_items WHERE item_key = ?').get(episodeKey) as { file_path: string | null } | undefined
|
|
if (!row?.file_path) {
|
|
return NextResponse.json({ error: 'Episode not found' }, { status: 404 })
|
|
}
|
|
|
|
let episodePath: string
|
|
try {
|
|
episodePath = resolveAndJail(root, row.file_path)
|
|
} catch {
|
|
return NextResponse.json({ error: 'Invalid episode path' }, { status: 400 })
|
|
}
|
|
|
|
try {
|
|
fs.unlinkSync(episodePath)
|
|
// Also remove sidecar NFO if it exists
|
|
const nfoPath = episodePath.replace(path.extname(episodePath), '.nfo')
|
|
if (fs.existsSync(nfoPath)) fs.unlinkSync(nfoPath)
|
|
} catch {
|
|
return NextResponse.json({ error: 'Failed to delete episode file' }, { status: 500 })
|
|
}
|
|
|
|
removeAllAssignmentsForItem(episodeKey)
|
|
db.prepare('DELETE FROM media_items WHERE item_key = ?').run(episodeKey)
|
|
|
|
return new NextResponse(null, { status: 204 })
|
|
}
|
|
|
|
// Series-level delete
|
|
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}:tv_series:${seriesId}`)
|
|
|
|
return new NextResponse(null, { status: 204 })
|
|
}
|