-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
100 lines (90 loc) · 3.3 KB
/
App.tsx
File metadata and controls
100 lines (90 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { useState } from 'react';
import * as pdfDoc from '@peculiar/pdf-doc';
import './App.css';
import FileUploader from './components/FileUploader';
import EncryptionForm from './components/EncryptionForm';
import Notice from './components/Notice';
import { encryptPDF, EncryptionParams } from './utils/pdfEncryption';
function App() {
const [pdfFile, setPdfFile] = useState<File | null>(null);
const [pdfDocument, setPdfDocument] = useState<pdfDoc.PDFDocument | null>(null);
const [hasSignatures, setHasSignatures] = useState<boolean | null>(null);
const [signatureNoticeDismissed, setSignatureNoticeDismissed] = useState(false);
const [isProcessing, setIsProcessing] = useState(false);
const handleFileSelect = (info: { file: File; pdfDocument?: pdfDoc.PDFDocument; hasSignatures?: boolean; }) => {
setPdfFile(info.file);
setPdfDocument(info.pdfDocument ?? null);
setHasSignatures(info.hasSignatures ?? null);
setSignatureNoticeDismissed(false);
};
const handleEncrypt = async (params: EncryptionParams) => {
if (!pdfFile) {
alert('Please select a PDF file first');
return;
}
// If we have a parsed PDFDocument, use it; otherwise fall back to reading the file
setIsProcessing(true);
try {
let encryptedBlob: Blob;
if (pdfDocument) {
encryptedBlob = await encryptPDF(pdfDocument, params);
} else {
// fallback: read raw file and load inside utility
encryptedBlob = await encryptPDF(pdfFile as any, params);
}
// Download the encrypted PDF
const url = URL.createObjectURL(encryptedBlob);
const a = document.createElement('a');
a.href = url;
a.download = `encrypted_${pdfFile.name}`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
} catch (error) {
console.error('Encryption failed:', error);
alert('Failed to encrypt PDF');
} finally {
setIsProcessing(false);
}
};
return (
<div className="app">
<header className="app-header">
<h1>PDF Encryption Tool</h1>
<p>Protect your PDF documents with password encryption</p>
</header>
{/* Informational notice about local processing */}
<Notice
type="info"
title="Local processing"
message={
'Files you upload to this page are not sent anywhere — they are processed and stored only in your browser.'
}
/>
<main className="app-main">
<FileUploader onFileSelect={handleFileSelect} currentFile={pdfFile} />
{pdfFile && (
hasSignatures ? (
!signatureNoticeDismissed && (
<Notice
type="warning"
title="Signed document detected"
message={
'This document contains a digital signature and cannot be protected with password-based encryption. Encryption controls are disabled.'
}
actions={[{ label: 'Dismiss', variant: 'default', onClick: () => setSignatureNoticeDismissed(true) }]}
/>
)
) : (
<EncryptionForm
onEncrypt={handleEncrypt}
isProcessing={isProcessing}
/>
)
)}
</main>
</div>
);
}
export default App;