Something went wrong. Try again.
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677import { readdir, readFile, writeFile, stat, mkdir } from 'node:fs/promises';import { resolve, join, dirname, extname, basename, sep } from 'node:path';import { loadConfig } from './config.js';import { home } from './paths.js';
// File access for the browser UI. Paths are restricted to the configured// roots (the home directory by default) so a stray request cannot wander.
const MIME = { '.svg': 'image/svg+xml', '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.gif': 'image/gif', '.webp': 'image/webp', '.avif': 'image/avif', '.bmp': 'image/bmp', '.ttf': 'font/ttf', '.otf': 'font/otf', '.woff': 'font/woff', '.woff2': 'font/woff2', '.css': 'text/css', '.txt': 'text/plain', '.json': 'application/json', '.html': 'text/html',};export const mimeOf = p => MIME[extname(p).toLowerCase()] || 'application/octet-stream';
export async function allowed (p) { const cfg = await loadConfig(); const roots = (cfg.files.roots?.length ? cfg.files.roots : [home]).map(r => resolve(r)); const abs = resolve(p); return roots.some(r => abs === r || abs.startsWith(r + sep));}
function expand (p) { if (!p || p === '~') return home; if (p.startsWith('~/')) return join(home, p.slice(2)); return p;}
export async function list (path, filter) { const dir = resolve(expand(path)); if (!(await allowed(dir))) throw Object.assign(new Error('Path not allowed'), { status: 403 }); const entries = await readdir(dir, { withFileTypes: true }); const exts = filter === 'svg' ? ['.svg'] : filter === 'image' ? ['.png', '.jpg', '.jpeg', '.gif', '.webp', '.avif', '.bmp', '.svg'] : filter === 'font' ? ['.ttf', '.otf', '.woff', '.woff2'] : null; const out = []; for (const e of entries) { if (e.name.startsWith('.')) continue; const full = join(dir, e.name); if (e.isDirectory()) { out.push({ name: e.name, path: full, dir: true }); continue; } if (!e.isFile()) continue; const ext = extname(e.name).toLowerCase(); if (exts && !exts.includes(ext)) continue; let size = 0, mtime = 0; try { const s = await stat(full); size = s.size; mtime = s.mtimeMs; } catch { /* ignore */ } out.push({ name: e.name, path: full, dir: false, size, mtime, mime: mimeOf(full) }); } out.sort((a, b) => (a.dir === b.dir ? a.name.localeCompare(b.name, undefined, { sensitivity: 'base' }) : a.dir ? -1 : 1)); return { dir, parent: dirname(dir) === dir ? null : dirname(dir), entries: out };}
export async function read (path) { const abs = resolve(expand(path)); if (!(await allowed(abs))) throw Object.assign(new Error('Path not allowed'), { status: 403 }); const buf = await readFile(abs); const mime = mimeOf(abs); if (mime === 'image/svg+xml' || mime.startsWith('text/') || mime === 'application/json') { return { path: abs, name: basename(abs), mime, text: buf.toString('utf-8'), size: buf.length }; } return { path: abs, name: basename(abs), mime, base64: buf.toString('base64'), size: buf.length };}
export async function write (path, { text, base64 }) { const abs = resolve(expand(path)); if (!(await allowed(abs))) throw Object.assign(new Error('Path not allowed'), { status: 403 }); await mkdir(dirname(abs), { recursive: true }); const data = text != null ? Buffer.from(text, 'utf-8') : Buffer.from(base64 || '', 'base64'); await writeFile(abs, data); return { path: abs, name: basename(abs), size: data.length };}
export async function exists (path) { try { await stat(resolve(expand(path))); return true; } catch { return false; }}