mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-11-14 06:03:45 +01:00
Merge pull request #22391 from overleaf/bg-issue22370
Provide fallback in web for requests to file urls GitOrigin-RevId: 793e8e138a08e28e097ac6dd0503333cc17375ab
This commit is contained in:
@@ -2,6 +2,7 @@ let HistoryController
|
||||
const OError = require('@overleaf/o-error')
|
||||
const async = require('async')
|
||||
const logger = require('@overleaf/logger')
|
||||
const metrics = require('@overleaf/metrics')
|
||||
const request = require('request')
|
||||
const settings = require('@overleaf/settings')
|
||||
const SessionManager = require('../Authentication/SessionManager')
|
||||
@@ -11,6 +12,7 @@ const Errors = require('../Errors/Errors')
|
||||
const HistoryManager = require('./HistoryManager')
|
||||
const ProjectDetailsHandler = require('../Project/ProjectDetailsHandler')
|
||||
const ProjectEntityUpdateHandler = require('../Project/ProjectEntityUpdateHandler')
|
||||
const ProjectLocator = require('../Project/ProjectLocator')
|
||||
const RestoreManager = require('./RestoreManager')
|
||||
const { pipeline } = require('stream')
|
||||
const Stream = require('stream')
|
||||
@@ -68,6 +70,33 @@ module.exports = HistoryController = {
|
||||
getBlob: expressify(getBlob),
|
||||
headBlob: expressify(headBlob),
|
||||
|
||||
/** Middleware to translate fileId requests to use the blob API.
|
||||
*
|
||||
* e.g. incoming requests to /project/:project_id/file/:file_id are
|
||||
* internally redirected to /project/:project_id/blob/:hash if the file
|
||||
* has a hash.
|
||||
* */
|
||||
fileToBlobRedirectMiddleware(req, res, next) {
|
||||
const projectId = req.params.Project_id
|
||||
const fileId = req.params.File_id
|
||||
ProjectLocator.findElement(
|
||||
{ project_id: projectId, element_id: fileId, type: 'file' },
|
||||
(err, file) => {
|
||||
if (err) {
|
||||
return next(err)
|
||||
}
|
||||
if (file?.hash) {
|
||||
req.url = `/project/${projectId}/blob/${file.hash}`
|
||||
}
|
||||
metrics.inc('fileToBlobRedirectMiddleware', 1, {
|
||||
method: req.method,
|
||||
status: Boolean(file?.hash),
|
||||
})
|
||||
next()
|
||||
}
|
||||
)
|
||||
},
|
||||
|
||||
proxyToHistoryApi(req, res, next) {
|
||||
const userId = SessionManager.getLoggedInUserId(req.session)
|
||||
const url = settings.apis.project_history.url + req.url
|
||||
|
||||
@@ -543,11 +543,13 @@ async function initialize(webRouter, privateApiRouter, publicApiRouter) {
|
||||
webRouter.head(
|
||||
'/Project/:Project_id/file/:File_id',
|
||||
AuthorizationMiddleware.ensureUserCanReadProject,
|
||||
HistoryController.fileToBlobRedirectMiddleware,
|
||||
FileStoreController.getFileHead
|
||||
)
|
||||
webRouter.get(
|
||||
'/Project/:Project_id/file/:File_id',
|
||||
AuthorizationMiddleware.ensureUserCanReadProject,
|
||||
HistoryController.fileToBlobRedirectMiddleware,
|
||||
FileStoreController.getFile
|
||||
)
|
||||
webRouter.head(
|
||||
|
||||
Reference in New Issue
Block a user