bring up to date with github
This commit is contained in:
48
src/lib/db.ts
Normal file
48
src/lib/db.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import path from 'path'
|
||||
import fs from 'fs'
|
||||
import Database from 'better-sqlite3'
|
||||
|
||||
const CONFIG_PATH = process.env.CONFIG_PATH ?? process.cwd()
|
||||
const DB_PATH = path.resolve(CONFIG_PATH, 'medialore.db')
|
||||
|
||||
let _db: Database.Database | null = null
|
||||
|
||||
export function getDb(): Database.Database {
|
||||
if (_db) return _db
|
||||
_db = new Database(DB_PATH)
|
||||
_db.pragma('journal_mode = WAL')
|
||||
_db.pragma('foreign_keys = ON')
|
||||
initDb(_db)
|
||||
return _db
|
||||
}
|
||||
|
||||
function initDb(db: Database.Database): void {
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS tag_categories (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL UNIQUE
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tags (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
category_id TEXT NOT NULL REFERENCES tag_categories(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS tags_name_category ON tags(name, category_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS media_tags (
|
||||
media_key TEXT NOT NULL,
|
||||
tag_id TEXT NOT NULL REFERENCES tags(id) ON DELETE CASCADE,
|
||||
PRIMARY KEY (media_key, tag_id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS libraries (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
path TEXT NOT NULL,
|
||||
type TEXT NOT NULL CHECK(type IN ('games', 'mixed')),
|
||||
cover_ext TEXT NULL
|
||||
);
|
||||
`)
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import type { DirectoryListing, FileEntry, MediaType } from '@/types'
|
||||
import { resolveAndJail } from '@/lib/libraries'
|
||||
|
||||
const HIDDEN_FILES = /^\./
|
||||
|
||||
@@ -27,9 +28,12 @@ export function scanDirectory(
|
||||
libraryId: string,
|
||||
subpath: string
|
||||
): DirectoryListing {
|
||||
const absPath = subpath
|
||||
? path.resolve(libraryRoot, subpath)
|
||||
: libraryRoot
|
||||
let absPath: string
|
||||
try {
|
||||
absPath = subpath ? resolveAndJail(libraryRoot, subpath) : libraryRoot
|
||||
} catch {
|
||||
return { path: subpath, entries: [] }
|
||||
}
|
||||
|
||||
let dirents: fs.Dirent[]
|
||||
try {
|
||||
|
||||
@@ -1,26 +1,25 @@
|
||||
import path from 'path'
|
||||
import fs from 'fs'
|
||||
import type { Library, LibraryType } from '@/types'
|
||||
|
||||
const CONFIG_PATH = process.env.LIBRARIES_CONFIG_PATH ?? path.resolve(process.cwd(), 'libraries.json')
|
||||
const CONFIG_TMP = CONFIG_PATH + '.tmp'
|
||||
import { getDb } from '@/lib/db'
|
||||
|
||||
export function getLibraries(): Library[] {
|
||||
try {
|
||||
const raw = fs.readFileSync(CONFIG_PATH, 'utf-8')
|
||||
return JSON.parse(raw) as Library[]
|
||||
} catch {
|
||||
return []
|
||||
}
|
||||
const db = getDb()
|
||||
return db
|
||||
.prepare('SELECT id, name, path, type, cover_ext as coverExt FROM libraries ORDER BY name')
|
||||
.all() as Library[]
|
||||
}
|
||||
|
||||
export function getLibrary(id: string): Library | undefined {
|
||||
return getLibraries().find((lib) => lib.id === id)
|
||||
const db = getDb()
|
||||
return db
|
||||
.prepare('SELECT id, name, path, type, cover_ext as coverExt FROM libraries WHERE id = ?')
|
||||
.get(id) as Library | undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a library's configured path to an absolute filesystem path.
|
||||
* Paths in libraries.json may be relative (to project root) or absolute.
|
||||
* Paths may be relative (to project root) or absolute.
|
||||
*/
|
||||
export function resolveLibraryRoot(library: Library): string {
|
||||
if (path.isAbsolute(library.path)) {
|
||||
@@ -56,15 +55,6 @@ function slugify(name: string): string {
|
||||
.replace(/^-|-$/g, '')
|
||||
}
|
||||
|
||||
/**
|
||||
* Atomically writes the library list to disk.
|
||||
*/
|
||||
function writeLibraries(libraries: Library[]): void {
|
||||
const json = JSON.stringify(libraries, null, 2) + '\n'
|
||||
fs.writeFileSync(CONFIG_TMP, json, 'utf-8')
|
||||
fs.renameSync(CONFIG_TMP, CONFIG_PATH)
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new library. Validates the path exists as a directory.
|
||||
* Throws a descriptive Error on failure.
|
||||
@@ -91,37 +81,47 @@ export function addLibrary(name: string, libraryPath: string, type: LibraryType)
|
||||
throw new Error(`Path is not a directory: ${trimmedPath}`)
|
||||
}
|
||||
|
||||
const libraries = getLibraries()
|
||||
const db = getDb()
|
||||
|
||||
// Reject exact duplicate names (case-insensitive)
|
||||
const duplicate = db.prepare('SELECT 1 FROM libraries WHERE LOWER(name) = LOWER(?)').get(trimmedName)
|
||||
if (duplicate) {
|
||||
throw new Error(`A library named "${trimmedName}" already exists.`)
|
||||
}
|
||||
|
||||
// Generate a unique id
|
||||
const baseId = slugify(trimmedName) || 'library'
|
||||
let id = baseId
|
||||
let suffix = 2
|
||||
while (libraries.some((lib) => lib.id === id)) {
|
||||
while (db.prepare('SELECT 1 FROM libraries WHERE id = ?').get(id)) {
|
||||
id = `${baseId}-${suffix++}`
|
||||
}
|
||||
|
||||
// Reject exact duplicate names (same name, case-insensitive)
|
||||
const duplicate = libraries.find(
|
||||
(lib) => lib.name.toLowerCase() === trimmedName.toLowerCase()
|
||||
)
|
||||
if (duplicate) {
|
||||
throw new Error(`A library named "${trimmedName}" already exists.`)
|
||||
}
|
||||
db.prepare('INSERT INTO libraries (id, name, path, type) VALUES (?, ?, ?, ?)').run(id, trimmedName, trimmedPath, type)
|
||||
return { id, name: trimmedName, path: trimmedPath, type, coverExt: null }
|
||||
}
|
||||
|
||||
const newLibrary: Library = { id, name: trimmedName, path: trimmedPath, type }
|
||||
writeLibraries([...libraries, newLibrary])
|
||||
return newLibrary
|
||||
/**
|
||||
* Sets the cover image extension for a library (e.g. "jpg", "png").
|
||||
*/
|
||||
export function updateLibraryCover(id: string, ext: string): void {
|
||||
const db = getDb()
|
||||
db.prepare('UPDATE libraries SET cover_ext = ? WHERE id = ?').run(ext, id)
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the custom cover image for a library.
|
||||
*/
|
||||
export function clearLibraryCover(id: string): void {
|
||||
const db = getDb()
|
||||
db.prepare('UPDATE libraries SET cover_ext = NULL WHERE id = ?').run(id)
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a library by id. Returns true if removed, false if not found.
|
||||
*/
|
||||
export function removeLibrary(id: string): boolean {
|
||||
const libraries = getLibraries()
|
||||
const index = libraries.findIndex((lib) => lib.id === id)
|
||||
if (index === -1) return false
|
||||
const updated = libraries.filter((lib) => lib.id !== id)
|
||||
writeLibraries(updated)
|
||||
return true
|
||||
const db = getDb()
|
||||
const result = db.prepare('DELETE FROM libraries WHERE id = ?').run(id)
|
||||
return result.changes > 0
|
||||
}
|
||||
|
||||
@@ -1,42 +1,5 @@
|
||||
import path from 'path'
|
||||
import Database from 'better-sqlite3'
|
||||
import type { Tag, TagCategory } from '@/types'
|
||||
|
||||
const DB_PATH = path.resolve(process.cwd(), 'medialore.db')
|
||||
|
||||
let _db: Database.Database | null = null
|
||||
|
||||
function getDb(): Database.Database {
|
||||
if (_db) return _db
|
||||
_db = new Database(DB_PATH)
|
||||
_db.pragma('journal_mode = WAL')
|
||||
_db.pragma('foreign_keys = ON')
|
||||
initDb(_db)
|
||||
return _db
|
||||
}
|
||||
|
||||
function initDb(db: Database.Database): void {
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS tag_categories (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL UNIQUE
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tags (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
category_id TEXT NOT NULL REFERENCES tag_categories(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS tags_name_category ON tags(name, category_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS media_tags (
|
||||
media_key TEXT NOT NULL,
|
||||
tag_id TEXT NOT NULL REFERENCES tags(id) ON DELETE CASCADE,
|
||||
PRIMARY KEY (media_key, tag_id)
|
||||
);
|
||||
`)
|
||||
}
|
||||
import { getDb } from '@/lib/db'
|
||||
|
||||
function slugify(name: string): string {
|
||||
return name
|
||||
|
||||
Reference in New Issue
Block a user