Explorar el Código

Merge pull request #1746 from overleaf/sk-fix-stale-pdf-download-3

Pass build-id when downloading pdf

GitOrigin-RevId: bc4b1558170661172304df1b71f9b859a59abc5b
Alasdair Smith hace 7 años
padre
commit
36035e8ccd

+ 21 - 1
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
 		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
 		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
 		# Used by the pdf viewers
 		webRouter.get  /^\/project\/([^\/]*)\/output\/(.*)$/,
 		webRouter.get  /^\/project\/([^\/]*)\/output\/(.*)$/,
 			((req, res, next) ->
 			((req, res, next) ->

+ 10 - 3
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
         // convert the qs hash into a query string and append it
         $scope.pdf.url += createQueryString(qs)
         $scope.pdf.url += createQueryString(qs)
+
         // Save all downloads as files
         // Save all downloads as files
         qs.popupDownload = true
         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 })
         fetchLogs(fileByPath, { pdfDownloadDomain })
       }
       }
 
 

+ 14 - 0
services/web/test/unit/coffee/Compile/CompileControllerTests.coffee

@@ -206,6 +206,20 @@ describe "CompileController", ->
 			it "should proxy the PDF from the CLSI", ->
 			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
 				@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", ->
 		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)->
 			it "should check the rate limiter when pdfng is not set", (done)->