diff --git a/services/web/app/coffee/Features/Exports/ExportsController.coffee b/services/web/app/coffee/Features/Exports/ExportsController.coffee index 7c74d14aed..e724951d2d 100644 --- a/services/web/app/coffee/Features/Exports/ExportsController.coffee +++ b/services/web/app/coffee/Features/Exports/ExportsController.coffee @@ -37,3 +37,11 @@ module.exports = status_detail: parsed_export.status_detail } res.send export_json: json + + exportZip: (req, res) -> + {export_id} = req.params + AuthenticationController.getLoggedInUserId(req) + ExportsHandler.fetchZip export_id, (err, export_zip_url) -> + return err if err? + + res.redirect export_zip_url diff --git a/services/web/app/coffee/Features/Exports/ExportsHandler.coffee b/services/web/app/coffee/Features/Exports/ExportsHandler.coffee index 0594b9fc2b..234a334241 100644 --- a/services/web/app/coffee/Features/Exports/ExportsHandler.coffee +++ b/services/web/app/coffee/Features/Exports/ExportsHandler.coffee @@ -118,3 +118,19 @@ module.exports = ExportsHandler = self = err = new Error("v1 export returned a failure status code: #{res.statusCode}") logger.err err:err, export:export_id, "v1 export returned failure status code: #{res.statusCode}" callback err + + fetchZip: (export_id, callback=(err, zip_url) ->) -> + console.log("#{settings.apis.v1.url}/api/v1/sharelatex/exports/#{export_id}/zip_url") + request.get { + url: "#{settings.apis.v1.url}/api/v1/sharelatex/exports/#{export_id}/zip_url" + auth: {user: settings.apis.v1.user, pass: settings.apis.v1.pass } + }, (err, res, body) -> + if err? + logger.err err:err, export:export_id, "error making request to v1 export" + callback err + else if 200 <= res.statusCode < 300 + callback null, body + else + err = new Error("v1 export returned a failure status code: #{res.statusCode}") + logger.err err:err, export:export_id, "v1 export zip fetch returned failure status code: #{res.statusCode}" + callback err diff --git a/services/web/app/coffee/router.coffee b/services/web/app/coffee/router.coffee index f35cd6c0ab..f48882a634 100644 --- a/services/web/app/coffee/router.coffee +++ b/services/web/app/coffee/router.coffee @@ -245,6 +245,7 @@ module.exports = class Router webRouter.post '/project/:project_id/export/:brand_variation_id', AuthorizationMiddlewear.ensureUserCanAdminProject, ExportsController.exportProject webRouter.get '/project/:project_id/export/:export_id', AuthorizationMiddlewear.ensureUserCanAdminProject, ExportsController.exportStatus + webRouter.get '/project/:project_id/export/:export_id/zip', AuthorizationMiddlewear.ensureUserCanAdminProject, ExportsController.exportZip webRouter.get '/Project/:Project_id/download/zip', AuthorizationMiddlewear.ensureUserCanReadProject, ProjectDownloadsController.downloadProject webRouter.get '/project/download/zip', AuthorizationMiddlewear.ensureUserCanReadMultipleProjects, ProjectDownloadsController.downloadMultipleProjects diff --git a/services/web/test/unit/coffee/Exports/ExportsHandlerTests.coffee b/services/web/test/unit/coffee/Exports/ExportsHandlerTests.coffee index 974e8e0c1b..8b16088552 100644 --- a/services/web/test/unit/coffee/Exports/ExportsHandlerTests.coffee +++ b/services/web/test/unit/coffee/Exports/ExportsHandlerTests.coffee @@ -281,3 +281,32 @@ describe 'ExportsHandler', -> @callback.calledWith(null, { body: @body }) .should.equal true + describe 'fetchZip', -> + beforeEach (done) -> + @settings.apis = + v1: + url: 'http://localhost:5000' + user: 'overleaf' + pass: 'pass' + @export_id = 897 + @body = "https://writelatex-conversions-dev.s3.amazonaws.com/exports/ieee_latexqc/tnb/2912/xggmprcrpfwbsnqzqqmvktddnrbqkqkr.zip?X-Amz-Expires=14400&X-Amz-Date=20180730T181003Z&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAJDGDIJFGLNVGZH6A/20180730/us-east-1/s3/aws4_request&X-Amz-SignedHeaders=host&X-Amz-Signature=dec990336913cef9933f0e269afe99722d7ab2830ebf2c618a75673ee7159fee" + @stubGet = sinon.stub().yields(null, {statusCode: 200}, { body: @body }) + done() + + describe "when all goes well", -> + beforeEach (done) -> + @stubRequest.get = @stubGet + @ExportsHandler.fetchZip @export_id, (error, body) => + @callback(error, body) + done() + + it 'should issue the request', -> + expect(@stubGet.getCall(0).args[0]).to.deep.equal + url: @settings.apis.v1.url + '/api/v1/sharelatex/exports/' + @export_id + '/zip_url' + auth: + user: @settings.apis.v1.user + pass: @settings.apis.v1.pass + + it 'should return the v1 export id', -> + @callback.calledWith(null, { body: @body }) + .should.equal true