const fragment = location.hash.slice(1);
if (!fragment) {
showError('No decryption key found in URL.');
} else try {
setStatus('Deriving key\u2026');
const rawKeyBytes = decodeKey(fragment);
const id = await hashKey(rawKeyBytes);
const key = await crypto.subtle.importKey(
'raw', rawKeyBytes, { name: 'AES-GCM' }, false, ['decrypt']
);
setStatus('Downloading\u2026');
const response = await fetch('/download/' + id + '/data');
if (response.status === 410) {
showError('This file has expired or reached its download limit.');
} else if (!response.ok) {
showError(`Download failed (${response.status}).`);
} else {
const disposition = response.headers.get('Content-Disposition') || '';
const filename = disposition.match(/filename="?([^"]+)"?/)?.[1] || 'download';
setStatus('Decrypting\u2026');
const plaintext = await decryptFile(await response.arrayBuffer(), key);
showPreview(filename, plaintext);
}
} catch (err) {
showError('Decryption failed: ' + err.message);
}
function setStatus(msg) {
const el = htmx.find('#download-status');
if (el) el.textContent = msg;
}
function escapeHtml(str) {
return str.replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"');
}
function getMimeType(filename) {
const ext = filename.split('.').pop().toLowerCase();
const types = {
jpg: 'image/jpeg', jpeg: 'image/jpeg', png: 'image/png', gif: 'image/gif',
webp: 'image/webp', svg: 'image/svg+xml', bmp: 'image/bmp', ico: 'image/x-icon',
mp4: 'video/mp4', webm: 'video/webm', ogv: 'video/ogg', mov: 'video/quicktime',
mp3: 'audio/mpeg', wav: 'audio/wav', ogg: 'audio/ogg', flac: 'audio/flac',
aac: 'audio/aac', m4a: 'audio/mp4',
txt: 'text/plain', md: 'text/plain', csv: 'text/csv', log: 'text/plain',
json: 'application/json', xml: 'text/xml',
js: 'text/plain', ts: 'text/plain', py: 'text/plain', java: 'text/plain',
c: 'text/plain', cpp: 'text/plain', h: 'text/plain', sh: 'text/plain',
yaml: 'text/plain', yml: 'text/plain', toml: 'text/plain', ini: 'text/plain',
pdf: 'application/pdf',
};
return types[ext] || 'application/octet-stream';
}
function showPreview(filename, plaintext) {
const mimeType = getMimeType(filename);
const blob = new Blob([plaintext], { type: mimeType });
const url = URL.createObjectURL(blob);
window.addEventListener('unload', () => URL.revokeObjectURL(url));
const name = escapeHtml(filename);
const downloadBtn = ``;
let previewHtml;
if (mimeType.startsWith('image/')) {
previewHtml = `
${escapeHtml(preview)}
${downloadBtn}`;
} else {
previewHtml = `