Skip to content
Snippets Groups Projects
Commit d1beb903 authored by Reiter, Christoph's avatar Reiter, Christoph :snake:
Browse files

Refactor PDF data extraction

Hide the FileReader API behind an asnyc function. This makes it
easier to await the result and is less racy.
parent e89990a0
No related branches found
No related tags found
No related merge requests found
...@@ -10,7 +10,7 @@ import * as commonStyles from '@dbp-toolkit/common/styles'; ...@@ -10,7 +10,7 @@ import * as commonStyles from '@dbp-toolkit/common/styles';
import pdfjs from 'pdfjs-dist/es5/build/pdf.js'; import pdfjs from 'pdfjs-dist/es5/build/pdf.js';
import buildinfo from 'consts:buildinfo'; import buildinfo from 'consts:buildinfo';
import {name as pkgName} from './../package.json'; import {name as pkgName} from './../package.json';
import {getPDFSignatureCount} from './utils.js'; import {getPDFSignatureCount, readBinaryFileContent} from './utils.js';
const i18n = createI18nInstance(); const i18n = createI18nInstance();
...@@ -209,36 +209,29 @@ export class PdfPreview extends ScopedElementsMixin(DBPLitElement) { ...@@ -209,36 +209,29 @@ export class PdfPreview extends ScopedElementsMixin(DBPLitElement) {
this.isShowPlacement = isShowPlacement; this.isShowPlacement = isShowPlacement;
this.isShowPage = true; this.isShowPage = true;
let reader = new FileReader();
reader.onload = async () => { const data = await readBinaryFileContent(file);
//this.isPageLoaded = false;
const data = reader.result;
// get handle of pdf document // get handle of pdf document
try { try {
this.pdfDoc = await pdfjs.getDocument({data: data}).promise; this.pdfDoc = await pdfjs.getDocument({data: data}).promise;
} catch (error) { } catch (error) {
console.error(error); console.error(error);
return;
return; }
}
// total pages in pdf
this.totalPages = this.pdfDoc.numPages;
const page = placementData.currentPage || 1;
// show the first page // total pages in pdf
// if the placementData has no values we want to initialize the signature position this.totalPages = this.pdfDoc.numPages;
await this.showPage(page, placementData["scaleX"] === undefined); const page = placementData.currentPage || 1;
this.isPageLoaded = true; // show the first page
// if the placementData has no values we want to initialize the signature position
await this.showPage(page, placementData["scaleX"] === undefined);
// fix width adaption after "this.isPageLoaded = true" this.isPageLoaded = true;
await this.showPage(page);
};
reader.readAsBinaryString(file); // fix width adaption after "this.isPageLoaded = true"
await this.showPage(page);
console.log(`Signature count: ${await getPDFSignatureCount(file)}`); console.log(`Signature count: ${await getPDFSignatureCount(file)}`);
} }
......
...@@ -81,6 +81,25 @@ export const fabricjs2pdfasPosition = (data) => { ...@@ -81,6 +81,25 @@ export const fabricjs2pdfasPosition = (data) => {
}; };
}; };
/**
* Returns the content of the file
*
* @param {File} file The file to read
* @returns {string} The content
*/
export const readBinaryFileContent = async (file) => {
return new Promise((resolve, reject) => {
let reader = new FileReader();
reader.onload = () => {
resolve(reader.result);
};
reader.onerror = () => {
reject(reader.error);
};
reader.readAsBinaryString(file);
});
};
/** /**
* Given a PDF file returns the amount of signatures found in it. * Given a PDF file returns the amount of signatures found in it.
* *
...@@ -94,22 +113,10 @@ export const getPDFSignatureCount = async (file) => { ...@@ -94,22 +113,10 @@ export const getPDFSignatureCount = async (file) => {
const sigRegex = new RegExp( const sigRegex = new RegExp(
"/Type\\s*/Sig\\s*/Filter\\s*/Adobe.PPKLite\\s*/SubFilter\\s*(/ETSI\\.CAdES\\.detached|/adbe\\.pkcs7\\.detached)", "/Type\\s*/Sig\\s*/Filter\\s*/Adobe.PPKLite\\s*/SubFilter\\s*(/ETSI\\.CAdES\\.detached|/adbe\\.pkcs7\\.detached)",
"g"); "g");
const content = await readBinaryFileContent(file);
const promise = new Promise((resolve, reject) => { let matches = 0;
let reader = new FileReader(); while (sigRegex.exec(content) !== null) {
reader.onload = async () => { matches++;
let result = reader.result; }
let matches = 0; return matches;
while (sigRegex.exec(result) !== null) {
matches++;
}
resolve(matches);
};
reader.onerror = async () => {
reject(reader.error);
};
reader.readAsBinaryString(file);
});
return promise;
}; };
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment