import { encrypt } from "./crypto"; import { getMode, setupModeRadios } from "./shared"; async function uploadFile() { ( document.getElementById("upload-dialog") as HTMLDialogElement | null )?.showModal(); const passwordInput = document.getElementById( "password", ) as HTMLInputElement | null; const fileInput = document.getElementById("file") as HTMLInputElement | null; const filenameInput = document.getElementById( "filename", ) as HTMLInputElement | null; const encryptedInput = document.getElementById( "encrypted", ) as HTMLInputElement | null; const form = document.getElementById("uploadForm") as HTMLFormElement | null; if ( !passwordInput || !fileInput || !filenameInput || !encryptedInput || !form ) { console.error("Missing required elements."); console.error( passwordInput, fileInput, filenameInput, encryptedInput, form, ); return; } const password = passwordInput.value; if (!password) { form.submit(); return; } const file = fileInput.files?.[0]; if (!file) { return; } const content = await file.arrayBuffer(); const encryptedContent = await encrypt(new Uint8Array(content), password); const myFile = new File([encryptedContent as BlobPart], file.name); const dataTransfer = new DataTransfer(); dataTransfer.items.add(myFile); fileInput.files = dataTransfer.files; encryptedInput.checked = true; form.submit(); } // Decides per submit whether to encrypt in the browser (client mode) or let the // form POST normally so the server encrypts (server mode / no password). function onUploadSubmit(): boolean { const passwordInput = document.getElementById( "password", ) as HTMLInputElement | null; if (getMode("encrypt_mode") === "client" && passwordInput?.value) { uploadFile(); return false; } return true; } // reset() first: it reverts controls to their HTML defaults (incl. the radios), // so apply the saved mode preference afterwards. (document.getElementById("uploadForm") as HTMLFormElement | null)?.reset(); setupModeRadios("encrypt_mode"); Object.assign(window, { uploadFile }); Object.assign(window, { onUploadSubmit }); //disable bfcache, otherwise dialog will stay open when navigating back window.addEventListener("unload", () => {}); window.addEventListener("beforeunload", () => {});