All checks were successful
Build and Push Docker Image / docker (push) Successful in 9s
34 lines
965 B
JavaScript
34 lines
965 B
JavaScript
import fs from 'fs';
|
|
import path from 'path';
|
|
import mime from 'mime';
|
|
import { DATA_DIR as RAW_DATA_DIR } from '../../config.js';
|
|
|
|
// Resolve DATA_DIR once, ensure absolute path and normalized with trailing separator for prefix checks
|
|
const DATA_DIR = path.resolve(RAW_DATA_DIR);
|
|
const DATA_DIR_WITH_SEP = DATA_DIR.endsWith(path.sep) ? DATA_DIR : DATA_DIR + path.sep;
|
|
|
|
export function resolveSafePath(name) {
|
|
if (!name || typeof name !== 'string') return null;
|
|
// Prevent path traversal and normalize input
|
|
const joined = path.join(DATA_DIR, name);
|
|
const resolved = path.resolve(joined);
|
|
if (resolved === DATA_DIR || resolved.startsWith(DATA_DIR_WITH_SEP)) return resolved;
|
|
return null;
|
|
}
|
|
|
|
export function fileExists(p) {
|
|
try {
|
|
return fs.existsSync(p);
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function statFile(p) {
|
|
return fs.statSync(p);
|
|
}
|
|
|
|
export function getMimeType(p, fallback = 'audio/mpeg') {
|
|
return mime.getType(p) || fallback;
|
|
}
|