add scanning
This commit is contained in:
38
src/lib/app-settings.ts
Normal file
38
src/lib/app-settings.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { getDb } from './db'
|
||||
|
||||
interface ScanConfig {
|
||||
schedule: string
|
||||
enabled: boolean
|
||||
lastScanAt: number | null
|
||||
}
|
||||
|
||||
function getSetting(key: string): string | null {
|
||||
const db = getDb()
|
||||
const row = db
|
||||
.prepare('SELECT value FROM app_settings WHERE key = ?')
|
||||
.get(key) as { value: string } | undefined
|
||||
return row?.value ?? null
|
||||
}
|
||||
|
||||
function setSetting(key: string, value: string): void {
|
||||
const db = getDb()
|
||||
db.prepare('INSERT OR REPLACE INTO app_settings (key, value) VALUES (?, ?)').run(key, value)
|
||||
}
|
||||
|
||||
export function getScanConfig(): ScanConfig {
|
||||
const schedule = getSetting('scan_schedule') ?? '0 * * * *'
|
||||
const enabled = getSetting('scan_enabled') !== 'false'
|
||||
const lastRanRaw = getSetting('scan_last_ran')
|
||||
const lastScanAt =
|
||||
lastRanRaw && lastRanRaw.length > 0 ? parseInt(lastRanRaw, 10) : null
|
||||
return { schedule, enabled, lastScanAt }
|
||||
}
|
||||
|
||||
export function updateScanConfig(schedule: string, enabled: boolean): void {
|
||||
setSetting('scan_schedule', schedule)
|
||||
setSetting('scan_enabled', enabled ? 'true' : 'false')
|
||||
}
|
||||
|
||||
export function setScanLastRan(ts: number): void {
|
||||
setSetting('scan_last_ran', String(ts))
|
||||
}
|
||||
@@ -71,9 +71,46 @@ function initDb(db: Database.Database): void {
|
||||
tv_loop INTEGER NOT NULL DEFAULT 0,
|
||||
tv_muted INTEGER NOT NULL DEFAULT 0
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS app_settings (
|
||||
key TEXT PRIMARY KEY,
|
||||
value TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS media_items (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
library_id TEXT NOT NULL REFERENCES libraries(id) ON DELETE CASCADE,
|
||||
item_key TEXT NOT NULL UNIQUE,
|
||||
item_type TEXT NOT NULL CHECK(item_type IN ('movie','tv_series','tv_season','tv_episode','game','game_series')),
|
||||
parent_key TEXT,
|
||||
title TEXT,
|
||||
year INTEGER,
|
||||
plot TEXT,
|
||||
genres TEXT,
|
||||
metadata TEXT,
|
||||
scanned_at INTEGER NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS media_items_library_id ON media_items(library_id);
|
||||
CREATE INDEX IF NOT EXISTS media_items_parent_key ON media_items(parent_key);
|
||||
`)
|
||||
|
||||
migrateLibrariesType(db)
|
||||
seedAppSettings(db)
|
||||
}
|
||||
|
||||
function seedAppSettings(db: Database.Database): void {
|
||||
const defaults: Record<string, string> = {
|
||||
scan_schedule: '0 * * * *',
|
||||
scan_enabled: 'true',
|
||||
scan_last_ran: '',
|
||||
}
|
||||
const insert = db.prepare(
|
||||
'INSERT OR IGNORE INTO app_settings (key, value) VALUES (?, ?)'
|
||||
)
|
||||
for (const [key, value] of Object.entries(defaults)) {
|
||||
insert.run(key, value)
|
||||
}
|
||||
}
|
||||
|
||||
function migrateLibrariesType(db: Database.Database): void {
|
||||
|
||||
373
src/lib/scanner.ts
Normal file
373
src/lib/scanner.ts
Normal file
@@ -0,0 +1,373 @@
|
||||
import path from 'path'
|
||||
import type Database from 'better-sqlite3'
|
||||
import type { Library, Movie, TvSeries, TvSeason, TvEpisode, Game, GameSeries } from '@/types'
|
||||
import { getDb } from './db'
|
||||
import { getLibraries, resolveLibraryRoot } from './libraries'
|
||||
import { setScanLastRan } from './app-settings'
|
||||
import { scanMoviesLibrary } from './movies'
|
||||
import { scanTvLibrary, scanTvSeasons, scanTvEpisodes } from './tv'
|
||||
import { scanGamesLibrary } from './games'
|
||||
import { getThumbnailPath } from './thumbnails'
|
||||
|
||||
const VIDEO_EXTENSIONS = new Set(['.mkv', '.mp4', '.avi', '.mov', '.m4v', '.wmv', '.ts', '.m2ts'])
|
||||
const IMAGE_EXTENSIONS = new Set(['.jpg', '.jpeg', '.png', '.gif', '.webp', '.bmp'])
|
||||
|
||||
let scanRunning = false
|
||||
|
||||
export function isScanRunning(): boolean {
|
||||
return scanRunning
|
||||
}
|
||||
|
||||
export async function runFullScan(): Promise<void> {
|
||||
if (scanRunning) return
|
||||
scanRunning = true
|
||||
console.log('[scanner] Starting full library scan')
|
||||
try {
|
||||
const libraries = getLibraries()
|
||||
for (const library of libraries) {
|
||||
try {
|
||||
await runLibraryScan(library)
|
||||
} catch (err) {
|
||||
console.error(`[scanner] Error scanning library "${library.name}":`, err)
|
||||
}
|
||||
}
|
||||
const now = Date.now()
|
||||
setScanLastRan(now)
|
||||
console.log('[scanner] Full scan complete')
|
||||
} finally {
|
||||
scanRunning = false
|
||||
}
|
||||
}
|
||||
|
||||
export async function runLibraryScan(library: Library): Promise<void> {
|
||||
const libraryRoot = resolveLibraryRoot(library)
|
||||
console.log(`[scanner] Scanning library "${library.name}" (${library.type}) at ${libraryRoot}`)
|
||||
|
||||
switch (library.type) {
|
||||
case 'movies':
|
||||
await scanMovies(library, libraryRoot)
|
||||
break
|
||||
case 'tv':
|
||||
await scanTv(library, libraryRoot)
|
||||
break
|
||||
case 'games':
|
||||
await scanGames(library, libraryRoot)
|
||||
break
|
||||
case 'mixed':
|
||||
await scanMixed(library, libraryRoot)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Movies
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function scanMovies(library: Library, libraryRoot: string): Promise<void> {
|
||||
const movies = scanMoviesLibrary(libraryRoot, library.id)
|
||||
const db = getDb()
|
||||
const now = Date.now()
|
||||
|
||||
clearLibraryItems(db, library.id)
|
||||
|
||||
const upsert = db.prepare(`
|
||||
INSERT INTO media_items (library_id, item_key, item_type, title, year, plot, genres, metadata, scanned_at)
|
||||
VALUES (@library_id, @item_key, @item_type, @title, @year, @plot, @genres, @metadata, @scanned_at)
|
||||
ON CONFLICT(item_key) DO UPDATE SET
|
||||
title = excluded.title,
|
||||
year = excluded.year,
|
||||
plot = excluded.plot,
|
||||
genres = excluded.genres,
|
||||
metadata = excluded.metadata,
|
||||
scanned_at = excluded.scanned_at
|
||||
`)
|
||||
|
||||
for (const movie of movies) {
|
||||
const itemKey = `${library.id}:movie:${movie.id}`
|
||||
upsert.run({
|
||||
library_id: library.id,
|
||||
item_key: itemKey,
|
||||
item_type: 'movie',
|
||||
title: movie.title,
|
||||
year: movie.year ?? null,
|
||||
plot: movie.plot ?? null,
|
||||
genres: JSON.stringify(movie.genres),
|
||||
metadata: JSON.stringify({ rating: movie.rating, runtime: movie.runtime }),
|
||||
scanned_at: now,
|
||||
})
|
||||
|
||||
// Pre-generate poster thumbnail
|
||||
if (movie.posterUrl) {
|
||||
await prewarmThumbnailFromUrl(movie.posterUrl, library.id, libraryRoot, 'image')
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`[scanner] movies: indexed ${movies.length} items`)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// TV
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function scanTv(library: Library, libraryRoot: string): Promise<void> {
|
||||
const series = scanTvLibrary(libraryRoot, library.id)
|
||||
const db = getDb()
|
||||
const now = Date.now()
|
||||
|
||||
clearLibraryItems(db, library.id)
|
||||
|
||||
const upsertSeries = db.prepare(`
|
||||
INSERT INTO media_items (library_id, item_key, item_type, title, year, plot, genres, metadata, scanned_at)
|
||||
VALUES (@library_id, @item_key, @item_type, @title, @year, @plot, @genres, @metadata, @scanned_at)
|
||||
ON CONFLICT(item_key) DO UPDATE SET
|
||||
title = excluded.title,
|
||||
year = excluded.year,
|
||||
plot = excluded.plot,
|
||||
genres = excluded.genres,
|
||||
metadata = excluded.metadata,
|
||||
scanned_at = excluded.scanned_at
|
||||
`)
|
||||
|
||||
const upsertChild = db.prepare(`
|
||||
INSERT INTO media_items (library_id, item_key, item_type, parent_key, title, year, plot, genres, metadata, scanned_at)
|
||||
VALUES (@library_id, @item_key, @item_type, @parent_key, @title, @year, @plot, @genres, @metadata, @scanned_at)
|
||||
ON CONFLICT(item_key) DO UPDATE SET
|
||||
parent_key = excluded.parent_key,
|
||||
title = excluded.title,
|
||||
year = excluded.year,
|
||||
plot = excluded.plot,
|
||||
genres = excluded.genres,
|
||||
metadata = excluded.metadata,
|
||||
scanned_at = excluded.scanned_at
|
||||
`)
|
||||
|
||||
let episodeCount = 0
|
||||
|
||||
for (const show of series) {
|
||||
const seriesKey = `${library.id}:tv_series:${show.id}`
|
||||
upsertSeries.run({
|
||||
library_id: library.id,
|
||||
item_key: seriesKey,
|
||||
item_type: 'tv_series',
|
||||
title: show.title,
|
||||
year: show.year ?? null,
|
||||
plot: show.plot ?? null,
|
||||
genres: JSON.stringify(show.genres),
|
||||
metadata: JSON.stringify({ status: show.status, seasonCount: show.seasonCount }),
|
||||
scanned_at: now,
|
||||
})
|
||||
|
||||
if (show.posterUrl) {
|
||||
await prewarmThumbnailFromUrl(show.posterUrl, library.id, libraryRoot, 'image')
|
||||
}
|
||||
|
||||
const seasons = scanTvSeasons(libraryRoot, library.id, show.id)
|
||||
for (const season of seasons) {
|
||||
const seasonKey = `${library.id}:tv_season:${show.id}:${season.id}`
|
||||
upsertChild.run({
|
||||
library_id: library.id,
|
||||
item_key: seasonKey,
|
||||
item_type: 'tv_season',
|
||||
parent_key: seriesKey,
|
||||
title: season.title,
|
||||
year: null,
|
||||
plot: null,
|
||||
genres: JSON.stringify([]),
|
||||
metadata: JSON.stringify({ seasonNumber: season.seasonNumber, episodeCount: season.episodeCount }),
|
||||
scanned_at: now,
|
||||
})
|
||||
|
||||
if (season.posterUrl) {
|
||||
await prewarmThumbnailFromUrl(season.posterUrl, library.id, libraryRoot, 'image')
|
||||
}
|
||||
|
||||
const episodes = scanTvEpisodes(libraryRoot, library.id, show.id, season.id)
|
||||
for (const episode of episodes) {
|
||||
const episodeKey = `${library.id}:tv_episode:${show.id}:${season.id}:${episode.id}`
|
||||
upsertChild.run({
|
||||
library_id: library.id,
|
||||
item_key: episodeKey,
|
||||
item_type: 'tv_episode',
|
||||
parent_key: seasonKey,
|
||||
title: episode.title,
|
||||
year: null,
|
||||
plot: episode.plot ?? null,
|
||||
genres: JSON.stringify([]),
|
||||
metadata: JSON.stringify({
|
||||
episodeNumber: episode.episodeNumber,
|
||||
seasonNumber: episode.seasonNumber,
|
||||
aired: episode.aired,
|
||||
rating: episode.rating,
|
||||
}),
|
||||
scanned_at: now,
|
||||
})
|
||||
|
||||
// Pre-generate video thumbnail (seek-based frame extraction)
|
||||
const videoAbsPath = path.join(libraryRoot, episode.videoPath)
|
||||
try {
|
||||
await getThumbnailPath(videoAbsPath, library.id, 'video')
|
||||
} catch (err) {
|
||||
console.warn(`[scanner] Could not generate thumbnail for ${episode.videoPath}:`, err instanceof Error ? err.message : err)
|
||||
}
|
||||
episodeCount++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`[scanner] tv: indexed ${series.length} series, ${episodeCount} episodes`)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Games
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function scanGames(library: Library, libraryRoot: string): Promise<void> {
|
||||
const items = scanGamesLibrary(libraryRoot, library.id)
|
||||
const db = getDb()
|
||||
const now = Date.now()
|
||||
|
||||
clearLibraryItems(db, library.id)
|
||||
|
||||
const upsertGame = db.prepare(`
|
||||
INSERT INTO media_items (library_id, item_key, item_type, title, metadata, scanned_at)
|
||||
VALUES (@library_id, @item_key, @item_type, @title, @metadata, @scanned_at)
|
||||
ON CONFLICT(item_key) DO UPDATE SET
|
||||
title = excluded.title,
|
||||
metadata = excluded.metadata,
|
||||
scanned_at = excluded.scanned_at
|
||||
`)
|
||||
|
||||
const upsertChildGame = db.prepare(`
|
||||
INSERT INTO media_items (library_id, item_key, item_type, parent_key, title, metadata, scanned_at)
|
||||
VALUES (@library_id, @item_key, @item_type, @parent_key, @title, @metadata, @scanned_at)
|
||||
ON CONFLICT(item_key) DO UPDATE SET
|
||||
parent_key = excluded.parent_key,
|
||||
title = excluded.title,
|
||||
metadata = excluded.metadata,
|
||||
scanned_at = excluded.scanned_at
|
||||
`)
|
||||
|
||||
let gameCount = 0
|
||||
|
||||
for (const item of items) {
|
||||
if ('games' in item) {
|
||||
// GameSeries
|
||||
const series = item as GameSeries
|
||||
const seriesKey = `${library.id}:game_series:${series.id}`
|
||||
upsertGame.run({
|
||||
library_id: library.id,
|
||||
item_key: seriesKey,
|
||||
item_type: 'game_series',
|
||||
title: series.title,
|
||||
metadata: JSON.stringify({ gameCount: series.games.length }),
|
||||
scanned_at: now,
|
||||
})
|
||||
|
||||
if (series.coverUrl) {
|
||||
await prewarmThumbnailFromUrl(series.coverUrl, library.id, libraryRoot, 'image')
|
||||
}
|
||||
|
||||
for (const game of series.games) {
|
||||
const gameKey = `${library.id}:game:${game.id}`
|
||||
upsertChildGame.run({
|
||||
library_id: library.id,
|
||||
item_key: gameKey,
|
||||
item_type: 'game',
|
||||
parent_key: seriesKey,
|
||||
title: game.title,
|
||||
metadata: JSON.stringify({ zipFiles: game.zipFiles }),
|
||||
scanned_at: now,
|
||||
})
|
||||
|
||||
if (game.coverUrl) {
|
||||
await prewarmThumbnailFromUrl(game.coverUrl, library.id, libraryRoot, 'image')
|
||||
}
|
||||
gameCount++
|
||||
}
|
||||
} else {
|
||||
// Standalone Game
|
||||
const game = item as Game
|
||||
const gameKey = `${library.id}:game:${game.id}`
|
||||
upsertGame.run({
|
||||
library_id: library.id,
|
||||
item_key: gameKey,
|
||||
item_type: 'game',
|
||||
title: game.title,
|
||||
metadata: JSON.stringify({ zipFiles: game.zipFiles }),
|
||||
scanned_at: now,
|
||||
})
|
||||
|
||||
if (game.coverUrl) {
|
||||
await prewarmThumbnailFromUrl(game.coverUrl, library.id, libraryRoot, 'image')
|
||||
}
|
||||
gameCount++
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`[scanner] games: indexed ${gameCount} games`)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Mixed (thumbnail pre-generation only — no DB indexing)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function scanMixed(library: Library, libraryRoot: string): Promise<void> {
|
||||
const fs = await import('fs')
|
||||
let entries: string[]
|
||||
try {
|
||||
entries = fs.readdirSync(libraryRoot, { withFileTypes: true })
|
||||
.filter((d) => d.isFile() && !d.name.startsWith('.'))
|
||||
.map((d) => d.name) as unknown as string[]
|
||||
} catch {
|
||||
return
|
||||
}
|
||||
|
||||
let count = 0
|
||||
for (const filename of entries) {
|
||||
const ext = path.extname(filename).toLowerCase()
|
||||
let mediaType: 'image' | 'video' | null = null
|
||||
if (IMAGE_EXTENSIONS.has(ext)) mediaType = 'image'
|
||||
else if (VIDEO_EXTENSIONS.has(ext)) mediaType = 'video'
|
||||
if (!mediaType) continue
|
||||
|
||||
const absPath = path.join(libraryRoot, filename)
|
||||
try {
|
||||
await getThumbnailPath(absPath, library.id, mediaType)
|
||||
count++
|
||||
} catch (err) {
|
||||
console.warn(`[scanner] Could not generate thumbnail for ${filename}:`, err instanceof Error ? err.message : err)
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`[scanner] mixed: pre-generated thumbnails for ${count} files`)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function clearLibraryItems(db: Database.Database, libraryId: string): void {
|
||||
db.prepare('DELETE FROM media_items WHERE library_id = ?').run(libraryId)
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the `path` query param from an /api/thumbnail URL and pre-warm
|
||||
* the thumbnail cache for that file.
|
||||
*/
|
||||
async function prewarmThumbnailFromUrl(
|
||||
apiUrl: string,
|
||||
libraryId: string,
|
||||
libraryRoot: string,
|
||||
mediaType: 'image' | 'video'
|
||||
): Promise<void> {
|
||||
try {
|
||||
const relPath = decodeURIComponent(
|
||||
new URL(apiUrl, 'http://localhost').searchParams.get('path') ?? ''
|
||||
)
|
||||
if (!relPath) return
|
||||
const absPath = path.join(libraryRoot, relPath)
|
||||
await getThumbnailPath(absPath, libraryId, mediaType)
|
||||
} catch (err) {
|
||||
console.warn(`[scanner] Could not prewarm thumbnail for ${apiUrl}:`, err instanceof Error ? err.message : err)
|
||||
}
|
||||
}
|
||||
39
src/lib/scheduler.ts
Normal file
39
src/lib/scheduler.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import cron, { type ScheduledTask } from 'node-cron'
|
||||
import { getScanConfig } from './app-settings'
|
||||
import { runFullScan } from './scanner'
|
||||
|
||||
let scheduledTask: ScheduledTask | null = null
|
||||
|
||||
export function startScheduler(): void {
|
||||
const { schedule, enabled } = getScanConfig()
|
||||
|
||||
if (!enabled) {
|
||||
console.log('[scheduler] Scanning is disabled — scheduler not started')
|
||||
return
|
||||
}
|
||||
|
||||
if (!cron.validate(schedule)) {
|
||||
console.error(`[scheduler] Invalid cron expression "${schedule}" — scheduler not started`)
|
||||
return
|
||||
}
|
||||
|
||||
scheduledTask = cron.schedule(schedule, () => {
|
||||
console.log('[scheduler] Cron triggered — running full scan')
|
||||
runFullScan().catch((err) => console.error('[scheduler] Scan error:', err))
|
||||
})
|
||||
|
||||
console.log(`[scheduler] Started with schedule: ${schedule}`)
|
||||
}
|
||||
|
||||
export function stopScheduler(): void {
|
||||
if (scheduledTask) {
|
||||
scheduledTask.stop()
|
||||
scheduledTask = null
|
||||
console.log('[scheduler] Stopped')
|
||||
}
|
||||
}
|
||||
|
||||
export function restartScheduler(): void {
|
||||
stopScheduler()
|
||||
startScheduler()
|
||||
}
|
||||
Reference in New Issue
Block a user