From 36035e8ccd15874048c7080593caede0b453eb9a Mon Sep 17 00:00:00 2001 From: Alasdair Smith Date: Wed, 8 May 2019 09:58:02 +0100 Subject: [PATCH] Merge pull request #1746 from overleaf/sk-fix-stale-pdf-download-3 Pass build-id when downloading pdf GitOrigin-RevId: bc4b1558170661172304df1b71f9b859a59abc5b --- services/web/app/coffee/router.coffee | 22 ++++++++++++++++++- .../src/ide/pdf/controllers/PdfController.js | 13 ++++++++--- .../Compile/CompileControllerTests.coffee | 14 ++++++++++++ 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/services/web/app/coffee/router.coffee b/services/web/app/coffee/router.coffee index 257f0193de..afb7c76a47 100644 --- a/services/web/app/coffee/router.coffee +++ b/services/web/app/coffee/router.coffee @@ -219,8 +219,28 @@ module.exports = class Router webRouter.post '/project/:Project_id/compile/stop', AuthorizationMiddleware.ensureUserCanReadProject, CompileController.stopCompile - # Used by the web download buttons, adds filename header + # LEGACY: Used by the web download buttons, adds filename header, TODO: remove at some future date webRouter.get '/project/:Project_id/output/output.pdf', AuthorizationMiddleware.ensureUserCanReadProject, CompileController.downloadPdf + + # PDF Download button + webRouter.get /^\/download\/project\/([^\/]*)\/output\/output\.pdf$/, + ((req, res, next) -> + params = + "Project_id": req.params[0] + req.params = params + next() + ), AuthorizationMiddleware.ensureUserCanReadProject, CompileController.downloadPdf + + # PDF Download button for specific build + webRouter.get /^\/download\/project\/([^\/]*)\/build\/([0-9a-f-]+)\/output\/output\.pdf$/, + ((req, res, next) -> + params = + "Project_id": req.params[0] + "build_id": req.params[1] + req.params = params + next() + ), AuthorizationMiddleware.ensureUserCanReadProject, CompileController.downloadPdf + # Used by the pdf viewers webRouter.get /^\/project\/([^\/]*)\/output\/(.*)$/, ((req, res, next) -> diff --git a/services/web/public/src/ide/pdf/controllers/PdfController.js b/services/web/public/src/ide/pdf/controllers/PdfController.js index 9ead3c4d63..260125f85b 100644 --- a/services/web/public/src/ide/pdf/controllers/PdfController.js +++ b/services/web/public/src/ide/pdf/controllers/PdfController.js @@ -503,12 +503,19 @@ define([ } // convert the qs hash into a query string and append it $scope.pdf.url += createQueryString(qs) + // Save all downloads as files qs.popupDownload = true - $scope.pdf.downloadUrl = - `/project/${$scope.project_id}/output/output.pdf` + - createQueryString(qs) + // Pass build id to download if we have it + let buildId = null + if (fileByPath['output.pdf'] && fileByPath['output.pdf'].build) { + buildId = fileByPath['output.pdf'].build + } + $scope.pdf.downloadUrl = + `/download/project/${$scope.project_id}${ + buildId ? '/build/' + buildId : '' + }/output/output.pdf` + createQueryString(qs) fetchLogs(fileByPath, { pdfDownloadDomain }) } diff --git a/services/web/test/unit/coffee/Compile/CompileControllerTests.coffee b/services/web/test/unit/coffee/Compile/CompileControllerTests.coffee index 0d070d687e..fa8fb7ea92 100644 --- a/services/web/test/unit/coffee/Compile/CompileControllerTests.coffee +++ b/services/web/test/unit/coffee/Compile/CompileControllerTests.coffee @@ -206,6 +206,20 @@ describe "CompileController", -> it "should proxy the PDF from the CLSI", -> @CompileController.proxyToClsi.calledWith(@project_id, "/project/#{@project_id}/user/#{@user_id}/output/output.pdf", @req, @res, @next).should.equal true + describe "when the a build-id is provided", -> + beforeEach -> + @req.params.build_id = @buildId = '1234-5678' + @CompileController.proxyToClsi = sinon.stub() + @RateLimiter.addCount.callsArgWith(1, null, true) + @CompileController.downloadPdf(@req, @res, @next) + + it "should proxy the PDF from the CLSI, with a build-id", -> + @CompileController.proxyToClsi.calledWith( + @project_id, + "/project/#{@project_id}/user/#{@user_id}/build/#{@buildId}/output/output.pdf", + @req, @res, @next + ).should.equal true + describe "when the pdf is not going to be used in pdfjs viewer", -> it "should check the rate limiter when pdfng is not set", (done)->