add more management capabilities
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
import fs from 'fs'
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { getLibrary } from '@/lib/libraries'
|
||||
import { getLibrary, resolveLibraryRoot, resolveAndJail } from '@/lib/libraries'
|
||||
import { gamesFromDb } from '@/lib/games'
|
||||
import { requireLibraryAccess } from '@/lib/auth'
|
||||
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
|
||||
@@ -24,3 +27,46 @@ export async function GET(request: NextRequest) {
|
||||
|
||||
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 })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user