Adds project_files.size (schema, idempotent migration, ensureFileSizesColumn), records multer size on both upload paths, backfills legacy rows from storage at startup and after restore (lib/file-sizes.js) with a lazy per-row update on read. GET /api/files and /api/files/detached accept min_size/max_size and a whitelisted sort (new/old/size_desc/size_asc); files page gets size and sort selects. size is carried through backup normalize/restore.
24 lines
870 B
JavaScript
24 lines
870 B
JavaScript
async function ensureProjectFileSizeColumn(pool) {
|
|
await pool.query('ALTER TABLE project_files ADD COLUMN IF NOT EXISTS size BIGINT');
|
|
}
|
|
|
|
async function backfillProjectFileSizes(pool, storage) {
|
|
const { rows } = await pool.query('SELECT id, path FROM project_files WHERE size IS NULL ORDER BY id');
|
|
if (!rows.length) return 0;
|
|
const objects = await storage.listAll('');
|
|
const sizes = new Map(objects.map(o => [o.key, o.size || 0]));
|
|
let updated = 0;
|
|
for (const row of rows) {
|
|
const key = storage.keyFromPath(row.path);
|
|
const size = key && sizes.has(key) ? sizes.get(key) : 0;
|
|
const { rowCount } = await pool.query(
|
|
'UPDATE project_files SET size = $1 WHERE id = $2 AND size IS NULL',
|
|
[size, row.id]
|
|
);
|
|
updated += rowCount;
|
|
}
|
|
return updated;
|
|
}
|
|
|
|
module.exports = { ensureProjectFileSizeColumn, backfillProjectFileSizes };
|