This commit is contained in:
metacryst
2025-11-13 15:30:11 -06:00
commit 493f1c2e1f
16 changed files with 3008 additions and 0 deletions

76
server/db/db.js Normal file
View File

@@ -0,0 +1,76 @@
import chalk from 'chalk'
import path from 'path';
import fs from 'fs/promises';
import { pathToFileURL } from 'url';
export default class Database {
#nodes;
#edges;
#labels = {}
constructor() {
this.getData()
}
async getData() {
const dbData = await fs.readFile(path.join(process.cwd(), 'db/db.json'), 'utf8');
let dbJson;
try {
dbJson = JSON.parse(dbData);
} catch {
dbJson = []
}
this.#nodes = dbJson["nodes"];
this.#edges = dbJson["edges"];
console.log(chalk.yellow("DB established."))
Object.preventExtensions(this);
}
// superKey = "nodes" || "edges"
async writeData(superKey, key, value) {
const dbData = await fs.readFile(path.join(process.cwd(), 'db/db.json'), 'utf8');
let dbJson;
try {
dbJson = JSON.parse(dbData);
} catch {
dbJson = []
}
dbJson[superKey][key] = value;
await fs.writeFile(path.join(process.cwd(), 'db/db.json'), JSON.stringify(dbJson, null, 2), 'utf8')
}
async getLabelModels() {
const labelHandlers = {};
const labelDir = path.join(process.cwd(), 'src/model/labels');
const files = await fs.readdir(labelDir);
for (const file of files) {
if (!file.endsWith('.js')) continue;
const label = path.basename(file, '.js');
const modulePath = path.join(labelDir, file);
const module = await import(pathToFileURL(modulePath).href);
labelHandlers[label] = module.default;
if (!this.#labels[label]) {
this.#labels[label] = [];
}
}
return labelHandlers
}
generateUserID() {
let id = this.#labels["User"].length + 1;
while (this.get.user(`user-${id}`)) {
id++;
}
return `user-${id}`; // O(1) most of the time
}
async getAll() {
return { nodes: this.#nodes }
}
}

67
server/db/getDownloads.js Normal file
View File

@@ -0,0 +1,67 @@
// getDownloadsFiles.js
import fs from 'fs/promises';
import path from 'path';
import os from 'os';
const DOWNLOADS = path.join(os.homedir(), 'Downloads');
/**
* Recursively get all files in Downloads with name, path, and inode.
* @returns {Promise<Array<{name: string, path: string, inode: number|null}>>}
*/
async function getAllDownloadsFiles() {
const files = [];
async function scan(dir) {
let entries;
try {
entries = await fs.readdir(dir, { withFileTypes: true });
} catch (err) {
console.warn(`Cannot read directory: ${dir}`, err.message);
return;
}
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
try {
// Get stats (inode = ino on Unix, null on Windows)
// const stats = await fs.stat(fullPath);
// const inode = stats.ino ?? null;
if (entry.isDirectory()) {
// Recurse into subdirectories
await scan(fullPath);
} else if (entry.isFile()) {
const parsed = path.parse(entry.name); // splits name/ext
const realExtension = path.extname(fullPath); // from full path (more reliable)
// Use realExtension if available, fallback to parsed.ext
const extension = realExtension || parsed.ext;
// Build clean name + extension
const nameWithExt = parsed.name + extension;
files.push({
name: entry.name, // original (may hide ext)
// nameWithExt: nameWithExt, // ALWAYS has correct ext
// displayName: nameWithExt, // use this in UI
// extension: extension.slice(1).toLowerCase() || null, // "jpg", null if none
// path: fullPath,
// inode: stats.ino ?? null,
// size: stats.size,
// mtime: stats.mtime.toISOString()
});
}
// Skip symlinks, sockets, etc. (or handle if needed)
} catch (err) {
console.warn(`Cannot access: ${fullPath}`, err.message);
}
}
}
await scan(DOWNLOADS);
return files;
}
export default getAllDownloadsFiles;