diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index 686e3cdbacd25062fc1a61be1b5a17e60ec9c62e..dc4534706ee37c51ec0d6ee69333c9170304ae7f 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -11,8 +11,8 @@ describe("PDF parser", () => { ); const parser = new PDFparser(file); - const actual = await parser.getPDFMeta(); + expect(actual.pages).toEqual(2); }); @@ -35,4 +35,16 @@ describe("PDF parser", () => { expect(error).toBeInstanceOf(AppError); } }); + + it("should throw error if file type is different then pdf", async () => { + const file = fs.readFileSync(path.resolve(__dirname, "./test.txt")); + + try { + const parser = new PDFparser(file); + const actual = await parser.getPDFMeta(); + } catch (error) { + expect(error).toBeInstanceOf(AppError); + expect(error.message).toEqual("Only pdf file type is supported"); + } + }); }); diff --git a/__tests__/test.txt b/__tests__/test.txt new file mode 100644 index 0000000000000000000000000000000000000000..7f489a9a0d5aa3aee95f98189b082347245d4c60 --- /dev/null +++ b/__tests__/test.txt @@ -0,0 +1 @@ +test txt file for tests diff --git a/dist/lib/generalUtils.d.ts b/dist/lib/generalUtils.d.ts index 71ffde6ad73633097e0a430e98a7d944241760d5..8bdf20864927cd9bde024b62960cae36e15b20dc 100644 --- a/dist/lib/generalUtils.d.ts +++ b/dist/lib/generalUtils.d.ts @@ -6,3 +6,4 @@ export declare const getByteRange: (pdfBuffer: Buffer) => { }; export declare const preparePDF: (pdf: any) => Buffer; export declare const getMetaRegexMatch: (keyName: string) => (str: any) => any; +export declare const isPDF: (buf: Buffer) => boolean; diff --git a/dist/lib/generalUtils.js b/dist/lib/generalUtils.js index ff972966add5ed2278b35abca9ebb10332cc5dc6..f757ed501c2040e65130d7266b1b2c34eeba1c5d 100644 --- a/dist/lib/generalUtils.js +++ b/dist/lib/generalUtils.js @@ -1,6 +1,6 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.getMetaRegexMatch = exports.preparePDF = exports.getByteRange = exports.checkForSubFilter = void 0; +exports.isPDF = exports.getMetaRegexMatch = exports.preparePDF = exports.getByteRange = exports.checkForSubFilter = void 0; const errors_1 = require("./errors"); const DEFAULT_BYTE_RANGE_PLACEHOLDER = "**********"; const checkForSubFilter = (pdfBuffer) => { @@ -50,3 +50,9 @@ const getMetaRegexMatch = (keyName) => (str) => { return meta; }; exports.getMetaRegexMatch = getMetaRegexMatch; +const isPDF = (buf) => { + return (Buffer.isBuffer(buf) && + buf.lastIndexOf("%PDF-") === 0 && + buf.lastIndexOf("%%EOF") > -1); +}; +exports.isPDF = isPDF; diff --git a/dist/pdfParser.d.ts b/dist/pdfParser.d.ts index 3dac58cde82d53d9d09b85af959ad3ec0138cb5e..05d9f6f360b2c693191cc9945fa1c82ed9176a86 100644 --- a/dist/pdfParser.d.ts +++ b/dist/pdfParser.d.ts @@ -1,10 +1,10 @@ /// <reference types="node" /> -import { IgetMetaResponse } from "./types"; +import { IGetMetaResponse } from "./types"; declare class PDFparser { readonly document: any; readonly config: any; constructor(document: Buffer); - getPDFMeta: () => Promise<IgetMetaResponse>; + getPDFMeta: () => Promise<IGetMetaResponse>; insertQrCode: (imgBytes: ArrayBuffer, url: string, scaleFactor: number) => Promise<ArrayBuffer>; private createPageLinkAnnotation; } diff --git a/dist/pdfParser.js b/dist/pdfParser.js index de661c6f59b2d6017f9c73a5b58cbef919b0872d..94d4530339c18f78776738e53d871bbe44c3f32a 100644 --- a/dist/pdfParser.js +++ b/dist/pdfParser.js @@ -15,12 +15,16 @@ const config_1 = require("./config"); const lib_1 = require("./lib"); const timeUtils_1 = require("./lib/timeUtils"); const errors_1 = require("./lib/errors"); +const generalUtils_1 = require("./lib/generalUtils"); class PDFparser { constructor(document) { this.getPDFMeta = () => __awaiter(this, void 0, void 0, function* () { if (!(this.document instanceof Buffer)) { throw new errors_1.AppError("Document is not Buffer"); } + if (!(0, generalUtils_1.isPDF)(this.document)) { + throw new errors_1.AppError("Only pdf file type is supported"); + } try { const signaturesMeta = yield (0, lib_1.verifyPDF)(this.document); const pdfMeta = yield pdfdataextract_1.PdfData.extract(this.document, config_1.config); diff --git a/dist/types.d.ts b/dist/types.d.ts index fcab7ba25fcc3ec23ab3dc4e1c993dc366c8ed2b..ab294915848048008c60d2f2f41d127344c54175 100644 --- a/dist/types.d.ts +++ b/dist/types.d.ts @@ -1,4 +1,4 @@ -export interface Icert { +export interface ICert { clientCertificate: boolean; issuedBy: { countryName: string; @@ -17,9 +17,19 @@ export interface Icert { }; pemCertificate: string; } -export interface IgetMetaResponse { +export interface ISignature { + isExpired: boolean; + meta: { + certs: Array<ICert>; + reason: string; + contactInfo: string; + location: string; + signDate: string; + }; +} +export interface IGetMetaResponse { expired?: boolean; - signatures?: Array<any>; + signatures?: Array<ISignature>; pages: number; title: string; author: string; diff --git a/src/lib/generalUtils.ts b/src/lib/generalUtils.ts index 153dcc9fb16044641b37ef1f011aef9cf2f48def..7c749b5ba8d77aa08c45a1f9ef1d4384290ca79c 100644 --- a/src/lib/generalUtils.ts +++ b/src/lib/generalUtils.ts @@ -57,3 +57,11 @@ export const getMetaRegexMatch = (keyName: string) => (str) => { return meta; }; + +export const isPDF = (buf: Buffer): boolean => { + return ( + Buffer.isBuffer(buf) && + buf.lastIndexOf("%PDF-") === 0 && + buf.lastIndexOf("%%EOF") > -1 + ); +}; diff --git a/src/pdfParser.ts b/src/pdfParser.ts index 44548c4a36173aa4404ed2ab95eed80642c0b036..0729fa8ef0cfd058fbfca0df2d864f5c94671b70 100644 --- a/src/pdfParser.ts +++ b/src/pdfParser.ts @@ -1,10 +1,11 @@ import { PDFName, PDFPage, PDFString, PDFDocument } from "pdf-lib"; import { PdfData } from "pdfdataextract"; import { config } from "./config"; -import { IgetMetaResponse } from "./types"; +import { IGetMetaResponse } from "./types"; import { verifyPDF } from "./lib"; import { formatPdfTime } from "./lib/timeUtils"; import { AppError, GeneralError } from "./lib/errors"; +import { isPDF } from "./lib/generalUtils"; class PDFparser { readonly document; readonly config; @@ -14,11 +15,15 @@ class PDFparser { this.config = config; } - getPDFMeta = async (): Promise<IgetMetaResponse> => { + getPDFMeta = async (): Promise<IGetMetaResponse> => { if (!(this.document instanceof Buffer)) { throw new AppError("Document is not Buffer"); } + if (!isPDF(this.document)) { + throw new AppError("Only pdf file type is supported"); + } + try { const signaturesMeta = await verifyPDF(this.document); const pdfMeta = await PdfData.extract(this.document, config); diff --git a/src/types.ts b/src/types.ts index 30659ddb566290c6dc7b64345e6d8d0050713db9..d5e512840ee6031484f50593d69f8f7c95499282 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,4 @@ -export interface Icert { +export interface ICert { clientCertificate: boolean; issuedBy: { countryName: string; @@ -18,11 +18,22 @@ export interface Icert { pemCertificate: string; } -export interface IgetMetaResponse { +export interface ISignature { + isExpired: boolean; + meta: { + certs: Array<ICert>; + reason: string; + contactInfo: string; + location: string; + signDate: string; + }; +} + +export interface IGetMetaResponse { // verified: boolean; // authenticity: boolean; expired?: boolean; - signatures?: Array<any>; + signatures?: Array<ISignature>; pages: number; title: string; author: string;