| 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> |
| 245 | (function () |
| 246 | { |
| 247 | 'use strict'; |
| 248 | |
| 249 | const token = location.pathname.split('/').pop(); |
| 250 | const VIDEO_FORWARD_BUFFER_SECONDS = 60 * 60; |
| 251 | const SHARE_VIDEO_PREFS_KEY = 'filemanager.share-video-prefs.v1'; |
| 252 | const VIDEO_TEXT_TRACK_SETTINGS_KEY = 'vjs-text-track-settings'; |
| 253 | const VIDEO_TEXT_TRACK_DEFAULT_SETTINGS_RAW = JSON.stringify({ |
| 254 | color: '#fff', |
| 255 | textOpacity: '1', |
| 256 | backgroundColor: '#000', |
| 257 | backgroundOpacity: '0', |
| 258 | windowColor: '#000', |
| 259 | windowOpacity: '0', |
| 260 | fontPercent: '1.00', |
| 261 | edgeStyle: 'uniform', |
| 262 | fontFamily: 'proportionalSansSerif', |
| 263 | }); |
| 264 | let expiresAt = null; |
| 265 | let timerInterval = null; |
| 266 | let shareVideoPlayer = null; |
| 267 | let shareTrackPrefs = null; |
| 268 | |
| 269 | function loadShareVideoPrefs () |
| 270 | { |
| 271 | try |
| 272 | { |
| 273 | const raw = localStorage.getItem(SHARE_VIDEO_PREFS_KEY); |
| 274 | const parsed = raw ? JSON.parse(raw) : {}; |
| 275 | if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return {}; |
| 276 | return parsed; |
| 277 | } catch |
| 278 | { |
| 279 | return {}; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | function saveShareVideoPrefs (patch) |
| 284 | { |
| 285 | try |
| 286 | { |
| 287 | const current = loadShareVideoPrefs(); |
| 288 | localStorage.setItem(SHARE_VIDEO_PREFS_KEY, JSON.stringify({ ...current, ...(patch || {}) })); |
| 289 | } catch |
| 290 | { |
| 291 | // Ignore localStorage failures. |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | function applyShareVideoTextTrackPrefs () |
| 296 | { |
| 297 | try |
| 298 | { |
| 299 | const prefs = loadShareVideoPrefs(); |
| 300 | const raw = String(prefs.textTrackSettings || ''); |
| 301 | let parsed = null; |
| 302 | if (raw) |
| 303 | { |
| 304 | try |
| 305 | { |
| 306 | const candidate = JSON.parse(raw); |
| 307 | if (candidate && typeof candidate === 'object' && !Array.isArray(candidate)) |
| 308 | { |
| 309 | parsed = raw; |
| 310 | } |
| 311 | } catch |
| 312 | { |
| 313 | parsed = null; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | const effective = parsed || VIDEO_TEXT_TRACK_DEFAULT_SETTINGS_RAW; |
| 318 | localStorage.setItem(VIDEO_TEXT_TRACK_SETTINGS_KEY, effective); |
| 319 | |
| 320 | if (parsed !== effective) |
| 321 | { |
| 322 | saveShareVideoPrefs({ textTrackSettings: effective }); |
| 323 | } |
| 324 | } catch |
| 325 | { |
| 326 | // Ignore localStorage failures. |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | function captureShareVideoTextTrackPrefs () |
| 331 | { |
| 332 | try |
| 333 | { |
| 334 | const raw = localStorage.getItem(VIDEO_TEXT_TRACK_SETTINGS_KEY); |
| 335 | if (typeof raw === 'string' && raw) |
| 336 | { |
| 337 | saveShareVideoPrefs({ textTrackSettings: raw }); |
| 338 | } |
| 339 | } catch |
| 340 | { |
| 341 | // Ignore localStorage failures. |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | function normaliseTrackName (name) |
| 346 | { |
| 347 | return String(name || '').trim().toLowerCase(); |
| 348 | } |
| 349 | |
| 350 | function getTrackName (track) |
| 351 | { |
| 352 | if (!track || typeof track !== 'object') return ''; |
| 353 | const label = String(track.label || '').trim(); |
| 354 | if (label) return label; |
| 355 | const name = String(track.name || '').trim(); |
| 356 | if (name) return name; |
| 357 | const language = String(track.language || '').trim(); |
| 358 | if (language) return language; |
| 359 | const id = String(track.id || '').trim(); |
| 360 | if (id) return id; |
| 361 | return ''; |
| 362 | } |
| 363 | |
| 364 | function trackListToArray (trackList) |
| 365 | { |
| 366 | if (!trackList || typeof trackList.length !== 'number') return []; |
| 367 | const out = []; |
| 368 | for (let i = 0; i < trackList.length; i++) |
| 369 | { |
| 370 | const track = trackList[i] || (typeof trackList.item === 'function' ? trackList.item(i) : null); |
| 371 | if (track) out.push(track); |
| 372 | } |
| 373 | return out; |
| 374 | } |
| 375 | |
| 376 | function subtitleTracksFromPlayer (player) |
| 377 | { |
| 378 | if (!player || typeof player.textTracks !== 'function') return []; |
| 379 | return trackListToArray(player.textTracks()).filter((track) => |
| 380 | { |
| 381 | const kind = String(track.kind || '').toLowerCase(); |
| 382 | return kind === 'subtitles' || kind === 'captions'; |
| 383 | }); |
| 384 | } |
| 385 | |
| 386 | function audioTracksFromPlayer (player) |
| 387 | { |
| 388 | if (!player || typeof player.audioTracks !== 'function') return []; |
| 389 | return trackListToArray(player.audioTracks()); |
| 390 | } |
| 391 | |
| 392 | function applySavedSubtitleTrack (player, savedName) |
| 393 | { |
| 394 | if (typeof savedName !== 'string') return; |
| 395 | const tracks = subtitleTracksFromPlayer(player); |
| 396 | if (!tracks.length) return; |
| 397 | |
| 398 | const wanted = normaliseTrackName(savedName); |
| 399 | if (!wanted) |
| 400 | { |
| 401 | for (const track of tracks) track.mode = 'disabled'; |
| 402 | return; |
| 403 | } |
| 404 | |
| 405 | const match = tracks.find((track) => normaliseTrackName(getTrackName(track)) === wanted); |
| 406 | if (!match) return; |
| 407 | |
| 408 | for (const track of tracks) |
| 409 | { |
| 410 | track.mode = track === match ? 'showing' : 'disabled'; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | function applySavedAudioTrack (player, savedName) |
| 415 | { |
| 416 | if (typeof savedName !== 'string') return; |
| 417 | const tracks = audioTracksFromPlayer(player); |
| 418 | if (!tracks.length) return; |
| 419 | |
| 420 | const wanted = normaliseTrackName(savedName); |
| 421 | if (!wanted) return; |
| 422 | |
| 423 | const match = tracks.find((track) => normaliseTrackName(getTrackName(track)) === wanted); |
| 424 | if (!match) return; |
| 425 | |
| 426 | for (const track of tracks) |
| 427 | { |
| 428 | track.enabled = track === match; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | function getSelectedSubtitleTrackName (player) |
| 433 | { |
| 434 | const tracks = subtitleTracksFromPlayer(player); |
| 435 | if (!tracks.length) return null; |
| 436 | const active = tracks.find((track) => String(track.mode || '').toLowerCase() === 'showing'); |
| 437 | return active ? getTrackName(active) : ''; |
| 438 | } |
| 439 | |
| 440 | function getSelectedAudioTrackName (player) |
| 441 | { |
| 442 | const tracks = audioTracksFromPlayer(player); |
| 443 | if (!tracks.length) return null; |
| 444 | const active = tracks.find((track) => !!track.enabled); |
| 445 | return active ? getTrackName(active) : ''; |
| 446 | } |
| 447 | |
| 448 | function bindShareVideoTrackPrefs (player, prefs) |
| 449 | { |
| 450 | const applySavedTracks = () => |
| 451 | { |
| 452 | applySavedSubtitleTrack(player, prefs.subtitleTrackName); |
| 453 | applySavedAudioTrack(player, prefs.audioTrackName); |
| 454 | }; |
| 455 | |
| 456 | const captureSelectedTracks = () => |
| 457 | { |
| 458 | const subtitleTrackName = getSelectedSubtitleTrackName(player); |
| 459 | const audioTrackName = getSelectedAudioTrackName(player); |
| 460 | const patch = {}; |
| 461 | |
| 462 | if (subtitleTrackName !== null) patch.subtitleTrackName = subtitleTrackName; |
| 463 | if (audioTrackName !== null) patch.audioTrackName = audioTrackName; |
| 464 | |
| 465 | if (Object.keys(patch).length) saveShareVideoPrefs(patch); |
| 466 | }; |
| 467 | |
| 468 | const textTrackList = typeof player.textTracks === 'function' ? player.textTracks() : null; |
| 469 | const audioTrackList = typeof player.audioTracks === 'function' ? player.audioTracks() : null; |
| 470 | const onTrackListChange = () => captureSelectedTracks(); |
| 471 | |
| 472 | if (textTrackList && typeof textTrackList.addEventListener === 'function') |
| 473 | { |
| 474 | textTrackList.addEventListener('change', onTrackListChange); |
| 475 | } |
| 476 | |
| 477 | if (audioTrackList && typeof audioTrackList.addEventListener === 'function') |
| 478 | { |
| 479 | audioTrackList.addEventListener('change', onTrackListChange); |
| 480 | } |
| 481 | |
| 482 | return { |
| 483 | applySavedTracks, |
| 484 | captureSelectedTracks, |
| 485 | detach: () => |
| 486 | { |
| 487 | if (textTrackList && typeof textTrackList.removeEventListener === 'function') |
| 488 | { |
| 489 | textTrackList.removeEventListener('change', onTrackListChange); |
| 490 | } |
| 491 | if (audioTrackList && typeof audioTrackList.removeEventListener === 'function') |
| 492 | { |
| 493 | audioTrackList.removeEventListener('change', onTrackListChange); |
| 494 | } |
| 495 | }, |
| 496 | }; |
| 497 | } |
| 498 | |
| 499 | function cleanupShareVideoPlayer () |
| 500 | { |
| 501 | if (!shareVideoPlayer) return; |
| 502 | |
| 503 | try { shareTrackPrefs && shareTrackPrefs.captureSelectedTracks(); } catch { /* ignore */ } |
| 504 | try { captureShareVideoTextTrackPrefs(); } catch { /* ignore */ } |
| 505 | try { shareTrackPrefs && shareTrackPrefs.detach(); } catch { /* ignore */ } |
| 506 | try { shareVideoPlayer.dispose(); } catch { /* ignore */ } |
| 507 | |
| 508 | shareVideoPlayer = null; |
| 509 | shareTrackPrefs = null; |
| 510 | } |
| 511 | |
| 512 | function configureVideoJsBuffering () |
| 513 | { |
| 514 | if (!window.videojs || !window.videojs.Vhs || window.videojs.__filemanagerBufferingConfigured) return; |
| 515 | window.videojs.__filemanagerBufferingConfigured = true; |
| 516 | |
| 517 | try |
| 518 | { |
| 519 | window.videojs.Vhs.GOAL_BUFFER_LENGTH = VIDEO_FORWARD_BUFFER_SECONDS; |
| 520 | window.videojs.Vhs.MAX_GOAL_BUFFER_LENGTH = VIDEO_FORWARD_BUFFER_SECONDS; |
| 521 | } catch |
| 522 | { |
| 523 | // Ignore builds that do not expose VHS buffer configuration. |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | // ── Timer ─────────────────────────────────────────────────────────────── |
| 528 | function startTimer () |
| 529 | { |
| 530 | const bar = document.getElementById('timer-bar'); |
| 531 | const clock = document.getElementById('timer-clock'); |
| 532 | bar.style.display = ''; |
| 533 | |
| 534 | function tick () |
| 535 | { |
| 536 | const remaining = expiresAt - Date.now(); |
| 537 | if (remaining <= 0) |
| 538 | { |
| 539 | clock.textContent = '00:00:00'; |
| 540 | bar.classList.add('expired'); |
| 541 | bar.querySelector('span').textContent = 'Expired —'; |
| 542 | const dlBtn = document.getElementById('btn-download'); |
| 543 | if (dlBtn) dlBtn.setAttribute('disabled', 'disabled'); |
| 544 | clearInterval(timerInterval); |
| 545 | return; |
| 546 | } |
| 547 | const totalSecs = Math.floor(remaining / 1000); |
| 548 | const hh = String(Math.floor(totalSecs / 3600)).padStart(2, '0'); |
| 549 | const mm = String(Math.floor((totalSecs % 3600) / 60)).padStart(2, '0'); |
| 550 | const ss = String(totalSecs % 60).padStart(2, '0'); |
| 551 | clock.textContent = `${hh}:${mm}:${ss}`; |
| 552 | } |
| 553 | |
| 554 | tick(); |
| 555 | timerInterval = setInterval(tick, 1000); |
| 556 | } |
| 557 | |
| 558 | // ── Preview ────────────────────────────────────────────────────────────── |
| 559 | function normaliseVideoDuration (player, durationSeconds) |
| 560 | { |
| 561 | const duration = Number(durationSeconds); |
| 562 | if (!player || !Number.isFinite(duration) || duration <= 0) return; |
| 563 | |
| 564 | let originalDuration = null; |
| 565 | let durationWasChanged = false; |
| 566 | try |
| 567 | { |
| 568 | originalDuration = typeof player.duration === 'function' ? player.duration.bind(player) : null; |
| 569 | installDurationFallback(player, originalDuration, duration); |
| 570 | if (player.__filemanagerDurationApplied) return; |
| 571 | |
| 572 | const current = originalDuration ? Number(originalDuration()) : NaN; |
| 573 | const withinTolerance = Number.isFinite(current) && current >= duration * 0.95; |
| 574 | console.log(`Current duration: ${formatDurationForDebug(current)}, Target time: ${formatDurationForDebug(duration)}, within 95%? ${withinTolerance ? 'yes' : 'no'}`); |
| 575 | if (withinTolerance) |
| 576 | { |
| 577 | player.__filemanagerDurationApplied = true; |
| 578 | return; |
| 579 | } |
| 580 | |
| 581 | if (originalDuration) |
| 582 | { |
| 583 | durationWasChanged = true; |
| 584 | originalDuration(duration); |
| 585 | } |
| 586 | } catch |
| 587 | { |
| 588 | // Ignore players that do not support setting duration. |
| 589 | } |
| 590 | |
| 591 | try |
| 592 | { |
| 593 | const updated = originalDuration ? Number(originalDuration()) : NaN; |
| 594 | if (Number.isFinite(updated) && updated >= duration * 0.95) |
| 595 | { |
| 596 | player.__filemanagerDurationApplied = true; |
| 597 | if (durationWasChanged && typeof player.trigger === 'function') player.trigger('durationchange'); |
| 598 | return; |
| 599 | } |
| 600 | |
| 601 | player.__filemanagerDurationApplied = true; |
| 602 | if (typeof player.trigger === 'function') player.trigger('durationchange'); |
| 603 | } catch |
| 604 | { |
| 605 | // Ignore duration fallback failures. |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | function installDurationFallback (player, originalDuration, duration) |
| 610 | { |
| 611 | if (!player || !originalDuration || player.__filemanagerDurationFallback) return; |
| 612 | |
| 613 | player.__filemanagerDurationFallback = true; |
| 614 | player.duration = function durationWithFallback (...args) |
| 615 | { |
| 616 | if (args.length) return originalDuration(...args); |
| 617 | const current = Number(originalDuration()); |
| 618 | return Number.isFinite(current) && current >= duration * 0.95 ? current : duration; |
| 619 | }; |
| 620 | } |
| 621 | |
| 622 | function formatDurationForDebug (seconds) |
| 623 | { |
| 624 | const value = Number(seconds); |
| 625 | if (!Number.isFinite(value) || value < 0) return 'unknown'; |
| 626 | |
| 627 | const totalSeconds = Math.floor(value); |
| 628 | const hh = String(Math.floor(totalSeconds / 3600)).padStart(2, '0'); |
| 629 | const mm = String(Math.floor((totalSeconds % 3600) / 60)).padStart(2, '0'); |
| 630 | const ss = String(totalSeconds % 60).padStart(2, '0'); |
| 631 | return `${hh}:${mm}:${ss}`; |
| 632 | } |
| 633 | |
| 634 | function ensureVideoPlaybackUi (player, durationSeconds) |
| 635 | { |
| 636 | if (!player || typeof player.on !== 'function') return; |
| 637 | |
| 638 | const forceVodUi = () => |
| 639 | { |
| 640 | normaliseVideoDuration(player, durationSeconds); |
| 641 | |
| 642 | try |
| 643 | { |
| 644 | if (player.liveTracker && typeof player.liveTracker.stopTracking === 'function') |
| 645 | { |
| 646 | player.liveTracker.stopTracking(); |
| 647 | } |
| 648 | } catch |
| 649 | { |
| 650 | // Ignore live tracker availability issues. |
| 651 | } |
| 652 | |
| 653 | try |
| 654 | { |
| 655 | if (typeof player.removeClass === 'function') |
| 656 | { |
| 657 | player.removeClass('vjs-live'); |
| 658 | } |
| 659 | } catch |
| 660 | { |
| 661 | // Ignore class update failures. |
| 662 | } |
| 663 | }; |
| 664 | |
| 665 | player.ready(forceVodUi); |
| 666 | player.on('loadedmetadata', forceVodUi); |
| 667 | player.on('durationchange', forceVodUi); |
| 668 | player.on('loadedplaylist', forceVodUi); |
| 669 | } |
| 670 | |
| 671 | function tryPreview (name, isDir) |
| 672 | { |
| 673 | if (isDir) return; // no preview for folders |
| 674 | |
| 675 | const wrap = document.getElementById('preview-wrap'); |
| 676 | const ext = name.split('.').pop().toLowerCase(); |
| 677 | |
| 678 | const imageExts = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg', 'bmp', 'avif', 'ico']; |
| 679 | const videoExts = ['mp4', 'webm', 'ogv', 'mov', 'avi', 'mkv']; |
| 680 | const audioExts = ['mp3', 'wav', 'ogg', 'flac', 'aac', 'opus', 'm4a']; |
| 681 | 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']; |
| 682 | |
| 683 | const previewUrl = `/api/share/preview/${token}`; |
| 684 | let el; |
| 685 | |
| 686 | if (imageExts.includes(ext)) |
| 687 | { |
| 688 | el = document.createElement('img'); |
| 689 | el.src = previewUrl; |
| 690 | el.alt = name; |
| 691 | } else if (videoExts.includes(ext)) |
| 692 | { |
| 693 | fetch(`/api/share/video-stream/${token}`) |
| 694 | .then(r => r.json().then(d => ({ ok: r.ok, data: d }))) |
| 695 | .then(({ ok, data }) => |
| 696 | { |
| 697 | if (!ok) throw new Error(data.error || 'Streaming failed'); |
| 698 | configureVideoJsBuffering(); |
| 699 | applyShareVideoTextTrackPrefs(); |
| 700 | const prefs = loadShareVideoPrefs(); |
| 701 | |
| 702 | const video = document.createElement('video'); |
| 703 | video.className = 'video-js vjs-big-play-centered'; |
| 704 | video.controls = true; |
| 705 | video.preload = 'auto'; |
| 706 | video.playsInline = true; |
| 707 | |
| 708 | wrap.innerHTML = ''; |
| 709 | wrap.appendChild(video); |
| 710 | |
| 711 | cleanupShareVideoPlayer(); |
| 712 | |
| 713 | shareVideoPlayer = window.videojs(video, { |
| 714 | controls: true, |
| 715 | fluid: true, |
| 716 | responsive: true, |
| 717 | preload: 'auto', |
| 718 | controlBar: { |
| 719 | currentTimeDisplay: true, |
| 720 | timeDivider: true, |
| 721 | durationDisplay: true, |
| 722 | remainingTimeDisplay: false, |
| 723 | }, |
| 724 | sources: [{ src: data.playlistUrl, type: 'application/x-mpegURL' }], |
| 725 | tracks: (data.subtitleTracks || []).map(track => ({ |
| 726 | kind: 'subtitles', |
| 727 | src: track.src, |
| 728 | srclang: track.language || 'und', |
| 729 | label: track.label, |
| 730 | default: !!track.default, |
| 731 | })), |
| 732 | }); |
| 733 | |
| 734 | shareTrackPrefs = bindShareVideoTrackPrefs(shareVideoPlayer, prefs); |
| 735 | ensureVideoPlaybackUi(shareVideoPlayer, data.durationSeconds); |
| 736 | |
| 737 | shareVideoPlayer.ready(() => |
| 738 | { |
| 739 | const volume = Number(prefs.volume); |
| 740 | if (Number.isFinite(volume)) |
| 741 | { |
| 742 | shareVideoPlayer.volume(Math.max(0, Math.min(1, volume))); |
| 743 | } |
| 744 | shareTrackPrefs.applySavedTracks(); |
| 745 | }); |
| 746 | |
| 747 | shareVideoPlayer.on('loadedmetadata', shareTrackPrefs.applySavedTracks); |
| 748 | shareVideoPlayer.on('loadedplaylist', shareTrackPrefs.applySavedTracks); |
| 749 | shareVideoPlayer.on('volumechange', () => |
| 750 | { |
| 751 | const v = Number(shareVideoPlayer.volume()); |
| 752 | if (Number.isFinite(v)) saveShareVideoPrefs({ volume: v }); |
| 753 | }); |
| 754 | |
| 755 | wrap.style.display = ''; |
| 756 | }) |
| 757 | .catch(() => |
| 758 | { |
| 759 | wrap.innerHTML = '<div class="no-preview">No preview available.</div>'; |
| 760 | wrap.style.display = ''; |
| 761 | }); |
| 762 | return; |
| 763 | } else if (audioExts.includes(ext)) |
| 764 | { |
| 765 | el = document.createElement('audio'); |
| 766 | el.src = previewUrl; |
| 767 | el.controls = true; |
| 768 | } else if (textExts.includes(ext)) |
| 769 | { |
| 770 | fetch(previewUrl) |
| 771 | .then(r => |
| 772 | { |
| 773 | if (!r.ok) throw new Error('preview failed'); |
| 774 | return r.text(); |
| 775 | }) |
| 776 | .then(text => |
| 777 | { |
| 778 | const pre = document.createElement('pre'); |
| 779 | pre.textContent = text; |
| 780 | wrap.innerHTML = ''; |
| 781 | wrap.appendChild(pre); |
| 782 | wrap.style.display = ''; |
| 783 | }) |
| 784 | .catch(() => { /* no preview available */ }); |
| 785 | return; |
| 786 | } else |
| 787 | { |
| 788 | return; // no preview |
| 789 | } |
| 790 | |
| 791 | el.addEventListener('error', () => { wrap.innerHTML = '<div class="no-preview">No preview available.</div>'; wrap.style.display = ''; }); |
| 792 | wrap.innerHTML = ''; |
| 793 | wrap.appendChild(el); |
| 794 | wrap.style.display = ''; |
| 795 | } |
| 796 | |
| 797 | // ── Boot ───────────────────────────────────────────────────────────────── |
| 798 | function showError (title, msg) |
| 799 | { |
| 800 | document.getElementById('loading').style.display = 'none'; |
| 801 | document.getElementById('content').style.display = 'none'; |
| 802 | document.getElementById('error-title').textContent = title; |
| 803 | document.getElementById('error-msg').textContent = msg; |
| 804 | document.getElementById('error-state').style.display = ''; |
| 805 | } |
| 806 | |
| 807 | function formatBytes (bytes) |
| 808 | { |
| 809 | if (bytes == null) return ''; |
| 810 | if (bytes < 1024) return bytes + ' B'; |
| 811 | if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB'; |
| 812 | if (bytes < 1024 * 1024 * 1024) return (bytes / (1024 * 1024)).toFixed(1) + ' MB'; |
| 813 | return (bytes / (1024 * 1024 * 1024)).toFixed(2) + ' GB'; |
| 814 | } |
| 815 | |
| 816 | fetch(`/api/share/info/${token}`) |
| 817 | .then(r => r.json().then(d => ({ ok: r.ok, status: r.status, data: d }))) |
| 818 | .then(({ ok, status, data }) => |
| 819 | { |
| 820 | document.getElementById('loading').style.display = 'none'; |
| 821 | |
| 822 | if (status === 410) |
| 823 | { |
| 824 | showError('Link expired', 'This share link is no longer valid.'); |
| 825 | return; |
| 826 | } |
| 827 | if (!ok) |
| 828 | { |
| 829 | showError('Not found', data.error || 'This link does not exist.'); |
| 830 | return; |
| 831 | } |
| 832 | |
| 833 | expiresAt = data.expiresAt; |
| 834 | startTimer(); |
| 835 | |
| 836 | document.getElementById('file-name').textContent = data.name; |
| 837 | const metaLine = data.isDir ? 'Folder (download as .zip)' : 'File'; |
| 838 | document.getElementById('file-meta').textContent = metaLine; |
| 839 | |
| 840 | const dlBtn = document.getElementById('btn-download'); |
| 841 | dlBtn.href = `/api/share/download/${token}`; |
| 842 | if (data.isDir) |
| 843 | { |
| 844 | document.getElementById('btn-download-label').textContent = 'Download as .zip'; |
| 845 | dlBtn.download = data.name + '.zip'; |
| 846 | } else |
| 847 | { |
| 848 | document.getElementById('btn-download-label').textContent = 'Download'; |
| 849 | dlBtn.download = data.name; |
| 850 | } |
| 851 | |
| 852 | document.getElementById('content').style.display = ''; |
| 853 | |
| 854 | tryPreview(data.name, data.isDir); |
| 855 | }) |
| 856 | .catch(() => |
| 857 | { |
| 858 | showError('Error', 'Could not reach the server.'); |
| 859 | }); |
| 860 | |
| 861 | window.addEventListener('pagehide', cleanupShareVideoPlayer); |
| 862 | window.addEventListener('beforeunload', cleanupShareVideoPlayer); |
| 863 | }()); |
| 864 | </script> |
| 865 | <script src="/vendor/video.js/video.min.js"></script> |
| 866 | </body> |
| 867 | |
| 868 | </html> |