diff --git a/services/project-history/app/js/HttpController.js b/services/project-history/app/js/HttpController.js index 5746c741c6..d49fbd39d0 100644 --- a/services/project-history/app/js/HttpController.js +++ b/services/project-history/app/js/HttpController.js @@ -20,10 +20,10 @@ import { pipeline } from 'stream' const ONE_DAY_IN_SECONDS = 24 * 60 * 60 export function getProjectBlob(req, res, next) { - const projectId = req.params.project_id + const historyId = req.params.history_id const blobHash = req.params.hash HistoryStoreManager.getProjectBlobStream( - projectId, + historyId, blobHash, (err, stream) => { if (err != null) { diff --git a/services/project-history/app/js/Router.js b/services/project-history/app/js/Router.js index 851b750735..cdf550ee73 100644 --- a/services/project-history/app/js/Router.js +++ b/services/project-history/app/js/Router.js @@ -183,7 +183,7 @@ export function initialize(app) { HttpController.forceDebugProject ) - app.get('/project/:project_id/blob/:hash', HttpController.getProjectBlob) + app.get('/project/:history_id/blob/:hash', HttpController.getProjectBlob) app.get('/status/failures', HttpController.getFailures) diff --git a/services/project-history/test/unit/js/HttpController/HttpControllerTests.js b/services/project-history/test/unit/js/HttpController/HttpControllerTests.js index 273bd1d867..834c3b8183 100644 --- a/services/project-history/test/unit/js/HttpController/HttpControllerTests.js +++ b/services/project-history/test/unit/js/HttpController/HttpControllerTests.js @@ -92,9 +92,10 @@ describe('HttpController', function () { beforeEach(function () { this.blobHash = 'abcd' this.stream = {} + this.historyId = 1337 this.HistoryStoreManager.getProjectBlobStream.yields(null, this.stream) this.HttpController.getProjectBlob( - { params: { project_id: this.projectId, hash: this.blobHash } }, + { params: { history_id: this.historyId, hash: this.blobHash } }, this.res, this.next ) @@ -102,7 +103,7 @@ describe('HttpController', function () { it('should get a blob stream', function () { this.HistoryStoreManager.getProjectBlobStream - .calledWith(this.projectId, this.blobHash) + .calledWith(this.historyId, this.blobHash) .should.equal(true) this.pipeline.should.have.been.calledWith(this.stream, this.res) }) diff --git a/services/web/app/src/Features/History/HistoryController.js b/services/web/app/src/Features/History/HistoryController.js index 6c76ba5b22..eadf71f1b2 100644 --- a/services/web/app/src/Features/History/HistoryController.js +++ b/services/web/app/src/Features/History/HistoryController.js @@ -38,6 +38,30 @@ module.exports = HistoryController = { }) }, + getBlob(req, res, next) { + const { project_id: projectId, blob } = req.params + + ProjectGetter.getProject( + projectId, + { 'overleaf.history.id': true }, + (err, project) => { + if (err) return next(err) + + const url = new URL(settings.apis.project_history.url) + url.pathname = `/project/${project.overleaf.history.id}/blob/${blob}` + + pipeline(request(url.href), res, err => { + // If the downstream request is cancelled, we get an + // ERR_STREAM_PREMATURE_CLOSE. + if (err && err.code !== 'ERR_STREAM_PREMATURE_CLOSE') { + logger.warn({ url, err }, 'history API error') + next(err) + } + }) + } + ) + }, + proxyToHistoryApiAndInjectUserDetails(req, res, next) { const userId = SessionManager.getLoggedInUserId(req.session) const url = settings.apis.project_history.url + req.url