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';
import pdfjs from 'pdfjs-dist/es5/build/pdf.js';
import buildinfo from 'consts:buildinfo';
import {name as pkgName} from './../package.json';
import {getPDFSignatureCount} from './utils.js';
import {getPDFSignatureCount, readBinaryFileContent} from './utils.js';
const i18n = createI18nInstance();
......@@ -209,36 +209,29 @@ export class PdfPreview extends ScopedElementsMixin(DBPLitElement) {
this.isShowPlacement = isShowPlacement;
this.isShowPage = true;
let reader = new FileReader();
reader.onload = async () => {
//this.isPageLoaded = false;
const data = reader.result;
const data = await readBinaryFileContent(file);
// get handle of pdf document
try {
this.pdfDoc = await pdfjs.getDocument({data: data}).promise;
} catch (error) {
console.error(error);
return;
}
// total pages in pdf
this.totalPages = this.pdfDoc.numPages;
const page = placementData.currentPage || 1;
// get handle of pdf document
try {
this.pdfDoc = await pdfjs.getDocument({data: data}).promise;
} catch (error) {
console.error(error);
return;
}
// show the first page
// if the placementData has no values we want to initialize the signature position
await this.showPage(page, placementData["scaleX"] === undefined);
// total pages in pdf
this.totalPages = this.pdfDoc.numPages;
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"
await this.showPage(page);
};
this.isPageLoaded = true;
reader.readAsBinaryString(file);
// fix width adaption after "this.isPageLoaded = true"
await this.showPage(page);
console.log(`Signature count: ${await getPDFSignatureCount(file)}`);
}
......
......@@ -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.
*
......@@ -94,22 +113,10 @@ export const getPDFSignatureCount = async (file) => {
const sigRegex = new RegExp(
"/Type\\s*/Sig\\s*/Filter\\s*/Adobe.PPKLite\\s*/SubFilter\\s*(/ETSI\\.CAdES\\.detached|/adbe\\.pkcs7\\.detached)",
"g");
const promise = new Promise((resolve, reject) => {
let reader = new FileReader();
reader.onload = async () => {
let result = reader.result;
let matches = 0;
while (sigRegex.exec(result) !== null) {
matches++;
}
resolve(matches);
};
reader.onerror = async () => {
reject(reader.error);
};
reader.readAsBinaryString(file);
});
return promise;
const content = await readBinaryFileContent(file);
let matches = 0;
while (sigRegex.exec(content) !== null) {
matches++;
}
return matches;
};
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment