add individual library scanning
This commit is contained in:
28
src/app/api/scan/[id]/route.ts
Normal file
28
src/app/api/scan/[id]/route.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { getLibrary } from '@/lib/libraries'
|
||||
import { isScanRunning, runSingleLibraryScan } from '@/lib/scanner'
|
||||
import { requireAdmin } from '@/lib/auth'
|
||||
|
||||
export async function POST(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
const auth = await requireAdmin(request)
|
||||
if (auth instanceof NextResponse) return auth
|
||||
|
||||
const { id } = await params
|
||||
|
||||
const library = getLibrary(id)
|
||||
if (!library) {
|
||||
return NextResponse.json({ error: 'Library not found' }, { status: 404 })
|
||||
}
|
||||
|
||||
if (isScanRunning()) {
|
||||
return NextResponse.json({ error: 'Scan already in progress' }, { status: 409 })
|
||||
}
|
||||
|
||||
// Fire-and-forget
|
||||
void runSingleLibraryScan(library)
|
||||
|
||||
return new NextResponse(null, { status: 202 })
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import GamesView from '@/components/games/GamesView'
|
||||
import MixedView from '@/components/mixed/MixedView'
|
||||
import MoviesView from '@/components/movies/MoviesView'
|
||||
import TvView from '@/components/tv/TvView'
|
||||
import ScanLibraryButton from '@/components/ScanLibraryButton'
|
||||
|
||||
interface Props {
|
||||
params: Promise<{ id: string }>
|
||||
@@ -37,6 +38,11 @@ export default async function LibraryPage({ params, searchParams }: Props) {
|
||||
<span className="text-sm font-medium" style={{ color: 'var(--text-primary)' }}>
|
||||
{library.name}
|
||||
</span>
|
||||
{session.role === 'admin' && (
|
||||
<div className="ml-auto">
|
||||
<ScanLibraryButton libraryId={id} />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{library.type === 'games' && <GamesView libraryId={id} />}
|
||||
|
||||
@@ -286,7 +286,10 @@ function AddLibraryForm({ onAdded }: { onAdded: () => void }) {
|
||||
return
|
||||
}
|
||||
|
||||
// Success — reset form
|
||||
// Success — fire scan for the new library (fire-and-forget)
|
||||
void fetch(`/api/scan/${encodeURIComponent((data as { id: string }).id)}`, { method: 'POST' })
|
||||
|
||||
// Reset form
|
||||
setName('')
|
||||
setLibPath('')
|
||||
setType('games')
|
||||
|
||||
Reference in New Issue
Block a user