BlueGit

timfa filemanager

Files

filemanager / public / share.html

share.html

261cda41 History
share.html 20.0 KiB Download Raw
1<!DOCTYPE html>
2<html lang="en-GB">
3
4<head>
5 <meta charset="UTF-8" />
6 <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7 <title>Shared file</title>
8 <link rel="stylesheet" href="/vendor/video.js/video-js.css" />
9 <style>
10 *,
11 *::before,
12 *::after {
13 box-sizing: border-box;
14 margin: 0;
15 padding: 0;
16 }
17
18 :root {
19 --bg: #0f1117;
20 --surface: #1a1d27;
21 --surface2: #21242f;
22 --border: #2e3247;
23 --accent: #5b7fff;
24 --accent-hover: #7b9fff;
25 --text: #e8eaf0;
26 --text-muted: #7880a0;
27 --danger: #e05c6e;
28 --success: #3fcf8e;
29 --radius: 10px;
30 }
31
32 html,
33 body {
34 height: 100%;
35 font-family: system-ui, -apple-system, sans-serif;
36 }
37
38 body {
39 background: var(--bg);
40 color: var(--text);
41 min-height: 100vh;
42 }
43
44 /* ── Timer bar ── */
45 #timer-bar {
46 position: sticky;
47 top: 0;
48 z-index: 10;
49 background: var(--surface);
50 border-bottom: 1px solid var(--border);
51 display: flex;
52 align-items: center;
53 justify-content: center;
54 gap: 0.6rem;
55 padding: 0.55rem 1rem;
56 font-size: 0.9rem;
57 color: var(--text-muted);
58 }
59
60 #timer-bar .timer-clock {
61 font-variant-numeric: tabular-nums;
62 font-weight: 600;
63 color: var(--text);
64 letter-spacing: 0.05em;
65 }
66
67 #timer-bar.expired {
68 color: var(--danger);
69 }
70
71 #timer-bar.expired .timer-clock {
72 color: var(--danger);
73 }
74
75 /* ── Main card ── */
76 #main {
77 max-width: 760px;
78 margin: 3rem auto;
79 padding: 0 1rem;
80 }
81
82 .card {
83 background: var(--surface);
84 border: 1px solid var(--border);
85 border-radius: var(--radius);
86 padding: 2rem;
87 margin-bottom: 1.5rem;
88 }
89
90 .card h1 {
91 font-size: 1.3rem;
92 font-weight: 600;
93 margin-bottom: 0.3rem;
94 word-break: break-all;
95 }
96
97 .card .meta {
98 font-size: 0.85rem;
99 color: var(--text-muted);
100 margin-bottom: 1.4rem;
101 }
102
103 /* ── Download button ── */
104 .btn {
105 display: inline-flex;
106 align-items: center;
107 gap: 0.5rem;
108 padding: 0.6rem 1.2rem;
109 border-radius: 7px;
110 font-size: 0.92rem;
111 font-weight: 500;
112 cursor: pointer;
113 border: none;
114 transition: background 0.15s;
115 }
116
117 .btn-primary {
118 background: var(--accent);
119 color: #fff;
120 }
121
122 .btn-primary:hover {
123 background: var(--accent-hover);
124 }
125
126 .btn-primary:disabled {
127 opacity: 0.45;
128 cursor: not-allowed;
129 }
130
131 /* ── Preview area ── */
132 #preview-wrap {
133 background: var(--surface2);
134 border: 1px solid var(--border);
135 border-radius: var(--radius);
136 overflow: hidden;
137 margin-bottom: 1.5rem;
138 min-height: 80px;
139 display: flex;
140 align-items: center;
141 justify-content: center;
142 }
143
144 #preview-wrap img,
145 #preview-wrap video,
146 #preview-wrap audio,
147 #preview-wrap .video-js {
148 max-width: 100%;
149 max-height: 60vh;
150 display: block;
151 }
152
153 #preview-wrap .video-js {
154 width: 100%;
155 min-height: 360px;
156 background: #000;
157 }
158
159 #preview-wrap .video-js .vjs-tech {
160 object-fit: contain;
161 }
162
163 #preview-wrap audio {
164 padding: 1rem;
165 width: 100%;
166 }
167
168 #preview-wrap pre {
169 padding: 1.2rem;
170 overflow: auto;
171 max-height: 60vh;
172 width: 100%;
173 font-size: 0.82rem;
174 font-family: monospace;
175 color: var(--text);
176 white-space: pre-wrap;
177 word-break: break-word;
178 }
179
180 #preview-wrap .no-preview {
181 padding: 2rem;
182 color: var(--text-muted);
183 font-size: 0.9rem;
184 text-align: center;
185 }
186
187 /* ── Error / expired states ── */
188 #error-state {
189 text-align: center;
190 padding: 4rem 1rem;
191 }
192
193 #error-state h2 {
194 font-size: 1.4rem;
195 margin-bottom: 0.5rem;
196 }
197
198 #error-state p {
199 color: var(--text-muted);
200 }
201
202 /* ── Loading spinner ── */
203 #loading {
204 text-align: center;
205 padding: 6rem 1rem;
206 color: var(--text-muted);
207 }
208 </style>
209</head>
210
211<body>
212
213 <div id="timer-bar" style="display:none">
214 <span>Valid for</span>
215 <span id="timer-clock" class="timer-clock">--:--:--</span>
216 </div>
217
218 <div id="main">
219 <div id="loading">Loading...</div>
220 <div id="error-state" style="display:none">
221 <h2 id="error-title">Not found</h2>
222 <p id="error-msg">This link does not exist or has expired.</p>
223 </div>
224
225 <div id="content" style="display:none">
226 <div class="card">
227 <h1 id="file-name"></h1>
228 <p id="file-meta" class="meta"></p>
229 <a id="btn-download" class="btn btn-primary" href="#" download>
230 <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none"
231 stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
232 <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" />
233 <polyline points="7 10 12 15 17 10" />
234 <line x1="12" y1="15" x2="12" y2="3" />
235 </svg>
236 <span id="btn-download-label">Download</span>
237 </a>
238 </div>
239
240 <div id="preview-wrap" style="display:none"></div>
241 </div>
242 </div>
243
244 <script src="/shared-video-player.js"></script>
245 <script>
246 (function ()
247 {
248 'use strict';
249
250 const token = location.pathname.split('/').pop();
251 const VIDEO_FORWARD_BUFFER_SECONDS = 60 * 60;
252 const IMAGE_SUBTITLE_LOOKAHEAD_SECONDS = 15;
253 const IMAGE_SUBTITLE_PREFETCH_REQUEST_INTERVAL_MS = 40;
254 const SHARE_VIDEO_PREFS_KEY = 'filemanager.share-video-prefs.v1';
255 let expiresAt = null;
256 let timerInterval = null;
257 let shareVideoPlayer = null;
258 let shareTrackPrefs = null;
259 let shareImageSubtitleController = null;
260 let shareVideoSessionId = null;
261
262 function sharedVideoPlayer ()
263 {
264 return window.FilemanagerSharedVideoPlayer || null;
265 }
266
267 function loadShareVideoPrefs ()
268 {
269 try
270 {
271 const raw = localStorage.getItem(SHARE_VIDEO_PREFS_KEY);
272 const parsed = raw ? JSON.parse(raw) : {};
273 if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return {};
274 return parsed;
275 } catch
276 {
277 return {};
278 }
279 }
280
281 function saveShareVideoPrefs (patch)
282 {
283 try
284 {
285 const current = loadShareVideoPrefs();
286 localStorage.setItem(SHARE_VIDEO_PREFS_KEY, JSON.stringify({ ...current, ...(patch || {}) }));
287 } catch
288 {
289 // Ignore localStorage failures.
290 }
291 }
292
293 function applyShareVideoTextTrackPrefs ()
294 {
295 const shared = sharedVideoPlayer();
296 if (!shared || typeof shared.applyDefaultTextTrackPrefs !== 'function') return;
297 shared.applyDefaultTextTrackPrefs();
298 }
299
300 function bindShareVideoTrackPrefs (player, prefs)
301 {
302 const shared = sharedVideoPlayer();
303 if (!shared || typeof shared.bindTrackPrefs !== 'function')
304 {
305 return {
306 applySavedTracks: () => { },
307 captureSelectedTracks: () => { },
308 detach: () => { },
309 };
310 }
311 return shared.bindTrackPrefs(player, prefs, saveShareVideoPrefs);
312 }
313
314 function releaseShareVideoSession (sessionId, opts = {})
315 {
316 const id = String(sessionId || '').trim();
317 if (!id) return Promise.resolve();
318
319 return fetch(`/api/share/video-stream/session/${encodeURIComponent(id)}`, {
320 method: 'DELETE',
321 credentials: 'same-origin',
322 keepalive: !!opts.keepalive,
323 }).catch(() => null);
324 }
325
326 function cleanupShareVideoPlayer ()
327 {
328 const sessionId = shareVideoSessionId;
329 shareVideoSessionId = null;
330
331 try { shareImageSubtitleController && shareImageSubtitleController.captureSelectedTracks(); } catch { /* ignore */ }
332 try { shareImageSubtitleController && shareImageSubtitleController.detach(); } catch { /* ignore */ }
333 try { shareTrackPrefs && shareTrackPrefs.captureSelectedTracks(); } catch { /* ignore */ }
334 try { shareTrackPrefs && shareTrackPrefs.detach(); } catch { /* ignore */ }
335 try { shareVideoPlayer && shareVideoPlayer.dispose(); } catch { /* ignore */ }
336
337 if (sessionId) void releaseShareVideoSession(sessionId, { keepalive: true });
338
339 shareVideoPlayer = null;
340 shareTrackPrefs = null;
341 shareImageSubtitleController = null;
342 }
343
344 function configureVideoJsBuffering ()
345 {
346 const shared = sharedVideoPlayer();
347 if (!shared || typeof shared.configureVideoJsBuffering !== 'function') return;
348 shared.configureVideoJsBuffering(window.videojs, { seconds: VIDEO_FORWARD_BUFFER_SECONDS });
349 }
350
351 // ── Timer ───────────────────────────────────────────────────────────────
352 function startTimer ()
353 {
354 const bar = document.getElementById('timer-bar');
355 const clock = document.getElementById('timer-clock');
356 bar.style.display = '';
357
358 function tick ()
359 {
360 const remaining = expiresAt - Date.now();
361 if (remaining <= 0)
362 {
363 clock.textContent = '00:00:00';
364 bar.classList.add('expired');
365 bar.querySelector('span').textContent = 'Expired —';
366 const dlBtn = document.getElementById('btn-download');
367 if (dlBtn) dlBtn.setAttribute('disabled', 'disabled');
368 clearInterval(timerInterval);
369 return;
370 }
371 const totalSecs = Math.floor(remaining / 1000);
372 const hh = String(Math.floor(totalSecs / 3600)).padStart(2, '0');
373 const mm = String(Math.floor((totalSecs % 3600) / 60)).padStart(2, '0');
374 const ss = String(totalSecs % 60).padStart(2, '0');
375 clock.textContent = `${hh}:${mm}:${ss}`;
376 }
377
378 tick();
379 timerInterval = setInterval(tick, 1000);
380 }
381
382 // ── Preview ──────────────────────────────────────────────────────────────
383 function normaliseVideoDuration (player, durationSeconds)
384 {
385 const shared = sharedVideoPlayer();
386 if (!shared || typeof shared.normaliseVideoDuration !== 'function') return;
387 shared.normaliseVideoDuration(player, durationSeconds);
388 }
389
390 function ensureVideoPlaybackUi (player, durationSeconds)
391 {
392 const shared = sharedVideoPlayer();
393 if (!shared || typeof shared.ensureVideoPlaybackUi !== 'function') return;
394 shared.ensureVideoPlaybackUi(player, durationSeconds);
395 }
396
397 function tryPreview (name, isDir)
398 {
399 if (isDir) return; // no preview for folders
400
401 const wrap = document.getElementById('preview-wrap');
402 const ext = name.split('.').pop().toLowerCase();
403
404 const imageExts = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg', 'bmp', 'avif', 'ico'];
405 const videoExts = ['mp4', 'webm', 'ogv', 'mov', 'avi', 'mkv'];
406 const audioExts = ['mp3', 'wav', 'ogg', 'flac', 'aac', 'opus', 'm4a'];
407 const textExts = ['txt', 'md', 'json', 'js', 'ts', 'html', 'css', 'xml', 'csv', 'yaml', 'yml', 'sh', 'log', 'ini', 'toml', 'py', 'rb', 'go', 'java', 'c', 'cpp', 'h', 'rs'];
408
409 const previewUrl = `/api/share/preview/${token}`;
410 let el;
411
412 if (imageExts.includes(ext))
413 {
414 el = document.createElement('img');
415 el.src = previewUrl;
416 el.alt = name;
417 } else if (videoExts.includes(ext))
418 {
419 fetch(`/api/share/video-stream/${token}`)
420 .then(r => r.json().then(d => ({ ok: r.ok, data: d })))
421 .then(({ ok, data }) =>
422 {
423 if (!ok) throw new Error(data.error || 'Streaming failed');
424 configureVideoJsBuffering();
425 applyShareVideoTextTrackPrefs();
426 const prefs = loadShareVideoPrefs();
427
428 const video = document.createElement('video');
429 video.className = 'video-js vjs-big-play-centered';
430 video.controls = true;
431 video.preload = 'auto';
432 video.playsInline = true;
433
434 wrap.innerHTML = '';
435 wrap.appendChild(video);
436
437 cleanupShareVideoPlayer();
438 shareVideoSessionId = String(data.sessionId || '').trim() || null;
439
440 shareVideoPlayer = window.videojs(video, {
441 controls: true,
442 fluid: true,
443 responsive: true,
444 preload: 'auto',
445 controlBar: {
446 currentTimeDisplay: true,
447 timeDivider: true,
448 durationDisplay: true,
449 remainingTimeDisplay: false,
450 },
451 sources: [{ src: data.playlistUrl, type: 'application/x-mpegURL' }],
452 tracks: (data.subtitleTracks || []).map(track => ({
453 kind: 'subtitles',
454 src: track.src,
455 srclang: track.language || 'und',
456 label: track.label,
457 default: !!track.default,
458 })),
459 });
460
461 shareTrackPrefs = bindShareVideoTrackPrefs(shareVideoPlayer, prefs);
462 const shared = sharedVideoPlayer();
463 shareImageSubtitleController = shared && typeof shared.bindImageSubtitleOverlay === 'function'
464 ? shared.bindImageSubtitleOverlay(shareVideoPlayer, window.videojs, prefs, data, {
465 savePrefs: saveShareVideoPrefs,
466 lookaheadSeconds: IMAGE_SUBTITLE_LOOKAHEAD_SECONDS,
467 prefetchRequestIntervalMs: IMAGE_SUBTITLE_PREFETCH_REQUEST_INTERVAL_MS,
468 sendSubtitleLogBatch: (entries) =>
469 {
470 if (!Array.isArray(entries) || !entries.length) return Promise.resolve();
471 return fetch(`/api/share/video-stream-subtitle-log/${encodeURIComponent(token)}`, {
472 method: 'POST',
473 credentials: 'same-origin',
474 keepalive: true,
475 headers: {
476 'Content-Type': 'application/json',
477 },
478 body: JSON.stringify({
479 sessionId: data.sessionId || null,
480 entries,
481 }),
482 }).catch(() => null);
483 },
484 buildFrameRequest: ({ currentTime, streamIndex, session }) => ({
485 url: `/api/share/video-stream-subtitle-frame/${encodeURIComponent(token)}?streamIndex=${streamIndex}&timeSeconds=${encodeURIComponent(String(currentTime))}&videoWidth=${encodeURIComponent(String(session.videoWidth || ''))}&videoHeight=${encodeURIComponent(String(session.videoHeight || ''))}`,
486 credentials: 'same-origin',
487 }),
488 })
489 : null;
490 ensureVideoPlaybackUi(shareVideoPlayer, data.durationSeconds);
491
492 shareVideoPlayer.ready(() =>
493 {
494 const volume = Number(prefs.volume);
495 if (Number.isFinite(volume))
496 {
497 shareVideoPlayer.volume(Math.max(0, Math.min(1, volume)));
498 }
499 shareTrackPrefs.applySavedTracks();
500 if (shareImageSubtitleController && typeof shareImageSubtitleController.applySavedTracks === 'function')
501 {
502 shareImageSubtitleController.applySavedTracks();
503 }
504 });
505
506 shareVideoPlayer.on('loadedmetadata', shareTrackPrefs.applySavedTracks);
507 shareVideoPlayer.on('loadedplaylist', shareTrackPrefs.applySavedTracks);
508 shareVideoPlayer.on('volumechange', () =>
509 {
510 const v = Number(shareVideoPlayer.volume());
511 if (Number.isFinite(v)) saveShareVideoPrefs({ volume: v });
512 });
513
514 wrap.style.display = '';
515 })
516 .catch(() =>
517 {
518 wrap.innerHTML = '<div class="no-preview">No preview available.</div>';
519 wrap.style.display = '';
520 });
521 return;
522 } else if (audioExts.includes(ext))
523 {
524 el = document.createElement('audio');
525 el.src = previewUrl;
526 el.controls = true;
527 } else if (textExts.includes(ext))
528 {
529 fetch(previewUrl)
530 .then(r =>
531 {
532 if (!r.ok) throw new Error('preview failed');
533 return r.text();
534 })
535 .then(text =>
536 {
537 const pre = document.createElement('pre');
538 pre.textContent = text;
539 wrap.innerHTML = '';
540 wrap.appendChild(pre);
541 wrap.style.display = '';
542 })
543 .catch(() => { /* no preview available */ });
544 return;
545 } else
546 {
547 return; // no preview
548 }
549
550 el.addEventListener('error', () => { wrap.innerHTML = '<div class="no-preview">No preview available.</div>'; wrap.style.display = ''; });
551 wrap.innerHTML = '';
552 wrap.appendChild(el);
553 wrap.style.display = '';
554 }
555
556 // ── Boot ─────────────────────────────────────────────────────────────────
557 function showError (title, msg)
558 {
559 document.getElementById('loading').style.display = 'none';
560 document.getElementById('content').style.display = 'none';
561 document.getElementById('error-title').textContent = title;
562 document.getElementById('error-msg').textContent = msg;
563 document.getElementById('error-state').style.display = '';
564 }
565
566 function formatBytes (bytes)
567 {
568 if (bytes == null) return '';
569 if (bytes < 1024) return bytes + ' B';
570 if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB';
571 if (bytes < 1024 * 1024 * 1024) return (bytes / (1024 * 1024)).toFixed(1) + ' MB';
572 return (bytes / (1024 * 1024 * 1024)).toFixed(2) + ' GB';
573 }
574
575 fetch(`/api/share/info/${token}`)
576 .then(r => r.json().then(d => ({ ok: r.ok, status: r.status, data: d })))
577 .then(({ ok, status, data }) =>
578 {
579 document.getElementById('loading').style.display = 'none';
580
581 if (status === 410)
582 {
583 showError('Link expired', 'This share link is no longer valid.');
584 return;
585 }
586 if (!ok)
587 {
588 showError('Not found', data.error || 'This link does not exist.');
589 return;
590 }
591
592 expiresAt = data.expiresAt;
593 startTimer();
594
595 document.getElementById('file-name').textContent = data.name;
596 const metaLine = data.isDir ? 'Folder (download as .zip)' : 'File';
597 document.getElementById('file-meta').textContent = metaLine;
598
599 const dlBtn = document.getElementById('btn-download');
600 dlBtn.href = `/api/share/download/${token}`;
601 if (data.isDir)
602 {
603 document.getElementById('btn-download-label').textContent = 'Download as .zip';
604 dlBtn.download = data.name + '.zip';
605 } else
606 {
607 document.getElementById('btn-download-label').textContent = 'Download';
608 dlBtn.download = data.name;
609 }
610
611 document.getElementById('content').style.display = '';
612
613 tryPreview(data.name, data.isDir);
614 })
615 .catch(() =>
616 {
617 showError('Error', 'Could not reach the server.');
618 });
619
620 window.addEventListener('pagehide', cleanupShareVideoPlayer);
621 window.addEventListener('beforeunload', cleanupShareVideoPlayer);
622 }());
623 </script>
624 <script src="/vendor/video.js/video.min.js"></script>
625</body>
626
627</html>