From e8c7c468285e66546853a96e682e2e07a869fbfe Mon Sep 17 00:00:00 2001 From: BJR Matos Date: Tue, 4 Jun 2019 10:32:42 -0500 Subject: [PATCH] use new format to specify inline pdf --- lib/main.js | 20 +++++++++++--------- test/staticPdfTest.js | 24 ++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/lib/main.js b/lib/main.js index 8debfe5..413d686 100644 --- a/lib/main.js +++ b/lib/main.js @@ -40,22 +40,24 @@ function execute (reporter) { reporter.logger.debug(`static-pdf is using asset ${pdfAssetPath}`, req) pdfAsset = assetEntity.content - } else if (req.template.staticPdf.rawContent != null) { - if (Buffer.isBuffer(req.template.staticPdf.rawContent)) { - pdfAsset = req.template.staticPdf.rawContent - } else if (typeof req.template.staticPdf.rawContent === 'string') { - pdfAsset = Buffer.from(req.template.staticPdf.rawContent, 'base64') + } else if (req.template.staticPdf.pdfAsset != null) { + if (Buffer.isBuffer(req.template.staticPdf.pdfAsset.content)) { + pdfAsset = req.template.staticPdf.pdfAsset.content } else { - throw reporter.createError(`Invalid value for req.template.staticPdf.rawContent, specify either a base64 string or a buffer`, { - statusCode: 400 - }) + if (!req.template.staticPdf.pdfAsset.encoding) { + throw reporter.createError(`Missing value for req.template.staticPdf.pdfAsset.encoding, encoding is required`, { + statusCode: 400 + }) + } + + pdfAsset = Buffer.from(req.template.staticPdf.pdfAsset.content, req.template.staticPdf.pdfAsset.encoding) } reporter.logger.debug(`static-pdf is using inline content`, req) } if (!pdfAsset) { - throw reporter.createError(`Source PDF asset was not specified, specify either req.template.staticPdf.pdfAssetShortid or req.template.staticPdf.rawContent`, { + throw reporter.createError(`Source PDF asset was not specified, specify either req.template.staticPdf.pdfAssetShortid or req.template.staticPdf.pdfAsset`, { statusCode: 400 }) } diff --git a/test/staticPdfTest.js b/test/staticPdfTest.js index 6e4c9cc..d35de26 100644 --- a/test/staticPdfTest.js +++ b/test/staticPdfTest.js @@ -28,7 +28,9 @@ describe('static-pdf', () => { engine: 'none', recipe: 'static-pdf', staticPdf: { - rawContent: fs.readFileSync(path.join(__dirname, 'static.pdf')) + pdfAsset: { + content: fs.readFileSync(path.join(__dirname, 'static.pdf')) + } } } }) @@ -43,7 +45,10 @@ describe('static-pdf', () => { engine: 'none', recipe: 'static-pdf', staticPdf: { - rawContent: fs.readFileSync(path.join(__dirname, 'static.pdf')).toString('base64') + pdfAsset: { + content: fs.readFileSync(path.join(__dirname, 'static.pdf')).toString('base64'), + encoding: 'base64' + } } } }) @@ -72,6 +77,21 @@ describe('static-pdf', () => { should(result.content.toString().includes('PDF')).be.true() }) + it('should throw error when inline asset does not specify encoding', async () => { + return should(reporter.render({ + template: { + content: 'Hello', + engine: 'none', + recipe: 'static-pdf', + staticPdf: { + pdfAsset: { + content: fs.readFileSync(path.join(__dirname, 'static.pdf')).toString('base64') + } + } + } + })).be.rejectedWith(/encoding is required/) + }) + it('should throw error when asset reference is not PDF', async () => { await reporter.documentStore.collection('assets').insert({ name: 'img.png',