BlueGit

timfa filemanager

Merge feature/persist-uploaded-at-metadata

timfa Tim Falken · pushed by timfa

Authored 2026-05-18 · 6 months ago

Hash
0e60dcb06f8cbeec3d612209d2fe58af526a5c61
4 files changed +147 −29
.github/technical-reference.md +3 −1
@@ -77,7 +77,7 @@
77 77 - GET /api/files/folder-icon - Returns the current icon source file for a folder ({icon:{filePath}} or {icon:null}); explicit assignment takes priority over auto-first.
78 78 - POST /api/files/folder-icon - Sets or clears a folder's explicit icon assignment; imagePath must have a cached thumbnail; write access required.
79 79 - GET /api/files/search - Fuzzy-searches all readable roots (and personal folder when configured) for files, returning ranked matches with path metadata.
80 - GET /api/files/list - Returns directory items including name/path/isDir plus size, mtime, and uploadedAt (creation-time approximation) used by client folder sorting/grouping.
80 - GET /api/files/list - Returns directory items including name/path/isDir plus size, mtime, and uploadedAt; uploadedAt comes from persisted `file_meta.uploaded_at` when present, otherwise it is backfilled from filesystem creation/change metadata and saved.
81 81 - GET /api/files/search-images - Fuzzy-searches readable roots for image files with ready thumbnails, returning capped results plus scan-truncation metadata for scalable global favicon selection.
82 82 - GET /api/files/global-favicon-image - Publicly serves the configured global favicon thumbnail file so browsers can load `<link rel="icon">` without auth headers.
83 83 - GET /api/files/global-favicon - Returns the configured global favicon source file path ({favicon:{filePath}} or {favicon:null}).
@@ -90,7 +90,9 @@
90 90 - pickNumberedPath(dir, requestedName) - Finds the first free `name (n).ext` path inside a directory.
91 91 - removePathRecursive(targetPath) - Deletes existing file or directory destination to support overwrite moves.
92 92 - movePathWithFallback(from, dest) - Moves files/folders with an EXDEV copy-then-delete fallback so cross-device moves into My Folder still succeed.
93 - rewriteMetaPaths(fromPath, toPath) - Rewrites metadata paths after rename/move while preserving all columns in `file_meta` (including uploaded_at, watched_by, hashes, and future columns).
93 94 - POST /api/files/upload - Streams multipart uploads into hidden temp files, computes/stores sha256, and enforces user-folder size limits.
95 - POST /api/files/upload - Stores authoritative upload timestamp in `file_meta.uploaded_at` at upload time; this timestamp persists across move/rename through metadata path rewrites.
94 96 - POST /api/files/move - Moves file/folder; supports conflict strategies: `error` (default), `overwrite`, or `rename` with optional `targetName`.
95 97 - POST /api/files/copy - Copies a file to a destination folder with conflict strategies (`error`, `overwrite`, `rename`) and user-folder size-limit checks.
96 98 - POST /api/files/video-stream - Creates a short-lived HLS session for a video and returns playlistUrl, durationSeconds, videoWidth, videoHeight, subtitleTracks, imageSubtitleTracks, and subtitleSummary ({total, converted, imageBased, skipped}).
@@ -77,7 +77,7 @@
77- GET /api/files/folder-icon - Returns the current icon source file for a folder ({icon:{filePath}} or {icon:null}); explicit assignment takes priority over auto-first.
77- GET /api/files/folder-icon - Returns the current icon source file for a folder ({icon:{filePath}} or {icon:null}); explicit assignment takes priority over auto-first.
78- POST /api/files/folder-icon - Sets or clears a folder's explicit icon assignment; imagePath must have a cached thumbnail; write access required.
78- POST /api/files/folder-icon - Sets or clears a folder's explicit icon assignment; imagePath must have a cached thumbnail; write access required.
79- GET /api/files/search - Fuzzy-searches all readable roots (and personal folder when configured) for files, returning ranked matches with path metadata.
79- GET /api/files/search - Fuzzy-searches all readable roots (and personal folder when configured) for files, returning ranked matches with path metadata.
80- GET /api/files/list - Returns directory items including name/path/isDir plus size, mtime, and uploadedAt (creation-time approximation) used by client folder sorting/grouping.
80- GET /api/files/list - Returns directory items including name/path/isDir plus size, mtime, and uploadedAt; uploadedAt comes from persisted `file_meta.uploaded_at` when present, otherwise it is backfilled from filesystem creation/change metadata and saved.
81- GET /api/files/search-images - Fuzzy-searches readable roots for image files with ready thumbnails, returning capped results plus scan-truncation metadata for scalable global favicon selection.
81- GET /api/files/search-images - Fuzzy-searches readable roots for image files with ready thumbnails, returning capped results plus scan-truncation metadata for scalable global favicon selection.
82- GET /api/files/global-favicon-image - Publicly serves the configured global favicon thumbnail file so browsers can load `<link rel="icon">` without auth headers.
82- GET /api/files/global-favicon-image - Publicly serves the configured global favicon thumbnail file so browsers can load `<link rel="icon">` without auth headers.
83- GET /api/files/global-favicon - Returns the configured global favicon source file path ({favicon:{filePath}} or {favicon:null}).
83- GET /api/files/global-favicon - Returns the configured global favicon source file path ({favicon:{filePath}} or {favicon:null}).
@@ -90,7 +90,9 @@
90- pickNumberedPath(dir, requestedName) - Finds the first free `name (n).ext` path inside a directory.
90- pickNumberedPath(dir, requestedName) - Finds the first free `name (n).ext` path inside a directory.
91- removePathRecursive(targetPath) - Deletes existing file or directory destination to support overwrite moves.
91- removePathRecursive(targetPath) - Deletes existing file or directory destination to support overwrite moves.
92- movePathWithFallback(from, dest) - Moves files/folders with an EXDEV copy-then-delete fallback so cross-device moves into My Folder still succeed.
92- movePathWithFallback(from, dest) - Moves files/folders with an EXDEV copy-then-delete fallback so cross-device moves into My Folder still succeed.
93- rewriteMetaPaths(fromPath, toPath) - Rewrites metadata paths after rename/move while preserving all columns in `file_meta` (including uploaded_at, watched_by, hashes, and future columns).
93- POST /api/files/upload - Streams multipart uploads into hidden temp files, computes/stores sha256, and enforces user-folder size limits.
94- POST /api/files/upload - Streams multipart uploads into hidden temp files, computes/stores sha256, and enforces user-folder size limits.
95- POST /api/files/upload - Stores authoritative upload timestamp in `file_meta.uploaded_at` at upload time; this timestamp persists across move/rename through metadata path rewrites.
94- POST /api/files/move - Moves file/folder; supports conflict strategies: `error` (default), `overwrite`, or `rename` with optional `targetName`.
96- POST /api/files/move - Moves file/folder; supports conflict strategies: `error` (default), `overwrite`, or `rename` with optional `targetName`.
95- POST /api/files/copy - Copies a file to a destination folder with conflict strategies (`error`, `overwrite`, `rename`) and user-folder size-limit checks.
97- POST /api/files/copy - Copies a file to a destination folder with conflict strategies (`error`, `overwrite`, `rename`) and user-folder size-limit checks.
96- POST /api/files/video-stream - Creates a short-lived HLS session for a video and returns playlistUrl, durationSeconds, videoWidth, videoHeight, subtitleTracks, imageSubtitleTracks, and subtitleSummary ({total, converted, imageBased, skipped}).
98- POST /api/files/video-stream - Creates a short-lived HLS session for a video and returns playlistUrl, durationSeconds, videoWidth, videoHeight, subtitleTracks, imageSubtitleTracks, and subtitleSummary ({total, converted, imageBased, skipped}).
server/db.js +7 −1
@@ -34,7 +34,8 @@ function initDb ()
34 34 file_path TEXT PRIMARY KEY,
35 35 uploader TEXT NOT NULL DEFAULT '',
36 36 downloaders TEXT NOT NULL DEFAULT '[]',
37 watched_by TEXT NOT NULL DEFAULT '{}'
37 watched_by TEXT NOT NULL DEFAULT '{}',
38 uploaded_at TEXT
38 39 );
39 40
40 41 CREATE TABLE IF NOT EXISTS auth_sessions (
@@ -79,6 +80,11 @@ function initDb ()
79 80 {
80 81 d.exec('CREATE INDEX IF NOT EXISTS idx_file_meta_sha256 ON file_meta(sha256)');
81 82 }
83
84 if (!colNames.includes('uploaded_at'))
85 {
86 d.exec('ALTER TABLE file_meta ADD COLUMN uploaded_at TEXT');
87 }
82 88 }
83 89
84 90 function closeDb ()
@@ -34,7 +34,8 @@ function initDb ()
34 file_path TEXT PRIMARY KEY,
34 file_path TEXT PRIMARY KEY,
35 uploader TEXT NOT NULL DEFAULT '',
35 uploader TEXT NOT NULL DEFAULT '',
36 downloaders TEXT NOT NULL DEFAULT '[]',
36 downloaders TEXT NOT NULL DEFAULT '[]',
37 watched_by TEXT NOT NULL DEFAULT '{}'
37 watched_by TEXT NOT NULL DEFAULT '{}',
38 uploaded_at TEXT
38 );
39 );
39
40
40 CREATE TABLE IF NOT EXISTS auth_sessions (
41 CREATE TABLE IF NOT EXISTS auth_sessions (
@@ -79,6 +80,11 @@ function initDb ()
79 {
80 {
80 d.exec('CREATE INDEX IF NOT EXISTS idx_file_meta_sha256 ON file_meta(sha256)');
81 d.exec('CREATE INDEX IF NOT EXISTS idx_file_meta_sha256 ON file_meta(sha256)');
81 }
82 }
83
84 if (!colNames.includes('uploaded_at'))
85 {
86 d.exec('ALTER TABLE file_meta ADD COLUMN uploaded_at TEXT');
87 }
82}
88}
83
89
84function closeDb ()
90function closeDb ()
server/routes/files.js +97 −24
@@ -682,23 +682,51 @@ function pathInsideResolved (child, root)
682 682 return !!relative && !relative.startsWith('..') && !path.isAbsolute(relative);
683 683 }
684 684
685 function uploadedAtFromStat (stat)
686 {
687 if (!stat || typeof stat !== 'object') return null;
688 const birthtimeMs = Number(stat.birthtimeMs);
689 const ctimeMs = Number(stat.ctimeMs);
690 const mtimeMs = Number(stat.mtimeMs);
691
692 const chosenMs = (Number.isFinite(birthtimeMs) && birthtimeMs > 0)
693 ? birthtimeMs
694 : (Number.isFinite(ctimeMs) && ctimeMs > 0)
695 ? ctimeMs
696 : (Number.isFinite(mtimeMs) && mtimeMs > 0)
697 ? mtimeMs
698 : null;
699
700 if (!Number.isFinite(chosenMs) || chosenMs <= 0) return null;
701 return new Date(chosenMs).toISOString();
702 }
703
685 704 function rewriteMetaPaths (fromPath, toPath)
686 705 {
687 706 const db = getDb();
688 const rows = db.prepare('SELECT file_path, uploader, downloaders, watched_by, sha256 FROM file_meta WHERE file_path = ? OR file_path LIKE ?')
707 const columns = db.prepare('PRAGMA table_info(file_meta)').all()
708 .map((col) => String(col.name || ''))
709 .filter(Boolean);
710
711 if (!columns.includes('file_path')) return;
712
713 const quotedColumns = columns.map((col) => `"${col}"`).join(', ');
714 const rows = db.prepare(`SELECT ${quotedColumns} FROM file_meta WHERE file_path = ? OR file_path LIKE ?`)
689 715 .all([fromPath, fromPath + path.sep + '%']);
690 716
691 717 if (!rows.length) return;
692 718
693 719 const del = db.prepare('DELETE FROM file_meta WHERE file_path = ?');
720 const placeholders = columns.map(() => '?').join(', ');
721 const updateAssignments = columns
722 .filter((col) => col !== 'file_path')
723 .map((col) => `"${col}" = excluded."${col}"`)
724 .join(',\n ');
694 725 const upsert = db.prepare(`
695 INSERT INTO file_meta (file_path, uploader, downloaders, watched_by, sha256)
696 VALUES (?, ?, ?, ?, ?)
726 INSERT INTO file_meta (${quotedColumns})
727 VALUES (${placeholders})
697 728 ON CONFLICT(file_path) DO UPDATE SET
698 uploader = excluded.uploader,
699 downloaders = excluded.downloaders,
700 watched_by = excluded.watched_by,
701 sha256 = excluded.sha256
729 ${updateAssignments}
702 730 `);
703 731
704 732 for (const row of rows)
@@ -707,7 +735,11 @@ function rewriteMetaPaths (fromPath, toPath)
707 735 ? toPath
708 736 : toPath + row.file_path.slice(fromPath.length);
709 737 del.run([row.file_path]);
710 upsert.run([nextPath, row.uploader, row.downloaders, row.watched_by || '{}', row.sha256 || null]);
738 const movedRow = {
739 ...row,
740 file_path: nextPath,
741 };
742 upsert.run(columns.map((col) => movedRow[col]));
711 743 }
712 744 }
713 745
@@ -1020,11 +1052,20 @@ router.get('/list', requireAuth, (req, res) =>
1020 1052 const stat = fs.statSync(fullPath);
1021 1053 size = stat.size;
1022 1054 mtime = stat.mtime.toISOString();
1023 // Approximate upload time from file creation metadata when available.
1024 const uploadDate = (stat.birthtime && Number.isFinite(stat.birthtimeMs) && stat.birthtimeMs > 0)
1025 ? stat.birthtime
1026 : stat.ctime;
1027 uploadedAt = uploadDate instanceof Date ? uploadDate.toISOString() : null;
1055
1056 const db = getDb();
1057 const existing = db.prepare('SELECT uploaded_at FROM file_meta WHERE file_path = ?').get([fullPath]);
1058 uploadedAt = String(existing && existing.uploaded_at || '').trim() || uploadedAtFromStat(stat);
1059
1060 if (uploadedAt)
1061 {
1062 db.prepare(`
1063 INSERT INTO file_meta (file_path, uploader, downloaders, watched_by, uploaded_at)
1064 VALUES (?, ?, ?, ?, ?)
1065 ON CONFLICT(file_path) DO UPDATE SET
1066 uploaded_at = COALESCE(NULLIF(file_meta.uploaded_at, ''), excluded.uploaded_at)
1067 `).run([fullPath, '', '[]', '{}', uploadedAt]);
1068 }
1028 1069 } catch { /* ignore */ }
1029 1070
1030 1071 return {
@@ -1133,6 +1174,22 @@ router.get('/meta', requireAuth, async (req, res) =>
1133 1174
1134 1175 const db = getDb();
1135 1176 let meta = db.prepare('SELECT * FROM file_meta WHERE file_path = ?').get([file]);
1177 let uploadedAt = meta ? String(meta.uploaded_at || '').trim() : '';
1178 if (!uploadedAt)
1179 {
1180 uploadedAt = uploadedAtFromStat(stat) || '';
1181 if (uploadedAt)
1182 {
1183 db.prepare(`
1184 INSERT INTO file_meta (file_path, uploader, downloaders, watched_by, uploaded_at)
1185 VALUES (?, ?, ?, ?, ?)
1186 ON CONFLICT(file_path) DO UPDATE SET
1187 uploaded_at = COALESCE(NULLIF(file_meta.uploaded_at, ''), excluded.uploaded_at)
1188 `).run([file, '', '[]', '{}', uploadedAt]);
1189
1190 meta = db.prepare('SELECT * FROM file_meta WHERE file_path = ?').get([file]);
1191 }
1192 }
1136 1193
1137 1194 // Lazily compute and persist the SHA-256 hash when it is absent.
1138 1195 let sha256 = meta ? (meta.sha256 || null) : null;
@@ -1146,8 +1203,8 @@ router.get('/meta', requireAuth, async (req, res) =>
1146 1203 db.prepare('UPDATE file_meta SET sha256 = ? WHERE file_path = ?').run([sha256, file]);
1147 1204 } else
1148 1205 {
1149 db.prepare('INSERT INTO file_meta (file_path, uploader, downloaders, sha256) VALUES (?, ?, ?, ?)')
1150 .run([file, '', '[]', sha256]);
1206 db.prepare('INSERT INTO file_meta (file_path, uploader, downloaders, watched_by, uploaded_at, sha256) VALUES (?, ?, ?, ?, ?, ?)')
1207 .run([file, '', '[]', '{}', uploadedAt || uploadedAtFromStat(stat), sha256]);
1151 1208 meta = db.prepare('SELECT * FROM file_meta WHERE file_path = ?').get([file]);
1152 1209 }
1153 1210 } catch
@@ -1160,6 +1217,7 @@ router.get('/meta', requireAuth, async (req, res) =>
1160 1217 name: path.basename(file),
1161 1218 size: stat.size,
1162 1219 mtime: stat.mtime.toISOString(),
1220 uploadedAt: uploadedAt || null,
1163 1221 uploader: meta ? meta.uploader : '',
1164 1222 downloaders: meta ? parseJsonArray(meta.downloaders) : [],
1165 1223 watchedBy: meta ? parseJsonObject(meta.watched_by) : {},
@@ -1278,6 +1336,11 @@ router.get('/download', requireAuth, (req, res) =>
1278 1336 // Record download
1279 1337 const db = getDb();
1280 1338 const meta = db.prepare('SELECT * FROM file_meta WHERE file_path = ?').get([file]);
1339 const uploadedAt = (() =>
1340 {
1341 try { return uploadedAtFromStat(fs.statSync(file)); }
1342 catch { return null; }
1343 })();
1281 1344 if (meta)
1282 1345 {
1283 1346 const downloaders = parseJsonArray(meta.downloaders);
@@ -1289,8 +1352,8 @@ router.get('/download', requireAuth, (req, res) =>
1289 1352 }
1290 1353 } else
1291 1354 {
1292 db.prepare('INSERT INTO file_meta (file_path, uploader, downloaders, watched_by) VALUES (?, ?, ?, ?)')
1293 .run([file, '', JSON.stringify([req.user.username]), '{}']);
1355 db.prepare('INSERT INTO file_meta (file_path, uploader, downloaders, watched_by, uploaded_at) VALUES (?, ?, ?, ?, ?)')
1356 .run([file, '', JSON.stringify([req.user.username]), '{}', uploadedAt]);
1294 1357 }
1295 1358
1296 1359 res.download(file, path.basename(file));
@@ -1367,7 +1430,16 @@ router.post('/watch-progress', requireAuth, (req, res) =>
1367 1430 return res.status(403).json({ error: 'Access denied.' });
1368 1431 }
1369 1432
1370 if (!fs.existsSync(filePath) || fs.statSync(filePath).isDirectory())
1433 let fileStat;
1434 try
1435 {
1436 fileStat = fs.statSync(filePath);
1437 } catch
1438 {
1439 return res.status(404).json({ error: 'File not found.' });
1440 }
1441
1442 if (fileStat.isDirectory())
1371 1443 {
1372 1444 return res.status(404).json({ error: 'File not found.' });
1373 1445 }
@@ -1389,8 +1461,8 @@ router.post('/watch-progress', requireAuth, (req, res) =>
1389 1461 .run([JSON.stringify(watchedBy), filePath]);
1390 1462 } else
1391 1463 {
1392 db.prepare('INSERT INTO file_meta (file_path, uploader, downloaders, watched_by) VALUES (?, ?, ?, ?)')
1393 .run([filePath, '', '[]', JSON.stringify(watchedBy)]);
1464 db.prepare('INSERT INTO file_meta (file_path, uploader, downloaders, watched_by, uploaded_at) VALUES (?, ?, ?, ?, ?)')
1465 .run([filePath, '', '[]', JSON.stringify(watchedBy), uploadedAtFromStat(fileStat)]);
1394 1466 }
1395 1467
1396 1468 res.json({ ok: true, updated: true, status: next });
@@ -1657,6 +1729,7 @@ router.post('/upload', requireAuth, (req, res, next) =>
1657 1729 for (const f of (req.files || []))
1658 1730 {
1659 1731 const filePath = path.join(dir, f.originalname);
1732 const uploadedAt = new Date().toISOString();
1660 1733
1661 1734 let sha256 = null;
1662 1735 try
@@ -1670,12 +1743,12 @@ router.post('/upload', requireAuth, (req, res, next) =>
1670 1743 const existing = db.prepare('SELECT * FROM file_meta WHERE file_path = ?').get([filePath]);
1671 1744 if (existing)
1672 1745 {
1673 db.prepare('UPDATE file_meta SET uploader = ?, sha256 = ? WHERE file_path = ?')
1674 .run([req.user.username, sha256, filePath]);
1746 db.prepare('UPDATE file_meta SET uploader = ?, sha256 = ?, uploaded_at = ? WHERE file_path = ?')
1747 .run([req.user.username, sha256, uploadedAt, filePath]);
1675 1748 } else
1676 1749 {
1677 db.prepare('INSERT INTO file_meta (file_path, uploader, downloaders, sha256) VALUES (?, ?, ?, ?)')
1678 .run([filePath, req.user.username, '[]', sha256]);
1750 db.prepare('INSERT INTO file_meta (file_path, uploader, downloaders, watched_by, uploaded_at, sha256) VALUES (?, ?, ?, ?, ?, ?)')
1751 .run([filePath, req.user.username, '[]', '{}', uploadedAt, sha256]);
1679 1752 }
1680 1753 }
1681 1754 res.json({ uploaded: (req.files || []).map(f => f.originalname) });
@@ -682,23 +682,51 @@ function pathInsideResolved (child, root)
682 return !!relative && !relative.startsWith('..') && !path.isAbsolute(relative);
682 return !!relative && !relative.startsWith('..') && !path.isAbsolute(relative);
683}
683}
684
684
685function uploadedAtFromStat (stat)
686{
687 if (!stat || typeof stat !== 'object') return null;
688 const birthtimeMs = Number(stat.birthtimeMs);
689 const ctimeMs = Number(stat.ctimeMs);
690 const mtimeMs = Number(stat.mtimeMs);
691
692 const chosenMs = (Number.isFinite(birthtimeMs) && birthtimeMs > 0)
693 ? birthtimeMs
694 : (Number.isFinite(ctimeMs) && ctimeMs > 0)
695 ? ctimeMs
696 : (Number.isFinite(mtimeMs) && mtimeMs > 0)
697 ? mtimeMs
698 : null;
699
700 if (!Number.isFinite(chosenMs) || chosenMs <= 0) return null;
701 return new Date(chosenMs).toISOString();
702}
703
685function rewriteMetaPaths (fromPath, toPath)
704function rewriteMetaPaths (fromPath, toPath)
686{
705{
687 const db = getDb();
706 const db = getDb();
688 const rows = db.prepare('SELECT file_path, uploader, downloaders, watched_by, sha256 FROM file_meta WHERE file_path = ? OR file_path LIKE ?')
707 const columns = db.prepare('PRAGMA table_info(file_meta)').all()
708 .map((col) => String(col.name || ''))
709 .filter(Boolean);
710
711 if (!columns.includes('file_path')) return;
712
713 const quotedColumns = columns.map((col) => `"${col}"`).join(', ');
714 const rows = db.prepare(`SELECT ${quotedColumns} FROM file_meta WHERE file_path = ? OR file_path LIKE ?`)
689 .all([fromPath, fromPath + path.sep + '%']);
715 .all([fromPath, fromPath + path.sep + '%']);
690
716
691 if (!rows.length) return;
717 if (!rows.length) return;
692
718
693 const del = db.prepare('DELETE FROM file_meta WHERE file_path = ?');
719 const del = db.prepare('DELETE FROM file_meta WHERE file_path = ?');
720 const placeholders = columns.map(() => '?').join(', ');
721 const updateAssignments = columns
722 .filter((col) => col !== 'file_path')
723 .map((col) => `"${col}" = excluded."${col}"`)
724 .join(',\n ');
694 const upsert = db.prepare(`
725 const upsert = db.prepare(`
695 INSERT INTO file_meta (file_path, uploader, downloaders, watched_by, sha256)
726 INSERT INTO file_meta (${quotedColumns})
696 VALUES (?, ?, ?, ?, ?)
727 VALUES (${placeholders})
697 ON CONFLICT(file_path) DO UPDATE SET
728 ON CONFLICT(file_path) DO UPDATE SET
698 uploader = excluded.uploader,
729 ${updateAssignments}
699 downloaders = excluded.downloaders,
700 watched_by = excluded.watched_by,
701 sha256 = excluded.sha256
702 `);
730 `);
703
731
704 for (const row of rows)
732 for (const row of rows)
@@ -707,7 +735,11 @@ function rewriteMetaPaths (fromPath, toPath)
707 ? toPath
735 ? toPath
708 : toPath + row.file_path.slice(fromPath.length);
736 : toPath + row.file_path.slice(fromPath.length);
709 del.run([row.file_path]);
737 del.run([row.file_path]);
710 upsert.run([nextPath, row.uploader, row.downloaders, row.watched_by || '{}', row.sha256 || null]);
738 const movedRow = {
739 ...row,
740 file_path: nextPath,
741 };
742 upsert.run(columns.map((col) => movedRow[col]));
711 }
743 }
712}
744}
713
745
@@ -1020,11 +1052,20 @@ router.get('/list', requireAuth, (req, res) =>
1020 const stat = fs.statSync(fullPath);
1052 const stat = fs.statSync(fullPath);
1021 size = stat.size;
1053 size = stat.size;
1022 mtime = stat.mtime.toISOString();
1054 mtime = stat.mtime.toISOString();
1023 // Approximate upload time from file creation metadata when available.
1055
1024 const uploadDate = (stat.birthtime && Number.isFinite(stat.birthtimeMs) && stat.birthtimeMs > 0)
1056 const db = getDb();
1025 ? stat.birthtime
1057 const existing = db.prepare('SELECT uploaded_at FROM file_meta WHERE file_path = ?').get([fullPath]);
1026 : stat.ctime;
1058 uploadedAt = String(existing && existing.uploaded_at || '').trim() || uploadedAtFromStat(stat);
1027 uploadedAt = uploadDate instanceof Date ? uploadDate.toISOString() : null;
1059
1060 if (uploadedAt)
1061 {
1062 db.prepare(`
1063 INSERT INTO file_meta (file_path, uploader, downloaders, watched_by, uploaded_at)
1064 VALUES (?, ?, ?, ?, ?)
1065 ON CONFLICT(file_path) DO UPDATE SET
1066 uploaded_at = COALESCE(NULLIF(file_meta.uploaded_at, ''), excluded.uploaded_at)
1067 `).run([fullPath, '', '[]', '{}', uploadedAt]);
1068 }
1028 } catch { /* ignore */ }
1069 } catch { /* ignore */ }
1029
1070
1030 return {
1071 return {
@@ -1133,6 +1174,22 @@ router.get('/meta', requireAuth, async (req, res) =>
1133
1174
1134 const db = getDb();
1175 const db = getDb();
1135 let meta = db.prepare('SELECT * FROM file_meta WHERE file_path = ?').get([file]);
1176 let meta = db.prepare('SELECT * FROM file_meta WHERE file_path = ?').get([file]);
1177 let uploadedAt = meta ? String(meta.uploaded_at || '').trim() : '';
1178 if (!uploadedAt)
1179 {
1180 uploadedAt = uploadedAtFromStat(stat) || '';
1181 if (uploadedAt)
1182 {
1183 db.prepare(`
1184 INSERT INTO file_meta (file_path, uploader, downloaders, watched_by, uploaded_at)
1185 VALUES (?, ?, ?, ?, ?)
1186 ON CONFLICT(file_path) DO UPDATE SET
1187 uploaded_at = COALESCE(NULLIF(file_meta.uploaded_at, ''), excluded.uploaded_at)
1188 `).run([file, '', '[]', '{}', uploadedAt]);
1189
1190 meta = db.prepare('SELECT * FROM file_meta WHERE file_path = ?').get([file]);
1191 }
1192 }
1136
1193
1137 // Lazily compute and persist the SHA-256 hash when it is absent.
1194 // Lazily compute and persist the SHA-256 hash when it is absent.
1138 let sha256 = meta ? (meta.sha256 || null) : null;
1195 let sha256 = meta ? (meta.sha256 || null) : null;
@@ -1146,8 +1203,8 @@ router.get('/meta', requireAuth, async (req, res) =>
1146 db.prepare('UPDATE file_meta SET sha256 = ? WHERE file_path = ?').run([sha256, file]);
1203 db.prepare('UPDATE file_meta SET sha256 = ? WHERE file_path = ?').run([sha256, file]);
1147 } else
1204 } else
1148 {
1205 {
1149 db.prepare('INSERT INTO file_meta (file_path, uploader, downloaders, sha256) VALUES (?, ?, ?, ?)')
1206 db.prepare('INSERT INTO file_meta (file_path, uploader, downloaders, watched_by, uploaded_at, sha256) VALUES (?, ?, ?, ?, ?, ?)')
1150 .run([file, '', '[]', sha256]);
1207 .run([file, '', '[]', '{}', uploadedAt || uploadedAtFromStat(stat), sha256]);
1151 meta = db.prepare('SELECT * FROM file_meta WHERE file_path = ?').get([file]);
1208 meta = db.prepare('SELECT * FROM file_meta WHERE file_path = ?').get([file]);
1152 }
1209 }
1153 } catch
1210 } catch
@@ -1160,6 +1217,7 @@ router.get('/meta', requireAuth, async (req, res) =>
1160 name: path.basename(file),
1217 name: path.basename(file),
1161 size: stat.size,
1218 size: stat.size,
1162 mtime: stat.mtime.toISOString(),
1219 mtime: stat.mtime.toISOString(),
1220 uploadedAt: uploadedAt || null,
1163 uploader: meta ? meta.uploader : '',
1221 uploader: meta ? meta.uploader : '',
1164 downloaders: meta ? parseJsonArray(meta.downloaders) : [],
1222 downloaders: meta ? parseJsonArray(meta.downloaders) : [],
1165 watchedBy: meta ? parseJsonObject(meta.watched_by) : {},
1223 watchedBy: meta ? parseJsonObject(meta.watched_by) : {},
@@ -1278,6 +1336,11 @@ router.get('/download', requireAuth, (req, res) =>
1278 // Record download
1336 // Record download
1279 const db = getDb();
1337 const db = getDb();
1280 const meta = db.prepare('SELECT * FROM file_meta WHERE file_path = ?').get([file]);
1338 const meta = db.prepare('SELECT * FROM file_meta WHERE file_path = ?').get([file]);
1339 const uploadedAt = (() =>
1340 {
1341 try { return uploadedAtFromStat(fs.statSync(file)); }
1342 catch { return null; }
1343 })();
1281 if (meta)
1344 if (meta)
1282 {
1345 {
1283 const downloaders = parseJsonArray(meta.downloaders);
1346 const downloaders = parseJsonArray(meta.downloaders);
@@ -1289,8 +1352,8 @@ router.get('/download', requireAuth, (req, res) =>
1289 }
1352 }
1290 } else
1353 } else
1291 {
1354 {
1292 db.prepare('INSERT INTO file_meta (file_path, uploader, downloaders, watched_by) VALUES (?, ?, ?, ?)')
1355 db.prepare('INSERT INTO file_meta (file_path, uploader, downloaders, watched_by, uploaded_at) VALUES (?, ?, ?, ?, ?)')
1293 .run([file, '', JSON.stringify([req.user.username]), '{}']);
1356 .run([file, '', JSON.stringify([req.user.username]), '{}', uploadedAt]);
1294 }
1357 }
1295
1358
1296 res.download(file, path.basename(file));
1359 res.download(file, path.basename(file));
@@ -1367,7 +1430,16 @@ router.post('/watch-progress', requireAuth, (req, res) =>
1367 return res.status(403).json({ error: 'Access denied.' });
1430 return res.status(403).json({ error: 'Access denied.' });
1368 }
1431 }
1369
1432
1370 if (!fs.existsSync(filePath) || fs.statSync(filePath).isDirectory())
1433 let fileStat;
1434 try
1435 {
1436 fileStat = fs.statSync(filePath);
1437 } catch
1438 {
1439 return res.status(404).json({ error: 'File not found.' });
1440 }
1441
1442 if (fileStat.isDirectory())
1371 {
1443 {
1372 return res.status(404).json({ error: 'File not found.' });
1444 return res.status(404).json({ error: 'File not found.' });
1373 }
1445 }
@@ -1389,8 +1461,8 @@ router.post('/watch-progress', requireAuth, (req, res) =>
1389 .run([JSON.stringify(watchedBy), filePath]);
1461 .run([JSON.stringify(watchedBy), filePath]);
1390 } else
1462 } else
1391 {
1463 {
1392 db.prepare('INSERT INTO file_meta (file_path, uploader, downloaders, watched_by) VALUES (?, ?, ?, ?)')
1464 db.prepare('INSERT INTO file_meta (file_path, uploader, downloaders, watched_by, uploaded_at) VALUES (?, ?, ?, ?, ?)')
1393 .run([filePath, '', '[]', JSON.stringify(watchedBy)]);
1465 .run([filePath, '', '[]', JSON.stringify(watchedBy), uploadedAtFromStat(fileStat)]);
1394 }
1466 }
1395
1467
1396 res.json({ ok: true, updated: true, status: next });
1468 res.json({ ok: true, updated: true, status: next });
@@ -1657,6 +1729,7 @@ router.post('/upload', requireAuth, (req, res, next) =>
1657 for (const f of (req.files || []))
1729 for (const f of (req.files || []))
1658 {
1730 {
1659 const filePath = path.join(dir, f.originalname);
1731 const filePath = path.join(dir, f.originalname);
1732 const uploadedAt = new Date().toISOString();
1660
1733
1661 let sha256 = null;
1734 let sha256 = null;
1662 try
1735 try
@@ -1670,12 +1743,12 @@ router.post('/upload', requireAuth, (req, res, next) =>
1670 const existing = db.prepare('SELECT * FROM file_meta WHERE file_path = ?').get([filePath]);
1743 const existing = db.prepare('SELECT * FROM file_meta WHERE file_path = ?').get([filePath]);
1671 if (existing)
1744 if (existing)
1672 {
1745 {
1673 db.prepare('UPDATE file_meta SET uploader = ?, sha256 = ? WHERE file_path = ?')
1746 db.prepare('UPDATE file_meta SET uploader = ?, sha256 = ?, uploaded_at = ? WHERE file_path = ?')
1674 .run([req.user.username, sha256, filePath]);
1747 .run([req.user.username, sha256, uploadedAt, filePath]);
1675 } else
1748 } else
1676 {
1749 {
1677 db.prepare('INSERT INTO file_meta (file_path, uploader, downloaders, sha256) VALUES (?, ?, ?, ?)')
1750 db.prepare('INSERT INTO file_meta (file_path, uploader, downloaders, watched_by, uploaded_at, sha256) VALUES (?, ?, ?, ?, ?, ?)')
1678 .run([filePath, req.user.username, '[]', sha256]);
1751 .run([filePath, req.user.username, '[]', '{}', uploadedAt, sha256]);
1679 }
1752 }
1680 }
1753 }
1681 res.json({ uploaded: (req.files || []).map(f => f.originalname) });
1754 res.json({ uploaded: (req.files || []).map(f => f.originalname) });
tests/files.routes.test.js +40 −3
@@ -44,6 +44,7 @@ jest.mock('../server/db', () =>
44 44 uploader TEXT NOT NULL DEFAULT '',
45 45 downloaders TEXT NOT NULL DEFAULT '[]',
46 46 watched_by TEXT NOT NULL DEFAULT '{}',
47 uploaded_at TEXT,
47 48 sha256 TEXT
48 49 );
49 50 CREATE INDEX IF NOT EXISTS idx_file_meta_sha256 ON file_meta(sha256);
@@ -360,9 +361,10 @@ describe('POST /api/files/move', () =>
360 361 const toDir = path.join(rootDir, 'folder');
361 362 fs.writeFileSync(from, 'hello');
362 363 fs.mkdirSync(toDir);
364 const uploadedAt = '2026-05-18T12:00:00.000Z';
363 365
364 getDb().prepare('INSERT INTO file_meta (file_path, uploader, downloaders) VALUES (?, ?, ?)')
365 .run([from, 'admin', '["alice"]']);
366 getDb().prepare('INSERT INTO file_meta (file_path, uploader, downloaders, watched_by, uploaded_at, sha256) VALUES (?, ?, ?, ?, ?, ?)')
367 .run([from, 'admin', '["alice"]', '{"admin":"partway"}', uploadedAt, 'abc123']);
366 368
367 369 const { status, body } = await req('POST', '/api/files/move', { from, toDir });
368 370
@@ -372,7 +374,13 @@ describe('POST /api/files/move', () =>
372 374 expect(fs.existsSync(path.join(toDir, 'alpha.txt'))).toBe(true);
373 375
374 376 const meta = getDb().prepare('SELECT * FROM file_meta WHERE file_path = ?').get([path.join(toDir, 'alpha.txt')]);
375 expect(meta).toMatchObject({ uploader: 'admin', downloaders: '["alice"]' });
377 expect(meta).toMatchObject({
378 uploader: 'admin',
379 downloaders: '["alice"]',
380 watched_by: '{"admin":"partway"}',
381 uploaded_at: uploadedAt,
382 sha256: 'abc123',
383 });
376 384 });
377 385
378 386 test('moves a folder into another folder and rewrites descendant metadata', async () =>
@@ -1121,6 +1129,19 @@ describe('SHA-256 hashing and duplicate detection', () =>
1121 1129 expect(meta.sha256).toMatch(/^[0-9a-f]{64}$/);
1122 1130 });
1123 1131
1132 test('POST /api/files/upload stores uploaded_at for uploaded files', async () =>
1133 {
1134 const { status } = await reqMultipart(`/api/files/upload?dir=${encodeURIComponent(rootDir)}`, [
1135 { name: 'uploaded-at.txt', content: 'hello', type: 'text/plain' },
1136 ]);
1137 expect(status).toBe(200);
1138
1139 const meta = getDb().prepare('SELECT uploaded_at FROM file_meta WHERE file_path = ?')
1140 .get([path.join(rootDir, 'uploaded-at.txt')]);
1141 expect(meta).toBeTruthy();
1142 expect(String(meta.uploaded_at || '')).toMatch(/^\d{4}-\d{2}-\d{2}T/);
1143 });
1144
1124 1145 test('GET /api/files/meta returns sha256 and computes it lazily when missing', async () =>
1125 1146 {
1126 1147 const filePath = path.join(rootDir, 'lazy.txt');
@@ -1139,6 +1160,22 @@ describe('SHA-256 hashing and duplicate detection', () =>
1139 1160 expect(row.sha256).toBe(body.sha256);
1140 1161 });
1141 1162
1163 test('GET /api/files/meta backfills uploaded_at when missing', async () =>
1164 {
1165 const filePath = path.join(rootDir, 'legacy.txt');
1166 fs.writeFileSync(filePath, 'legacy');
1167
1168 getDb().prepare('INSERT INTO file_meta (file_path, uploader, downloaders) VALUES (?, ?, ?)')
1169 .run([filePath, 'admin', '[]']);
1170
1171 const { status, body } = await req('GET', `/api/files/meta?file=${encodeURIComponent(filePath)}`);
1172 expect(status).toBe(200);
1173 expect(String(body.uploadedAt || '')).toMatch(/^\d{4}-\d{2}-\d{2}T/);
1174
1175 const row = getDb().prepare('SELECT uploaded_at FROM file_meta WHERE file_path = ?').get([filePath]);
1176 expect(String(row.uploaded_at || '')).toBe(String(body.uploadedAt || ''));
1177 });
1178
1142 1179 test('GET /api/files/duplicates returns other files with the same hash', async () =>
1143 1180 {
1144 1181 const content = 'duplicate content';
@@ -44,6 +44,7 @@ jest.mock('../server/db', () =>
44 uploader TEXT NOT NULL DEFAULT '',
44 uploader TEXT NOT NULL DEFAULT '',
45 downloaders TEXT NOT NULL DEFAULT '[]',
45 downloaders TEXT NOT NULL DEFAULT '[]',
46 watched_by TEXT NOT NULL DEFAULT '{}',
46 watched_by TEXT NOT NULL DEFAULT '{}',
47 uploaded_at TEXT,
47 sha256 TEXT
48 sha256 TEXT
48 );
49 );
49 CREATE INDEX IF NOT EXISTS idx_file_meta_sha256 ON file_meta(sha256);
50 CREATE INDEX IF NOT EXISTS idx_file_meta_sha256 ON file_meta(sha256);
@@ -360,9 +361,10 @@ describe('POST /api/files/move', () =>
360 const toDir = path.join(rootDir, 'folder');
361 const toDir = path.join(rootDir, 'folder');
361 fs.writeFileSync(from, 'hello');
362 fs.writeFileSync(from, 'hello');
362 fs.mkdirSync(toDir);
363 fs.mkdirSync(toDir);
364 const uploadedAt = '2026-05-18T12:00:00.000Z';
363
365
364 getDb().prepare('INSERT INTO file_meta (file_path, uploader, downloaders) VALUES (?, ?, ?)')
366 getDb().prepare('INSERT INTO file_meta (file_path, uploader, downloaders, watched_by, uploaded_at, sha256) VALUES (?, ?, ?, ?, ?, ?)')
365 .run([from, 'admin', '["alice"]']);
367 .run([from, 'admin', '["alice"]', '{"admin":"partway"}', uploadedAt, 'abc123']);
366
368
367 const { status, body } = await req('POST', '/api/files/move', { from, toDir });
369 const { status, body } = await req('POST', '/api/files/move', { from, toDir });
368
370
@@ -372,7 +374,13 @@ describe('POST /api/files/move', () =>
372 expect(fs.existsSync(path.join(toDir, 'alpha.txt'))).toBe(true);
374 expect(fs.existsSync(path.join(toDir, 'alpha.txt'))).toBe(true);
373
375
374 const meta = getDb().prepare('SELECT * FROM file_meta WHERE file_path = ?').get([path.join(toDir, 'alpha.txt')]);
376 const meta = getDb().prepare('SELECT * FROM file_meta WHERE file_path = ?').get([path.join(toDir, 'alpha.txt')]);
375 expect(meta).toMatchObject({ uploader: 'admin', downloaders: '["alice"]' });
377 expect(meta).toMatchObject({
378 uploader: 'admin',
379 downloaders: '["alice"]',
380 watched_by: '{"admin":"partway"}',
381 uploaded_at: uploadedAt,
382 sha256: 'abc123',
383 });
376 });
384 });
377
385
378 test('moves a folder into another folder and rewrites descendant metadata', async () =>
386 test('moves a folder into another folder and rewrites descendant metadata', async () =>
@@ -1121,6 +1129,19 @@ describe('SHA-256 hashing and duplicate detection', () =>
1121 expect(meta.sha256).toMatch(/^[0-9a-f]{64}$/);
1129 expect(meta.sha256).toMatch(/^[0-9a-f]{64}$/);
1122 });
1130 });
1123
1131
1132 test('POST /api/files/upload stores uploaded_at for uploaded files', async () =>
1133 {
1134 const { status } = await reqMultipart(`/api/files/upload?dir=${encodeURIComponent(rootDir)}`, [
1135 { name: 'uploaded-at.txt', content: 'hello', type: 'text/plain' },
1136 ]);
1137 expect(status).toBe(200);
1138
1139 const meta = getDb().prepare('SELECT uploaded_at FROM file_meta WHERE file_path = ?')
1140 .get([path.join(rootDir, 'uploaded-at.txt')]);
1141 expect(meta).toBeTruthy();
1142 expect(String(meta.uploaded_at || '')).toMatch(/^\d{4}-\d{2}-\d{2}T/);
1143 });
1144
1124 test('GET /api/files/meta returns sha256 and computes it lazily when missing', async () =>
1145 test('GET /api/files/meta returns sha256 and computes it lazily when missing', async () =>
1125 {
1146 {
1126 const filePath = path.join(rootDir, 'lazy.txt');
1147 const filePath = path.join(rootDir, 'lazy.txt');
@@ -1139,6 +1160,22 @@ describe('SHA-256 hashing and duplicate detection', () =>
1139 expect(row.sha256).toBe(body.sha256);
1160 expect(row.sha256).toBe(body.sha256);
1140 });
1161 });
1141
1162
1163 test('GET /api/files/meta backfills uploaded_at when missing', async () =>
1164 {
1165 const filePath = path.join(rootDir, 'legacy.txt');
1166 fs.writeFileSync(filePath, 'legacy');
1167
1168 getDb().prepare('INSERT INTO file_meta (file_path, uploader, downloaders) VALUES (?, ?, ?)')
1169 .run([filePath, 'admin', '[]']);
1170
1171 const { status, body } = await req('GET', `/api/files/meta?file=${encodeURIComponent(filePath)}`);
1172 expect(status).toBe(200);
1173 expect(String(body.uploadedAt || '')).toMatch(/^\d{4}-\d{2}-\d{2}T/);
1174
1175 const row = getDb().prepare('SELECT uploaded_at FROM file_meta WHERE file_path = ?').get([filePath]);
1176 expect(String(row.uploaded_at || '')).toBe(String(body.uploadedAt || ''));
1177 });
1178
1142 test('GET /api/files/duplicates returns other files with the same hash', async () =>
1179 test('GET /api/files/duplicates returns other files with the same hash', async () =>
1143 {
1180 {
1144 const content = 'duplicate content';
1181 const content = 'duplicate content';