| 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 | let expiresAt = null; |
| 251 | let timerInterval = null; |
| 252 | let shareVideoPlayer = null; |
| 253 | |
| 254 | // ── Timer ─────────────────────────────────────────────────────────────── |
| 255 | function startTimer () |
| 256 | { |
| 257 | const bar = document.getElementById('timer-bar'); |
| 258 | const clock = document.getElementById('timer-clock'); |
| 259 | bar.style.display = ''; |
| 260 | |
| 261 | function tick () |
| 262 | { |
| 263 | const remaining = expiresAt - Date.now(); |
| 264 | if (remaining <= 0) |
| 265 | { |
| 266 | clock.textContent = '00:00:00'; |
| 267 | bar.classList.add('expired'); |
| 268 | bar.querySelector('span').textContent = 'Expired —'; |
| 269 | const dlBtn = document.getElementById('btn-download'); |
| 270 | if (dlBtn) dlBtn.setAttribute('disabled', 'disabled'); |
| 271 | clearInterval(timerInterval); |
| 272 | return; |
| 273 | } |
| 274 | const totalSecs = Math.floor(remaining / 1000); |
| 275 | const hh = String(Math.floor(totalSecs / 3600)).padStart(2, '0'); |
| 276 | const mm = String(Math.floor((totalSecs % 3600) / 60)).padStart(2, '0'); |
| 277 | const ss = String(totalSecs % 60).padStart(2, '0'); |
| 278 | clock.textContent = `${hh}:${mm}:${ss}`; |
| 279 | } |
| 280 | |
| 281 | tick(); |
| 282 | timerInterval = setInterval(tick, 1000); |
| 283 | } |
| 284 | |
| 285 | // ── Preview ────────────────────────────────────────────────────────────── |
| 286 | function normaliseVideoDuration (player, durationSeconds) |
| 287 | { |
| 288 | const duration = Number(durationSeconds); |
| 289 | if (!player || !Number.isFinite(duration) || duration <= 0) return; |
| 290 | |
| 291 | let originalDuration = null; |
| 292 | let durationWasChanged = false; |
| 293 | try |
| 294 | { |
| 295 | originalDuration = typeof player.duration === 'function' ? player.duration.bind(player) : null; |
| 296 | installDurationFallback(player, originalDuration, duration); |
| 297 | if (player.__filemanagerDurationApplied) return; |
| 298 | |
| 299 | const current = originalDuration ? Number(originalDuration()) : NaN; |
| 300 | const withinTolerance = Number.isFinite(current) && current >= duration * 0.95; |
| 301 | console.log(`Current duration: ${formatDurationForDebug(current)}, Target time: ${formatDurationForDebug(duration)}, within 95%? ${withinTolerance ? 'yes' : 'no'}`); |
| 302 | if (withinTolerance) |
| 303 | { |
| 304 | player.__filemanagerDurationApplied = true; |
| 305 | return; |
| 306 | } |
| 307 | |
| 308 | if (originalDuration) |
| 309 | { |
| 310 | durationWasChanged = true; |
| 311 | originalDuration(duration); |
| 312 | } |
| 313 | } catch |
| 314 | { |
| 315 | // Ignore players that do not support setting duration. |
| 316 | } |
| 317 | |
| 318 | try |
| 319 | { |
| 320 | const updated = originalDuration ? Number(originalDuration()) : NaN; |
| 321 | if (Number.isFinite(updated) && updated >= duration * 0.95) |
| 322 | { |
| 323 | player.__filemanagerDurationApplied = true; |
| 324 | if (durationWasChanged && typeof player.trigger === 'function') player.trigger('durationchange'); |
| 325 | return; |
| 326 | } |
| 327 | |
| 328 | player.__filemanagerDurationApplied = true; |
| 329 | if (typeof player.trigger === 'function') player.trigger('durationchange'); |
| 330 | } catch |
| 331 | { |
| 332 | // Ignore duration fallback failures. |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | function installDurationFallback (player, originalDuration, duration) |
| 337 | { |
| 338 | if (!player || !originalDuration || player.__filemanagerDurationFallback) return; |
| 339 | |
| 340 | player.__filemanagerDurationFallback = true; |
| 341 | player.duration = function durationWithFallback (...args) |
| 342 | { |
| 343 | if (args.length) return originalDuration(...args); |
| 344 | const current = Number(originalDuration()); |
| 345 | return Number.isFinite(current) && current >= duration * 0.95 ? current : duration; |
| 346 | }; |
| 347 | } |
| 348 | |
| 349 | function formatDurationForDebug (seconds) |
| 350 | { |
| 351 | const value = Number(seconds); |
| 352 | if (!Number.isFinite(value) || value < 0) return 'unknown'; |
| 353 | |
| 354 | const totalSeconds = Math.floor(value); |
| 355 | const hh = String(Math.floor(totalSeconds / 3600)).padStart(2, '0'); |
| 356 | const mm = String(Math.floor((totalSeconds % 3600) / 60)).padStart(2, '0'); |
| 357 | const ss = String(totalSeconds % 60).padStart(2, '0'); |
| 358 | return `${hh}:${mm}:${ss}`; |
| 359 | } |
| 360 | |
| 361 | function ensureVideoPlaybackUi (player, durationSeconds) |
| 362 | { |
| 363 | if (!player || typeof player.on !== 'function') return; |
| 364 | |
| 365 | const forceVodUi = () => |
| 366 | { |
| 367 | normaliseVideoDuration(player, durationSeconds); |
| 368 | |
| 369 | try |
| 370 | { |
| 371 | if (player.liveTracker && typeof player.liveTracker.stopTracking === 'function') |
| 372 | { |
| 373 | player.liveTracker.stopTracking(); |
| 374 | } |
| 375 | } catch |
| 376 | { |
| 377 | // Ignore live tracker availability issues. |
| 378 | } |
| 379 | |
| 380 | try |
| 381 | { |
| 382 | if (typeof player.removeClass === 'function') |
| 383 | { |
| 384 | player.removeClass('vjs-live'); |
| 385 | } |
| 386 | } catch |
| 387 | { |
| 388 | // Ignore class update failures. |
| 389 | } |
| 390 | }; |
| 391 | |
| 392 | player.ready(forceVodUi); |
| 393 | player.on('loadedmetadata', forceVodUi); |
| 394 | player.on('durationchange', forceVodUi); |
| 395 | player.on('loadedplaylist', forceVodUi); |
| 396 | } |
| 397 | |
| 398 | function tryPreview (name, isDir) |
| 399 | { |
| 400 | if (isDir) return; // no preview for folders |
| 401 | |
| 402 | const wrap = document.getElementById('preview-wrap'); |
| 403 | const ext = name.split('.').pop().toLowerCase(); |
| 404 | |
| 405 | const imageExts = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg', 'bmp', 'avif', 'ico']; |
| 406 | const videoExts = ['mp4', 'webm', 'ogg', 'mov']; |
| 407 | const audioExts = ['mp3', 'wav', 'ogg', 'flac', 'aac', 'opus', 'm4a']; |
| 408 | 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']; |
| 409 | |
| 410 | const previewUrl = `/api/share/preview/${token}`; |
| 411 | let el; |
| 412 | |
| 413 | if (imageExts.includes(ext)) |
| 414 | { |
| 415 | el = document.createElement('img'); |
| 416 | el.src = previewUrl; |
| 417 | el.alt = name; |
| 418 | } else if (videoExts.includes(ext)) |
| 419 | { |
| 420 | fetch(`/api/share/video-stream/${token}`) |
| 421 | .then(r => r.json().then(d => ({ ok: r.ok, data: d }))) |
| 422 | .then(({ ok, data }) => |
| 423 | { |
| 424 | if (!ok) throw new Error(data.error || 'Streaming failed'); |
| 425 | |
| 426 | const video = document.createElement('video'); |
| 427 | video.className = 'video-js vjs-big-play-centered'; |
| 428 | video.controls = true; |
| 429 | video.preload = 'auto'; |
| 430 | video.playsInline = true; |
| 431 | |
| 432 | wrap.innerHTML = ''; |
| 433 | wrap.appendChild(video); |
| 434 | |
| 435 | if (shareVideoPlayer) |
| 436 | { |
| 437 | try { shareVideoPlayer.dispose(); } catch { /* ignore */ } |
| 438 | } |
| 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 | ensureVideoPlaybackUi(shareVideoPlayer, data.durationSeconds); |
| 461 | |
| 462 | wrap.style.display = ''; |
| 463 | }) |
| 464 | .catch(() => |
| 465 | { |
| 466 | wrap.innerHTML = '<div class="no-preview">No preview available.</div>'; |
| 467 | wrap.style.display = ''; |
| 468 | }); |
| 469 | return; |
| 470 | } else if (audioExts.includes(ext)) |
| 471 | { |
| 472 | el = document.createElement('audio'); |
| 473 | el.src = previewUrl; |
| 474 | el.controls = true; |
| 475 | } else if (textExts.includes(ext)) |
| 476 | { |
| 477 | fetch(previewUrl) |
| 478 | .then(r => |
| 479 | { |
| 480 | if (!r.ok) throw new Error('preview failed'); |
| 481 | return r.text(); |
| 482 | }) |
| 483 | .then(text => |
| 484 | { |
| 485 | const pre = document.createElement('pre'); |
| 486 | pre.textContent = text; |
| 487 | wrap.innerHTML = ''; |
| 488 | wrap.appendChild(pre); |
| 489 | wrap.style.display = ''; |
| 490 | }) |
| 491 | .catch(() => { /* no preview available */ }); |
| 492 | return; |
| 493 | } else |
| 494 | { |
| 495 | return; // no preview |
| 496 | } |
| 497 | |
| 498 | el.addEventListener('error', () => { wrap.innerHTML = '<div class="no-preview">No preview available.</div>'; wrap.style.display = ''; }); |
| 499 | wrap.innerHTML = ''; |
| 500 | wrap.appendChild(el); |
| 501 | wrap.style.display = ''; |
| 502 | } |
| 503 | |
| 504 | // ── Boot ───────────────────────────────────────────────────────────────── |
| 505 | function showError (title, msg) |
| 506 | { |
| 507 | document.getElementById('loading').style.display = 'none'; |
| 508 | document.getElementById('content').style.display = 'none'; |
| 509 | document.getElementById('error-title').textContent = title; |
| 510 | document.getElementById('error-msg').textContent = msg; |
| 511 | document.getElementById('error-state').style.display = ''; |
| 512 | } |
| 513 | |
| 514 | function formatBytes (bytes) |
| 515 | { |
| 516 | if (bytes == null) return ''; |
| 517 | if (bytes < 1024) return bytes + ' B'; |
| 518 | if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB'; |
| 519 | if (bytes < 1024 * 1024 * 1024) return (bytes / (1024 * 1024)).toFixed(1) + ' MB'; |
| 520 | return (bytes / (1024 * 1024 * 1024)).toFixed(2) + ' GB'; |
| 521 | } |
| 522 | |
| 523 | fetch(`/api/share/info/${token}`) |
| 524 | .then(r => r.json().then(d => ({ ok: r.ok, status: r.status, data: d }))) |
| 525 | .then(({ ok, status, data }) => |
| 526 | { |
| 527 | document.getElementById('loading').style.display = 'none'; |
| 528 | |
| 529 | if (status === 410) |
| 530 | { |
| 531 | showError('Link expired', 'This share link is no longer valid.'); |
| 532 | return; |
| 533 | } |
| 534 | if (!ok) |
| 535 | { |
| 536 | showError('Not found', data.error || 'This link does not exist.'); |
| 537 | return; |
| 538 | } |
| 539 | |
| 540 | expiresAt = data.expiresAt; |
| 541 | startTimer(); |
| 542 | |
| 543 | document.getElementById('file-name').textContent = data.name; |
| 544 | const metaLine = data.isDir ? 'Folder (download as .zip)' : 'File'; |
| 545 | document.getElementById('file-meta').textContent = metaLine; |
| 546 | |
| 547 | const dlBtn = document.getElementById('btn-download'); |
| 548 | dlBtn.href = `/api/share/download/${token}`; |
| 549 | if (data.isDir) |
| 550 | { |
| 551 | document.getElementById('btn-download-label').textContent = 'Download as .zip'; |
| 552 | dlBtn.download = data.name + '.zip'; |
| 553 | } else |
| 554 | { |
| 555 | document.getElementById('btn-download-label').textContent = 'Download'; |
| 556 | dlBtn.download = data.name; |
| 557 | } |
| 558 | |
| 559 | document.getElementById('content').style.display = ''; |
| 560 | |
| 561 | tryPreview(data.name, data.isDir); |
| 562 | }) |
| 563 | .catch(() => |
| 564 | { |
| 565 | showError('Error', 'Could not reach the server.'); |
| 566 | }); |
| 567 | }()); |
| 568 | </script> |
| 569 | <script src="/vendor/video.js/video.min.js"></script> |
| 570 | </body> |
| 571 | |
| 572 | </html> |