MCPcopy Index your code
hub / github.com/OpenSignLabs/OpenSign / handleFileUpload

Function handleFileUpload

apps/OpenSign/src/components/pdf/PdfHeader.jsx:70–157  ·  view source on GitHub ↗
(e)

Source from the content-addressed store, hash-verified

68 };
69
70 const handleFileUpload = async (e) => {
71 const file = e.target.files[0];
72 if (!file) {
73 alert(t("please-select-pdf"));
74 return;
75 }
76 if (!file.type.includes("pdf")) {
77 alert(t("only-pdf-allowed"));
78 return;
79 }
80 const fileSize =
81 maxFileSize;
82 const pdfsize = file?.size;
83 const fileSizeBytes = fileSize * 1024 * 1024;
84 if (pdfsize > fileSizeBytes) {
85 alert(`${t("file-alert-1")} ${fileSize} MB`);
86 removeFile(e);
87 return;
88 }
89 try {
90 let uploadedPdfBytes = await file.arrayBuffer();
91 try {
92 uploadedPdfBytes = await flattenPdf(uploadedPdfBytes);
93 } catch (err) {
94 if (err?.message?.includes("is encrypted")) {
95 try {
96 const pdfFile = await decryptPdf(file, "");
97 const pdfArrayBuffer = await getFileAsArrayBuffer(pdfFile);
98 uploadedPdfBytes = await flattenPdf(pdfArrayBuffer);
99 } catch (err) {
100 if (err?.response?.status === 401) {
101 const password = prompt(
102 `PDF "${file.name}" is password-protected. Enter password:`
103 );
104 if (password) {
105 try {
106 const pdfFile = await decryptPdf(file, password);
107 const pdfArrayBuffer = await getFileAsArrayBuffer(pdfFile);
108 uploadedPdfBytes = await flattenPdf(pdfArrayBuffer);
109 // Upload the file to Parse Server
110 } catch (err) {
111 console.error("Incorrect password or decryption failed", err);
112 alert(t("incorrect-password-or-decryption-failed"));
113 }
114 } else {
115 alert(t("provide-password"));
116 }
117 } else {
118 console.log("Err ", err);
119 alert(t("error-uploading-pdf"));
120 }
121 }
122 } else {
123 alert(t("error-uploading-pdf"));
124 }
125 }
126 const uploadedPdfDoc = await PDFDocument.load(uploadedPdfBytes, {
127 ignoreEncryption: true

Callers

nothing calls this directly

Calls 5

flattenPdfFunction · 0.90
decryptPdfFunction · 0.90
getFileAsArrayBufferFunction · 0.90
base64ToArrayBufferFunction · 0.90
removeFileFunction · 0.70

Tested by

no test coverage detected