import { showLoader, hideLoader, showAlert } from '../ui.js'; import { downloadFile, initializeQpdf, readFileAsArrayBuffer, } from '../utils/helpers.js'; import { state } from '../state.js'; export async function decrypt() { const file = state.files[0]; const password = ( document.getElementById('password-input') as HTMLInputElement )?.value; if (!password?.trim()) { showAlert('Input Required', 'Please enter the PDF password.'); return; } const inputPath = '/input.pdf'; const outputPath = '/output.pdf'; let qpdf: any; try { showLoader('Initializing decryption...'); qpdf = await initializeQpdf(); showLoader('Reading encrypted PDF...'); const fileBuffer = await readFileAsArrayBuffer(file); const uint8Array = new Uint8Array(fileBuffer as ArrayBuffer); qpdf.FS.writeFile(inputPath, uint8Array); showLoader('Decrypting PDF...'); const args = [inputPath, '--password=' + password, '--decrypt', outputPath]; try { qpdf.callMain(args); } catch (qpdfError: any) { console.error('qpdf execution error:', qpdfError); if ( qpdfError.message?.includes('invalid password') || qpdfError.message?.includes('password') ) { throw new Error('INVALID_PASSWORD'); } throw qpdfError; } showLoader('Preparing download...'); const outputFile = qpdf.FS.readFile(outputPath, { encoding: 'binary' }); if (!outputFile || outputFile.length === 0) { throw new Error('Decryption resulted in an empty file.'); } const blob = new Blob([outputFile], { type: 'application/pdf' }); downloadFile(blob, `unlocked-${file.name}`); hideLoader(); showAlert( 'Success', 'PDF decrypted successfully! Your download has started.' ); } catch (error: any) { console.error('Error during PDF decryption:', error); hideLoader(); if (error.message === 'INVALID_PASSWORD') { showAlert( 'Incorrect Password', 'The password you entered is incorrect. Please try again.' ); } else if (error.message?.includes('password')) { showAlert( 'Password Error', 'Unable to decrypt the PDF with the provided password.' ); } else { showAlert( 'Decryption Failed', `An error occurred: ${error.message || 'The password you entered is wrong or the file is corrupted.'}` ); } } finally { try { if (qpdf?.FS) { try { qpdf.FS.unlink(inputPath); } catch (e) { console.warn('Failed to unlink input file:', e); } try { qpdf.FS.unlink(outputPath); } catch (e) { console.warn('Failed to unlink output file:', e); } } } catch (cleanupError) { console.warn('Failed to cleanup WASM FS:', cleanupError); } } }