73 lines
2.3 KiB
TypeScript
73 lines
2.3 KiB
TypeScript
import fs from 'fs'
|
|
import { NextRequest, NextResponse } from 'next/server'
|
|
import { getLibrary, resolveLibraryRoot, resolveAndJail } from '@/lib/libraries'
|
|
import { gamesFromDb } from '@/lib/games'
|
|
import { requireLibraryAccess, requireAdmin } from '@/lib/auth'
|
|
import { removeAllAssignmentsForItem } from '@/lib/tags'
|
|
import { getDb } from '@/lib/db'
|
|
|
|
export async function GET(request: NextRequest) {
|
|
const { searchParams } = request.nextUrl
|
|
const libraryId = searchParams.get('libraryId')
|
|
|
|
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 !== 'games') {
|
|
return NextResponse.json({ error: 'Library is not a games library' }, { status: 400 })
|
|
}
|
|
|
|
return NextResponse.json(gamesFromDb(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 gameId = searchParams.get('gameId')
|
|
|
|
if (!libraryId || !gameId) {
|
|
return NextResponse.json({ error: 'Missing libraryId or gameId' }, { status: 400 })
|
|
}
|
|
|
|
const library = getLibrary(libraryId)
|
|
if (!library) {
|
|
return NextResponse.json({ error: 'Library not found' }, { status: 404 })
|
|
}
|
|
if (library.type !== 'games') {
|
|
return NextResponse.json({ error: 'Library is not a games library' }, { status: 400 })
|
|
}
|
|
|
|
const root = resolveLibraryRoot(library)
|
|
const dirName = decodeURIComponent(gameId)
|
|
|
|
let gameDir: string
|
|
try {
|
|
gameDir = resolveAndJail(root, dirName)
|
|
} catch {
|
|
return NextResponse.json({ error: 'Invalid game path' }, { status: 400 })
|
|
}
|
|
|
|
try {
|
|
fs.rmSync(gameDir, { recursive: true, force: true })
|
|
} catch {
|
|
return NextResponse.json({ error: 'Failed to delete game directory' }, { status: 500 })
|
|
}
|
|
|
|
const itemKey = `${libraryId}:game:${gameId}`
|
|
removeAllAssignmentsForItem(itemKey)
|
|
getDb().prepare('DELETE FROM media_items WHERE item_key = ?').run(itemKey)
|
|
|
|
return new NextResponse(null, { status: 204 })
|
|
}
|