server_photo_s85js/utils/resolveFilePath.js.ok
2026-08-11 08:45:04 +02:00

64 lines
1.5 KiB
Text

const fs = require('fs');
const path = require('path');
const { WEB_ROOT } = require('../api_v1/config');
const db = require('../db/knex');
async function resolveFilePathForEntryId(entryId) {
try {
const row = await db('photos').where({ id: entryId }).first();
if (!row) return null;
const candidates = [
row.path,
row.file,
row.file_path,
row.original,
row.uri,
row.fullpath,
row.storage_path,
row.local_path,
];
for (let c of candidates) {
if (!c || typeof c !== 'string') continue;
c = c.trim();
if (path.isAbsolute(c) && fs.existsSync(c)) return c;
if (c.startsWith('/')) {
const p1 = path.join(__dirname, c);
if (fs.existsSync(p1)) return p1;
const p2 = path.join(__dirname, WEB_ROOT, c.replace(/^\/+/, ''));
if (fs.existsSync(p2)) return p2;
} else {
const p3 = path.join(__dirname, WEB_ROOT, c);
if (fs.existsSync(p3)) return p3;
const p4 = path.join(__dirname, c);
if (fs.existsSync(p4)) return p4;
}
}
if (row.user && row.cartella && (row.name || row.file)) {
const filename = row.name || row.file;
const p = path.join(
__dirname,
WEB_ROOT,
'photos',
row.user,
'original',
row.cartella,
filename
);
if (fs.existsSync(p)) return p;
}
return null;
} catch (err) {
console.error('resolveFilePathForEntryId error:', err);
return null;
}
}
module.exports = { resolveFilePathForEntryId };