diff --git a/services/web/app/src/Features/Compile/CompileController.js b/services/web/app/src/Features/Compile/CompileController.js index f46b65d370..327c7cc9a3 100644 --- a/services/web/app/src/Features/Compile/CompileController.js +++ b/services/web/app/src/Features/Compile/CompileController.js @@ -387,7 +387,7 @@ module.exports = CompileController = { compileAndDownloadPdf(req, res, next) { const projectId = req.params.project_id // pass userId as null, since templates are an "anonymous" compile - CompileManager.compile(projectId, null, {}, function (err) { + CompileManager.compile(projectId, null, {}, (err, _status, outputFiles) => { if (err) { logger.err( { err, projectId }, @@ -396,11 +396,19 @@ module.exports = CompileController = { res.sendStatus(500) return } - const url = `/project/${projectId}/output/output.pdf` + const pdf = outputFiles.find(f => f.path === 'output.pdf') + if (!pdf) { + logger.warn( + { projectId }, + 'something went wrong compile and downloading pdf: no pdf' + ) + res.sendStatus(500) + return + } CompileController.proxyToClsi( projectId, 'output-file', - url, + pdf.url, {}, req, res, diff --git a/services/web/test/unit/src/Compile/CompileControllerTests.js b/services/web/test/unit/src/Compile/CompileControllerTests.js index ac1a5e056c..0e9c33f4c3 100644 --- a/services/web/test/unit/src/Compile/CompileControllerTests.js +++ b/services/web/test/unit/src/Compile/CompileControllerTests.js @@ -794,7 +794,13 @@ describe('CompileController', function () { project_id: this.projectId, }, } - this.CompileManager.compile.callsArgWith(3) + this.downloadPath = `/project/${this.projectId}/build/123/output/output.pdf` + this.CompileManager.compile.callsArgWith(3, null, 'success', [ + { + path: 'output.pdf', + url: this.downloadPath, + }, + ]) this.CompileController.proxyToClsi = sinon.stub() this.res = { send: () => {}, sendStatus: sinon.stub() } }) @@ -811,7 +817,7 @@ describe('CompileController', function () { this.CompileController.proxyToClsi, this.projectId, 'output-file', - `/project/${this.projectId}/output/output.pdf`, + this.downloadPath, {}, this.req, this.res @@ -821,7 +827,7 @@ describe('CompileController', function () { .calledWith( this.projectId, 'output-file', - `/project/${this.projectId}/output/output.pdf`, + this.downloadPath, {}, this.req, this.res @@ -836,6 +842,13 @@ describe('CompileController', function () { this.res.sendStatus.should.have.been.calledWith(500) this.CompileController.proxyToClsi.should.not.have.been.called }) + + it('should not download anything on missing pdf', function () { + this.CompileManager.compile.yields(null, 'success', []) + this.CompileController.compileAndDownloadPdf(this.req, this.res) + this.res.sendStatus.should.have.been.calledWith(500) + this.CompileController.proxyToClsi.should.not.have.been.called + }) }) describe('wordCount', function () {