BlueGit

timfa filemanager

Lower partway watch threshold to 10% and harden upload abort stream cleanup

timfa Tim Falken · pushed by timfa

Authored 2026-05-08 · 6 months ago

Hash
be44098bd19a538f2c1b0fc1ce20be2b086b38bf
Parent
37872c5f
4 files changed +17 −9
.github/technical-reference.md +5 −5
@@ -56,23 +56,23 @@
56 56 - splitNameExt(name) - Splits a filename into stem/ext, preserving dotless names and hidden-file behavior.
57 57 - pickNumberedPath(dir, requestedName) - Finds the first free `name (n).ext` path inside a directory.
58 58 - removePathRecursive(targetPath) - Deletes existing file or directory destination to support overwrite moves.
59 - POST /api/files/upload - Streams multipart uploads into hidden temp files, cleans them up on abort, and only exposes final filenames after completion.
59 - POST /api/files/upload - Streams multipart uploads into hidden temp files, unpipes stream writes on abort/failure, cleans temp files, and only exposes final filenames after completion.
60 60 - POST /api/files/move - Moves file/folder; supports conflict strategies: `error` (default), `overwrite`, or `rename` with optional `targetName`.
61 - POST /api/files/video-stream - Creates a short-lived HLS session for a video and returns playlistUrl, durationSeconds, subtitleTracks, and subtitleSummary ({total, converted, skipped}).
62 - POST /api/files/watch-progress - Records per-user watch status (`partway` >=30%, `fully` >=70%) for sidebar "Watched by" display.
61 - POST /api/files/video-stream - Creates a short-lived HLS session for a video and returns playlistUrl, durationSeconds, subtitleTracks, and subtitleSummary ({total, converted, skipped}).
62 - POST /api/files/watch-progress - Records per-user watch status (`partway` >=10%, `fully` >=70%) for sidebar "Watched by" display.
63 63
64 64 ## server/routes/share.js
65 65 - POST /api/share/create - Authenticated. Creates a 24-hour share token for a file or directory; returns {token, expiresAt}.
66 66 - GET /api/share/info/:token - Public. Returns {name, isDir, expiresAt} for a valid non-expired token; 410 when expired, 404 when unknown.
67 67 - GET /api/share/preview/:token - Public. Streams file content inline for preview (files only, max 50 MB); 400 for directories, 410 expired.
68 68 - GET /api/share/download/:token - Public. Downloads the file or a zip of the directory for a valid token; 410 expired.
69 - GET /api/share/video-stream/:token - Public. Creates a short-lived HLS session for a shared video and returns playlistUrl, durationSeconds, subtitleTracks, and subtitleSummary ({total, converted, skipped}).
69 - GET /api/share/video-stream/:token - Public. Creates a short-lived HLS session for a shared video and returns playlistUrl, durationSeconds, subtitleTracks, and subtitleSummary ({total, converted, skipped}).
70 70 - buildZip(dirPath, cacheKey) - Creates (or returns cached) zip in zip-cache/; prunes stale zips before building; exported for reuse in files.js.
71 71 - pruneZipCache() - Deletes zip files in zip-cache/ older than 1 day; called automatically before each zip build.
72 72 - ZIP_CACHE_DIR - Absolute path to the zip-cache/ directory (project root).
73 73
74 74 ## server/video-stream.js
75 - createVideoStreamSession(sourcePath) - Probes the input duration, attempts WebVTT conversion for all subtitle streams (skipping only failed conversions), starts ffmpeg HLS transcoding, and resolves only when HLS assets are present (not subtitle files alone); returns durationSeconds, subtitleTracks, and subtitleSummary {total, converted, skipped}.
75 - createVideoStreamSession(sourcePath) - Probes the input duration, attempts WebVTT conversion for all subtitle streams (skipping only failed conversions), starts ffmpeg HLS transcoding, and resolves only when HLS assets are present (not subtitle files alone); returns durationSeconds, subtitleTracks, and subtitleSummary {total, converted, skipped}.
76 76 - sendVideoStreamAsset(sessionId, assetPath, res) - Serves HLS playlists, segments, and extracted subtitle files from an active short-lived stream session.
77 77 - STREAM_ROOT_DIR - Temporary root directory for active ffmpeg-backed stream sessions.
78 78 - STREAM_SESSION_TTL_MS - Idle lifetime for a stream session before cleanup.
@@ -56,23 +56,23 @@
56- splitNameExt(name) - Splits a filename into stem/ext, preserving dotless names and hidden-file behavior.
56- splitNameExt(name) - Splits a filename into stem/ext, preserving dotless names and hidden-file behavior.
57- pickNumberedPath(dir, requestedName) - Finds the first free `name (n).ext` path inside a directory.
57- pickNumberedPath(dir, requestedName) - Finds the first free `name (n).ext` path inside a directory.
58- removePathRecursive(targetPath) - Deletes existing file or directory destination to support overwrite moves.
58- removePathRecursive(targetPath) - Deletes existing file or directory destination to support overwrite moves.
59- POST /api/files/upload - Streams multipart uploads into hidden temp files, cleans them up on abort, and only exposes final filenames after completion.
59- POST /api/files/upload - Streams multipart uploads into hidden temp files, unpipes stream writes on abort/failure, cleans temp files, and only exposes final filenames after completion.
60- POST /api/files/move - Moves file/folder; supports conflict strategies: `error` (default), `overwrite`, or `rename` with optional `targetName`.
60- POST /api/files/move - Moves file/folder; supports conflict strategies: `error` (default), `overwrite`, or `rename` with optional `targetName`.
61- POST /api/files/video-stream - Creates a short-lived HLS session for a video and returns playlistUrl, durationSeconds, subtitleTracks, and subtitleSummary ({total, converted, skipped}).
61- POST /api/files/video-stream - Creates a short-lived HLS session for a video and returns playlistUrl, durationSeconds, subtitleTracks, and subtitleSummary ({total, converted, skipped}).
62- POST /api/files/watch-progress - Records per-user watch status (`partway` >=30%, `fully` >=70%) for sidebar "Watched by" display.
62- POST /api/files/watch-progress - Records per-user watch status (`partway` >=10%, `fully` >=70%) for sidebar "Watched by" display.
63
63
64## server/routes/share.js
64## server/routes/share.js
65- POST /api/share/create - Authenticated. Creates a 24-hour share token for a file or directory; returns {token, expiresAt}.
65- POST /api/share/create - Authenticated. Creates a 24-hour share token for a file or directory; returns {token, expiresAt}.
66- GET /api/share/info/:token - Public. Returns {name, isDir, expiresAt} for a valid non-expired token; 410 when expired, 404 when unknown.
66- GET /api/share/info/:token - Public. Returns {name, isDir, expiresAt} for a valid non-expired token; 410 when expired, 404 when unknown.
67- GET /api/share/preview/:token - Public. Streams file content inline for preview (files only, max 50 MB); 400 for directories, 410 expired.
67- GET /api/share/preview/:token - Public. Streams file content inline for preview (files only, max 50 MB); 400 for directories, 410 expired.
68- GET /api/share/download/:token - Public. Downloads the file or a zip of the directory for a valid token; 410 expired.
68- GET /api/share/download/:token - Public. Downloads the file or a zip of the directory for a valid token; 410 expired.
69- GET /api/share/video-stream/:token - Public. Creates a short-lived HLS session for a shared video and returns playlistUrl, durationSeconds, subtitleTracks, and subtitleSummary ({total, converted, skipped}).
69- GET /api/share/video-stream/:token - Public. Creates a short-lived HLS session for a shared video and returns playlistUrl, durationSeconds, subtitleTracks, and subtitleSummary ({total, converted, skipped}).
70- buildZip(dirPath, cacheKey) - Creates (or returns cached) zip in zip-cache/; prunes stale zips before building; exported for reuse in files.js.
70- buildZip(dirPath, cacheKey) - Creates (or returns cached) zip in zip-cache/; prunes stale zips before building; exported for reuse in files.js.
71- pruneZipCache() - Deletes zip files in zip-cache/ older than 1 day; called automatically before each zip build.
71- pruneZipCache() - Deletes zip files in zip-cache/ older than 1 day; called automatically before each zip build.
72- ZIP_CACHE_DIR - Absolute path to the zip-cache/ directory (project root).
72- ZIP_CACHE_DIR - Absolute path to the zip-cache/ directory (project root).
73
73
74## server/video-stream.js
74## server/video-stream.js
75- createVideoStreamSession(sourcePath) - Probes the input duration, attempts WebVTT conversion for all subtitle streams (skipping only failed conversions), starts ffmpeg HLS transcoding, and resolves only when HLS assets are present (not subtitle files alone); returns durationSeconds, subtitleTracks, and subtitleSummary {total, converted, skipped}.
75- createVideoStreamSession(sourcePath) - Probes the input duration, attempts WebVTT conversion for all subtitle streams (skipping only failed conversions), starts ffmpeg HLS transcoding, and resolves only when HLS assets are present (not subtitle files alone); returns durationSeconds, subtitleTracks, and subtitleSummary {total, converted, skipped}.
76- sendVideoStreamAsset(sessionId, assetPath, res) - Serves HLS playlists, segments, and extracted subtitle files from an active short-lived stream session.
76- sendVideoStreamAsset(sessionId, assetPath, res) - Serves HLS playlists, segments, and extracted subtitle files from an active short-lived stream session.
77- STREAM_ROOT_DIR - Temporary root directory for active ffmpeg-backed stream sessions.
77- STREAM_ROOT_DIR - Temporary root directory for active ffmpeg-backed stream sessions.
78- STREAM_SESSION_TTL_MS - Idle lifetime for a stream session before cleanup.
78- STREAM_SESSION_TTL_MS - Idle lifetime for a stream session before cleanup.
public/app.js +1 −1
@@ -2481,7 +2481,7 @@ async function renderStreamedVideoPreview (item, targetEl)
2481 2481 return;
2482 2482 }
2483 2483
2484 if (ratio >= 0.3)
2484 if (ratio >= 0.1)
2485 2485 {
2486 2486 if (sentPartway) return;
2487 2487 sentPartway = true;
@@ -2481,7 +2481,7 @@ async function renderStreamedVideoPreview (item, targetEl)
2481 return;
2481 return;
2482 }
2482 }
2483
2483
2484 if (ratio >= 0.3)
2484 if (ratio >= 0.1)
2485 {
2485 {
2486 if (sentPartway) return;
2486 if (sentPartway) return;
2487 sentPartway = true;
2487 sentPartway = true;
server/routes/files.js +9 −1
@@ -65,6 +65,13 @@ const storage = {
65 65 out.off('finish', onFinish);
66 66 };
67 67
68 const stopPiping = () =>
69 {
70 // Prevent late file stream chunks from writing into a destroyed stream.
71 file.stream.unpipe(out);
72 if (typeof file.stream.resume === 'function') file.stream.resume();
73 };
74
68 75 const cleanup = (done) =>
69 76 {
70 77 if (cleaned) return done(null);
@@ -76,6 +83,7 @@ const storage = {
76 83 {
77 84 if (settled) return;
78 85 settled = true;
86 stopPiping();
79 87 detach();
80 88 out.destroy();
81 89 cleanup((cleanupErr) => cb(cleanupErr || err));
@@ -306,7 +314,7 @@ function watchStatusForProgress (progress)
306 314 {
307 315 if (!Number.isFinite(progress) || progress < 0) return null;
308 316 if (progress >= 0.7) return 'fully';
309 if (progress >= 0.3) return 'partway';
317 if (progress >= 0.1) return 'partway';
310 318 return null;
311 319 }
312 320
@@ -65,6 +65,13 @@ const storage = {
65 out.off('finish', onFinish);
65 out.off('finish', onFinish);
66 };
66 };
67
67
68 const stopPiping = () =>
69 {
70 // Prevent late file stream chunks from writing into a destroyed stream.
71 file.stream.unpipe(out);
72 if (typeof file.stream.resume === 'function') file.stream.resume();
73 };
74
68 const cleanup = (done) =>
75 const cleanup = (done) =>
69 {
76 {
70 if (cleaned) return done(null);
77 if (cleaned) return done(null);
@@ -76,6 +83,7 @@ const storage = {
76 {
83 {
77 if (settled) return;
84 if (settled) return;
78 settled = true;
85 settled = true;
86 stopPiping();
79 detach();
87 detach();
80 out.destroy();
88 out.destroy();
81 cleanup((cleanupErr) => cb(cleanupErr || err));
89 cleanup((cleanupErr) => cb(cleanupErr || err));
@@ -306,7 +314,7 @@ function watchStatusForProgress (progress)
306{
314{
307 if (!Number.isFinite(progress) || progress < 0) return null;
315 if (!Number.isFinite(progress) || progress < 0) return null;
308 if (progress >= 0.7) return 'fully';
316 if (progress >= 0.7) return 'fully';
309 if (progress >= 0.3) return 'partway';
317 if (progress >= 0.1) return 'partway';
310 return null;
318 return null;
311}
319}
312
320
tests/files.routes.test.js +2 −2
@@ -735,12 +735,12 @@ describe('video streaming routes', () =>
735 735 const filePath = path.join(rootDir, 'clip.mkv');
736 736 fs.writeFileSync(filePath, 'video');
737 737
738 let result = await req('POST', '/api/files/watch-progress', { filePath, progress: 0.35 });
738 let result = await req('POST', '/api/files/watch-progress', { filePath, progress: 0.15 });
739 739 expect(result.status).toBe(200);
740 740 expect(result.body).toMatchObject({ ok: true, updated: true, status: 'partway' });
741 741
742 742 // A lower progress update should not downgrade the status.
743 result = await req('POST', '/api/files/watch-progress', { filePath, progress: 0.4 });
743 result = await req('POST', '/api/files/watch-progress', { filePath, progress: 0.2 });
744 744 expect(result.status).toBe(200);
745 745 expect(result.body).toMatchObject({ ok: true, updated: false, status: 'partway' });
746 746
@@ -735,12 +735,12 @@ describe('video streaming routes', () =>
735 const filePath = path.join(rootDir, 'clip.mkv');
735 const filePath = path.join(rootDir, 'clip.mkv');
736 fs.writeFileSync(filePath, 'video');
736 fs.writeFileSync(filePath, 'video');
737
737
738 let result = await req('POST', '/api/files/watch-progress', { filePath, progress: 0.35 });
738 let result = await req('POST', '/api/files/watch-progress', { filePath, progress: 0.15 });
739 expect(result.status).toBe(200);
739 expect(result.status).toBe(200);
740 expect(result.body).toMatchObject({ ok: true, updated: true, status: 'partway' });
740 expect(result.body).toMatchObject({ ok: true, updated: true, status: 'partway' });
741
741
742 // A lower progress update should not downgrade the status.
742 // A lower progress update should not downgrade the status.
743 result = await req('POST', '/api/files/watch-progress', { filePath, progress: 0.4 });
743 result = await req('POST', '/api/files/watch-progress', { filePath, progress: 0.2 });
744 expect(result.status).toBe(200);
744 expect(result.status).toBe(200);
745 expect(result.body).toMatchObject({ ok: true, updated: false, status: 'partway' });
745 expect(result.body).toMatchObject({ ok: true, updated: false, status: 'partway' });
746
746