implemented api for local library & global config

This commit is contained in:
2025-12-26 22:12:36 +01:00
parent dbce12b708
commit 6075dcf149
8 changed files with 394 additions and 3 deletions

View File

@@ -0,0 +1,71 @@
import fs from 'fs';
import path from 'path';
import os from 'os';
import yaml from 'js-yaml';
const BASE_DIR = path.join(os.homedir(), 'WaifuBoards');
const CONFIG_PATH = path.join(BASE_DIR, 'config.yaml');
const DEFAULT_CONFIG = {
library: {
anime: null,
manga: null,
novels: null
}
};
function ensureConfigFile() {
if (!fs.existsSync(BASE_DIR)) {
fs.mkdirSync(BASE_DIR, { recursive: true });
}
if (!fs.existsSync(CONFIG_PATH)) {
fs.writeFileSync(
CONFIG_PATH,
yaml.dump(DEFAULT_CONFIG),
'utf8'
);
}
}
export function getConfig() {
ensureConfigFile();
const raw = fs.readFileSync(CONFIG_PATH, 'utf8');
return yaml.load(raw) || DEFAULT_CONFIG;
}
export function setConfig(partialConfig) {
ensureConfigFile();
const current = getConfig();
const next = deepMerge(current, partialConfig);
fs.writeFileSync(
CONFIG_PATH,
yaml.dump(next),
'utf8'
);
return next;
}
function deepMerge(target, source) {
for (const key in source) {
if (
source[key] &&
typeof source[key] === 'object' &&
!Array.isArray(source[key])
) {
target[key] = deepMerge(target[key] || {}, source[key]);
} else {
target[key] = source[key];
}
}
return target;
}
module.exports = {
ensureConfigFile,
getConfig,
setConfig,
};

View File

@@ -2,6 +2,54 @@ const sqlite3 = require('sqlite3').verbose();
const path = require("path");
const fs = require("fs");
async function ensureLocalLibrarySchema(db) {
await run(db, `
CREATE TABLE IF NOT EXISTS local_entries (
id TEXT PRIMARY KEY,
type TEXT NOT NULL,
path TEXT NOT NULL,
folder_name TEXT NOT NULL,
matched_id INTEGER,
matched_source TEXT,
last_scan INTEGER NOT NULL
)
`);
await run(db, `
CREATE TABLE IF NOT EXISTS local_files (
id TEXT PRIMARY KEY,
entry_id TEXT NOT NULL,
file_path TEXT NOT NULL,
unit_number INTEGER,
FOREIGN KEY (entry_id) REFERENCES local_entries(id)
)
`);
await run(db, `
CREATE INDEX IF NOT EXISTS idx_local_entries_type
ON local_entries(type)
`);
await run(db, `
CREATE INDEX IF NOT EXISTS idx_local_entries_matched
ON local_entries(matched_id)
`);
await run(db, `
CREATE INDEX IF NOT EXISTS idx_local_files_entry
ON local_files(entry_id)
`);
}
function run(db, sql, params = []) {
return new Promise((resolve, reject) => {
db.run(sql, params, err => {
if (err) reject(err);
else resolve();
});
});
}
async function ensureUserDataDB(dbPath) {
const dir = path.dirname(dbPath);
@@ -230,5 +278,6 @@ module.exports = {
ensureAnilistSchema,
ensureExtensionsTable,
ensureCacheTable,
ensureFavoritesDB
ensureFavoritesDB,
ensureLocalLibrarySchema
};