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 MixedView from '@/components/mixed/MixedView'
|
||||||
import MoviesView from '@/components/movies/MoviesView'
|
import MoviesView from '@/components/movies/MoviesView'
|
||||||
import TvView from '@/components/tv/TvView'
|
import TvView from '@/components/tv/TvView'
|
||||||
|
import ScanLibraryButton from '@/components/ScanLibraryButton'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
params: Promise<{ id: string }>
|
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)' }}>
|
<span className="text-sm font-medium" style={{ color: 'var(--text-primary)' }}>
|
||||||
{library.name}
|
{library.name}
|
||||||
</span>
|
</span>
|
||||||
|
{session.role === 'admin' && (
|
||||||
|
<div className="ml-auto">
|
||||||
|
<ScanLibraryButton libraryId={id} />
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{library.type === 'games' && <GamesView libraryId={id} />}
|
{library.type === 'games' && <GamesView libraryId={id} />}
|
||||||
|
|||||||
@@ -286,7 +286,10 @@ function AddLibraryForm({ onAdded }: { onAdded: () => void }) {
|
|||||||
return
|
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('')
|
setName('')
|
||||||
setLibPath('')
|
setLibPath('')
|
||||||
setType('games')
|
setType('games')
|
||||||
|
|||||||
53
src/components/ScanLibraryButton.tsx
Normal file
53
src/components/ScanLibraryButton.tsx
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
'use client'
|
||||||
|
|
||||||
|
import { useState } from 'react'
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
libraryId: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function ScanLibraryButton({ libraryId }: Props) {
|
||||||
|
const [scanning, setScanning] = useState(false)
|
||||||
|
const [message, setMessage] = useState<string | null>(null)
|
||||||
|
|
||||||
|
const handleScan = async () => {
|
||||||
|
setScanning(true)
|
||||||
|
setMessage(null)
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch(`/api/scan/${encodeURIComponent(libraryId)}`, { method: 'POST' })
|
||||||
|
|
||||||
|
if (res.status === 409) {
|
||||||
|
setMessage('A scan is already in progress.')
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
setMessage('Failed to start scan.')
|
||||||
|
} finally {
|
||||||
|
setScanning(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex items-center gap-3">
|
||||||
|
<button
|
||||||
|
onClick={handleScan}
|
||||||
|
disabled={scanning}
|
||||||
|
className="text-sm px-3 py-1.5 rounded-lg transition-colors disabled:opacity-50"
|
||||||
|
style={{ backgroundColor: 'var(--border)', color: 'var(--text-secondary)' }}
|
||||||
|
onMouseEnter={(e) => {
|
||||||
|
if (!scanning) (e.currentTarget as HTMLElement).style.color = 'var(--text-primary)'
|
||||||
|
}}
|
||||||
|
onMouseLeave={(e) => {
|
||||||
|
;(e.currentTarget as HTMLElement).style.color = 'var(--text-secondary)'
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{scanning ? 'Scanning…' : 'Scan'}
|
||||||
|
</button>
|
||||||
|
{message && (
|
||||||
|
<span className="text-xs" style={{ color: 'var(--text-secondary)' }}>
|
||||||
|
{message}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -38,6 +38,20 @@ export async function runFullScan(): Promise<void> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function runSingleLibraryScan(library: Library): Promise<void> {
|
||||||
|
if (scanRunning) return
|
||||||
|
scanRunning = true
|
||||||
|
console.log(`[scanner] Starting single library scan for "${library.name}"`)
|
||||||
|
try {
|
||||||
|
await runLibraryScan(library)
|
||||||
|
const now = Date.now()
|
||||||
|
setScanLastRan(now)
|
||||||
|
console.log(`[scanner] Single library scan complete for "${library.name}"`)
|
||||||
|
} finally {
|
||||||
|
scanRunning = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function runLibraryScan(library: Library): Promise<void> {
|
export async function runLibraryScan(library: Library): Promise<void> {
|
||||||
const libraryRoot = resolveLibraryRoot(library)
|
const libraryRoot = resolveLibraryRoot(library)
|
||||||
console.log(`[scanner] Scanning library "${library.name}" (${library.type}) at ${libraryRoot}`)
|
console.log(`[scanner] Scanning library "${library.name}" (${library.type}) at ${libraryRoot}`)
|
||||||
|
|||||||
Reference in New Issue
Block a user