Pārlūkot izejas kodu

[web] fix download of output.pdf when creating Server Pro template (#23988)

GitOrigin-RevId: 899b9bfd54af8d3f345393068663d769ec3a869b
Jakob Ackermann 1 gadu atpakaļ
vecāks
revīzija
195a12a3dc

+ 11 - 3
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,

+ 16 - 3
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 () {