Skip to content

Commit

Permalink
fix: show error if trying to open encrypted file
Browse files Browse the repository at this point in the history
  • Loading branch information
vegarringdal committed Jul 16, 2020
1 parent ce9aa73 commit b311f5f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/backend/getFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { PDFDocument } from "pdf-lib";

export async function getFile(filePath: string) {
const uint8Array = fs.readFileSync(filePath);
const pdfDoc = await PDFDocument.load(uint8Array);

const pdfDoc = await PDFDocument.load(uint8Array, {
ignoreEncryption: true,
});

return pdfDoc;
}
4 changes: 4 additions & 0 deletions src/backend/getPDFData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { parsePDF } from "./parsePDF";

export async function getPDFData(filePath: string) {
const pdfDoc = await getFile(filePath);
if (pdfDoc.isEncrypted) {
return { isEncrypted: true };
}

const parseData = parsePDF(pdfDoc, filePath);
return parseData;
}
11 changes: 10 additions & 1 deletion src/backend/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ ipcMain.handle("openFile", async () => {

if (file?.length) {
const filedata = await getPDFData(file[0]);
getWindow().webContents.send("openFile", filedata);
if ((filedata as any)?.isEncrypted) {
dialog.showErrorBox(
"read error",
"file is encrypted, this is not supported at the moment"
);
// return nothing
getWindow().webContents.send("openFile");
} else {
getWindow().webContents.send("openFile", filedata);
}
} else {
getWindow().webContents.send("openFile");
}
Expand Down

0 comments on commit b311f5f

Please sign in to comment.