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

Merge branch 'read-signature-count' into 'master'

Add a funtion for guessing the signature count of a PDF file

See merge request dbp/apps/signature!9
parents b7c6b5e2 504b663c
Branches
No related tags found
1 merge request!9Add a funtion for guessing the signature count of a PDF file
Pipeline #15462 passed
......@@ -10,6 +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';
const i18n = createI18nInstance();
......@@ -238,6 +239,8 @@ export class PdfPreview extends ScopedElementsMixin(DBPLitElement) {
};
reader.readAsBinaryString(file);
console.log(`Signature count: ${await getPDFSignatureCount(file)}`);
}
getSignatureRect() {
......
......@@ -80,3 +80,36 @@ export const fabricjs2pdfasPosition = (data) => {
p: data.currentPage
};
};
/**
* Given a PDF file returns the amount of signatures found in it.
*
* Note that this uses an heuristic, so the result can be wrong
* (improvements welcome).
*
* @param {File} file The PDF file object
* @returns {number} The amount of signatures found
*/
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;
};
\ No newline at end of file
......@@ -2,6 +2,7 @@ import {assert} from 'chai';
import '../src/dbp-official-signature-pdf-upload';
import '../src/dbp-signature.js';
import {getPDFSignatureCount} from '../src/utils.js';
suite('dbp-official-signature-pdf-upload basics', () => {
let node;
......@@ -39,3 +40,24 @@ suite('dbp-signature-app basics', () => {
});
});
suite('pdf signature detection', () => {
function getPDFFile(data) {
return new File([new Blob([data])], 'test.pdf', {type: 'application/pdf'});
}
test('getPDFSignatureCount', async () => {
let sig1 = "/Type\n/Sig\n/Filter\n/Adobe.PPKLite\n/SubFilter\n/ETSI.CAdES.detached";
let sig2 = "/Type\n/Sig\n/Filter\n/Adobe.PPKLite\n/SubFilter\n/adbe.pkcs7.detached";
assert(await getPDFSignatureCount(getPDFFile(sig1)) === 1);
assert(await getPDFSignatureCount(getPDFFile(sig2)) === 1);
assert(await getPDFSignatureCount(getPDFFile(sig1 + sig2)) === 2);
assert(await getPDFSignatureCount(getPDFFile("foo" + sig1 + "bar" + sig2 + "quux")) === 2);
assert(await getPDFSignatureCount(getPDFFile("\nfoo" + sig1 + "bar\n")) === 1);
assert(await getPDFSignatureCount(getPDFFile("\nfoo" + sig2 + "bar\n")) === 1);
assert(await getPDFSignatureCount(getPDFFile("foobar")) === 0);
assert(await getPDFSignatureCount(getPDFFile("")) === 0);
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment