BlueGit

timfa filemanager

Add bulk watched-status API endpoint

timfa Tim Falken · pushed by timfa

Authored 2026-05-15 · 6 months ago

Hash
70917e69ecf22170dd47ba19bcf5082d2e722890
Parent
6a9f5570
2 files changed +88 −0
server/routes/files.js +53 −0
@@ -468,6 +468,12 @@ function watchStatusForProgress (progress)
468 468 return null;
469 469 }
470 470
471 function normalisedWatchedStatus (value)
472 {
473 const status = String(value || '').toLowerCase();
474 return status === 'fully' || status === 'partway' ? status : null;
475 }
476
471 477 function splitNameExt (name)
472 478 {
473 479 const dot = String(name || '').lastIndexOf('.');
@@ -941,6 +947,53 @@ router.get('/download', requireAuth, (req, res) =>
941 947 res.download(file, path.basename(file));
942 948 });
943 949
950 // ---------------------------------------------------------------------------
951 // POST /api/files/watched-statuses
952 // Body: { filePaths: [<absolute-path>, ...] }
953 // Returns per-path watched status for the authenticated user.
954 // ---------------------------------------------------------------------------
955 router.post('/watched-statuses', requireAuth, (req, res) =>
956 {
957 const rawPaths = req.body && Array.isArray(req.body.filePaths) ? req.body.filePaths : null;
958 if (!rawPaths)
959 {
960 return res.status(400).json({ error: 'filePaths array is required.' });
961 }
962
963 const paths = rawPaths
964 .map((p) => String(p || '').trim())
965 .filter(Boolean)
966 .slice(0, 500);
967
968 const { config, roots } = loadConfigAndRoots();
969 const db = getDb();
970 const statuses = {};
971 const selectMeta = db.prepare('SELECT watched_by FROM file_meta WHERE file_path = ?');
972
973 for (const filePath of paths)
974 {
975 const info = resolveRootForUser(filePath, roots, req.user, config);
976 if (!info || !canReadInfo(req.user, info)) continue;
977
978 let stat;
979 try
980 {
981 stat = fs.statSync(filePath);
982 } catch
983 {
984 continue;
985 }
986 if (stat.isDirectory()) continue;
987
988 const row = selectMeta.get([filePath]);
989 const watchedBy = parseJsonObject(row && row.watched_by);
990 const status = normalisedWatchedStatus(watchedBy[req.user.username]);
991 if (status) statuses[filePath] = status;
992 }
993
994 res.json({ statuses });
995 });
996
944 997 // ---------------------------------------------------------------------------
945 998 // POST /api/files/watch-progress
946 999 // Body: { filePath: <absolute-path>, progress: <0..1> }
@@ -468,6 +468,12 @@ function watchStatusForProgress (progress)
468 return null;
468 return null;
469}
469}
470
470
471function normalisedWatchedStatus (value)
472{
473 const status = String(value || '').toLowerCase();
474 return status === 'fully' || status === 'partway' ? status : null;
475}
476
471function splitNameExt (name)
477function splitNameExt (name)
472{
478{
473 const dot = String(name || '').lastIndexOf('.');
479 const dot = String(name || '').lastIndexOf('.');
@@ -941,6 +947,53 @@ router.get('/download', requireAuth, (req, res) =>
941 res.download(file, path.basename(file));
947 res.download(file, path.basename(file));
942});
948});
943
949
950// ---------------------------------------------------------------------------
951// POST /api/files/watched-statuses
952// Body: { filePaths: [<absolute-path>, ...] }
953// Returns per-path watched status for the authenticated user.
954// ---------------------------------------------------------------------------
955router.post('/watched-statuses', requireAuth, (req, res) =>
956{
957 const rawPaths = req.body && Array.isArray(req.body.filePaths) ? req.body.filePaths : null;
958 if (!rawPaths)
959 {
960 return res.status(400).json({ error: 'filePaths array is required.' });
961 }
962
963 const paths = rawPaths
964 .map((p) => String(p || '').trim())
965 .filter(Boolean)
966 .slice(0, 500);
967
968 const { config, roots } = loadConfigAndRoots();
969 const db = getDb();
970 const statuses = {};
971 const selectMeta = db.prepare('SELECT watched_by FROM file_meta WHERE file_path = ?');
972
973 for (const filePath of paths)
974 {
975 const info = resolveRootForUser(filePath, roots, req.user, config);
976 if (!info || !canReadInfo(req.user, info)) continue;
977
978 let stat;
979 try
980 {
981 stat = fs.statSync(filePath);
982 } catch
983 {
984 continue;
985 }
986 if (stat.isDirectory()) continue;
987
988 const row = selectMeta.get([filePath]);
989 const watchedBy = parseJsonObject(row && row.watched_by);
990 const status = normalisedWatchedStatus(watchedBy[req.user.username]);
991 if (status) statuses[filePath] = status;
992 }
993
994 res.json({ statuses });
995});
996
944// ---------------------------------------------------------------------------
997// ---------------------------------------------------------------------------
945// POST /api/files/watch-progress
998// POST /api/files/watch-progress
946// Body: { filePath: <absolute-path>, progress: <0..1> }
999// Body: { filePath: <absolute-path>, progress: <0..1> }
tests/files.routes.test.js +35 −0
@@ -823,6 +823,41 @@ describe('video streaming routes', () =>
823 823 expect(result.status).toBe(200);
824 824 expect(result.body.watchedBy).toEqual({ admin: 'fully' });
825 825 });
826
827 test('POST /api/files/watched-statuses returns bulk watched statuses for accessible files', async () =>
828 {
829 const clipA = path.join(rootDir, 'clip-a.mkv');
830 const clipB = path.join(rootDir, 'clip-b.mkv');
831 const clipC = path.join(rootDir, 'clip-c.mkv');
832 fs.writeFileSync(clipA, 'a');
833 fs.writeFileSync(clipB, 'b');
834 fs.writeFileSync(clipC, 'c');
835
836 const db = getDb();
837 db.prepare('INSERT INTO file_meta (file_path, uploader, downloaders, watched_by) VALUES (?, ?, ?, ?)')
838 .run([clipA, '', '[]', JSON.stringify({ admin: 'fully' })]);
839 db.prepare('INSERT INTO file_meta (file_path, uploader, downloaders, watched_by) VALUES (?, ?, ?, ?)')
840 .run([clipB, '', '[]', JSON.stringify({ admin: 'partway' })]);
841 db.prepare('INSERT INTO file_meta (file_path, uploader, downloaders, watched_by) VALUES (?, ?, ?, ?)')
842 .run([clipC, '', '[]', JSON.stringify({ admin: 'none' })]);
843
844 const { status, body } = await req('POST', '/api/files/watched-statuses', {
845 filePaths: [clipA, clipB, clipC],
846 });
847
848 expect(status).toBe(200);
849 expect(body.statuses).toEqual({
850 [clipA]: 'fully',
851 [clipB]: 'partway',
852 });
853 });
854
855 test('POST /api/files/watched-statuses validates filePaths input', async () =>
856 {
857 const { status, body } = await req('POST', '/api/files/watched-statuses', { filePaths: 'nope' });
858 expect(status).toBe(400);
859 expect(body.error).toMatch(/filePaths array is required/i);
860 });
826 861 });
827 862
828 863 describe('SHA-256 hashing and duplicate detection', () =>
@@ -823,6 +823,41 @@ describe('video streaming routes', () =>
823 expect(result.status).toBe(200);
823 expect(result.status).toBe(200);
824 expect(result.body.watchedBy).toEqual({ admin: 'fully' });
824 expect(result.body.watchedBy).toEqual({ admin: 'fully' });
825 });
825 });
826
827 test('POST /api/files/watched-statuses returns bulk watched statuses for accessible files', async () =>
828 {
829 const clipA = path.join(rootDir, 'clip-a.mkv');
830 const clipB = path.join(rootDir, 'clip-b.mkv');
831 const clipC = path.join(rootDir, 'clip-c.mkv');
832 fs.writeFileSync(clipA, 'a');
833 fs.writeFileSync(clipB, 'b');
834 fs.writeFileSync(clipC, 'c');
835
836 const db = getDb();
837 db.prepare('INSERT INTO file_meta (file_path, uploader, downloaders, watched_by) VALUES (?, ?, ?, ?)')
838 .run([clipA, '', '[]', JSON.stringify({ admin: 'fully' })]);
839 db.prepare('INSERT INTO file_meta (file_path, uploader, downloaders, watched_by) VALUES (?, ?, ?, ?)')
840 .run([clipB, '', '[]', JSON.stringify({ admin: 'partway' })]);
841 db.prepare('INSERT INTO file_meta (file_path, uploader, downloaders, watched_by) VALUES (?, ?, ?, ?)')
842 .run([clipC, '', '[]', JSON.stringify({ admin: 'none' })]);
843
844 const { status, body } = await req('POST', '/api/files/watched-statuses', {
845 filePaths: [clipA, clipB, clipC],
846 });
847
848 expect(status).toBe(200);
849 expect(body.statuses).toEqual({
850 [clipA]: 'fully',
851 [clipB]: 'partway',
852 });
853 });
854
855 test('POST /api/files/watched-statuses validates filePaths input', async () =>
856 {
857 const { status, body } = await req('POST', '/api/files/watched-statuses', { filePaths: 'nope' });
858 expect(status).toBe(400);
859 expect(body.error).toMatch(/filePaths array is required/i);
860 });
826});
861});
827
862
828describe('SHA-256 hashing and duplicate detection', () =>
863describe('SHA-256 hashing and duplicate detection', () =>