2019-12-16 05:42:31 -05:00
|
|
|
const FileHandler = require('./FileHandler')
|
2020-11-25 06:57:23 -05:00
|
|
|
const metrics = require('@overleaf/metrics')
|
2019-12-16 05:42:31 -05:00
|
|
|
const parseRange = require('range-parser')
|
|
|
|
const Errors = require('./Errors')
|
2024-11-08 05:21:56 -05:00
|
|
|
const { pipeline } = require('node:stream')
|
2015-08-28 06:45:16 -04:00
|
|
|
|
2019-12-16 05:42:31 -05:00
|
|
|
const maxSizeInBytes = 1024 * 1024 * 1024 // 1GB
|
2015-08-28 06:45:16 -04:00
|
|
|
|
2020-01-02 10:51:09 -05:00
|
|
|
module.exports = {
|
|
|
|
getFile,
|
|
|
|
getFileHead,
|
|
|
|
insertFile,
|
|
|
|
copyFile,
|
|
|
|
deleteFile,
|
2020-03-14 10:56:29 -04:00
|
|
|
deleteProject,
|
2021-07-13 07:04:46 -04:00
|
|
|
directorySize,
|
2020-01-02 10:51:09 -05:00
|
|
|
}
|
|
|
|
|
2020-01-03 12:42:28 -05:00
|
|
|
function getFile(req, res, next) {
|
2020-01-02 10:51:09 -05:00
|
|
|
const { key, bucket } = req
|
|
|
|
const { format, style } = req.query
|
|
|
|
const options = {
|
|
|
|
key,
|
|
|
|
bucket,
|
|
|
|
format,
|
2021-07-13 07:04:46 -04:00
|
|
|
style,
|
2020-01-02 10:51:09 -05:00
|
|
|
}
|
2020-01-07 16:19:26 -05:00
|
|
|
|
2020-01-02 10:51:09 -05:00
|
|
|
metrics.inc('getFile')
|
2020-01-14 07:02:39 -05:00
|
|
|
req.requestLogger.setMessage('getting file')
|
|
|
|
req.requestLogger.addFields({
|
2020-01-10 12:08:47 -05:00
|
|
|
key,
|
|
|
|
bucket,
|
|
|
|
format,
|
|
|
|
style,
|
2021-07-13 07:04:46 -04:00
|
|
|
cacheWarm: req.query.cacheWarm,
|
2020-01-10 12:08:47 -05:00
|
|
|
})
|
2020-01-02 10:51:09 -05:00
|
|
|
|
|
|
|
if (req.headers.range) {
|
|
|
|
const range = _getRange(req.headers.range)
|
|
|
|
if (range) {
|
2019-12-16 05:42:31 -05:00
|
|
|
options.start = range.start
|
|
|
|
options.end = range.end
|
2020-01-14 07:02:39 -05:00
|
|
|
req.requestLogger.addFields({ range })
|
2019-12-16 05:42:31 -05:00
|
|
|
}
|
2020-01-02 10:51:09 -05:00
|
|
|
}
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
FileHandler.getRedirectUrl(bucket, key, options, function (err, redirectUrl) {
|
2020-01-02 10:51:09 -05:00
|
|
|
if (err) {
|
2020-04-09 12:11:19 -04:00
|
|
|
metrics.inc('file_redirect_error')
|
2020-01-02 10:51:09 -05:00
|
|
|
}
|
|
|
|
|
2020-04-09 12:11:19 -04:00
|
|
|
if (redirectUrl) {
|
|
|
|
metrics.inc('file_redirect')
|
|
|
|
return res.redirect(redirectUrl)
|
2020-01-02 10:51:09 -05:00
|
|
|
}
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
FileHandler.getFile(bucket, key, options, function (err, fileStream) {
|
2020-04-09 12:11:19 -04:00
|
|
|
if (err) {
|
|
|
|
if (err instanceof Errors.NotFoundError) {
|
|
|
|
res.sendStatus(404)
|
|
|
|
} else {
|
|
|
|
next(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.query.cacheWarm) {
|
2023-05-08 07:03:50 -04:00
|
|
|
fileStream.destroy()
|
2020-04-09 12:11:19 -04:00
|
|
|
return res.sendStatus(200).end()
|
2020-01-09 09:13:24 -05:00
|
|
|
}
|
2020-04-09 12:11:19 -04:00
|
|
|
|
2021-07-13 07:04:46 -04:00
|
|
|
pipeline(fileStream, res, err => {
|
2020-04-09 12:11:19 -04:00
|
|
|
if (err && err.code === 'ERR_STREAM_PREMATURE_CLOSE') {
|
|
|
|
res.end()
|
|
|
|
} else if (err) {
|
|
|
|
next(
|
2020-07-07 08:49:54 -04:00
|
|
|
new Errors.ReadError(
|
|
|
|
'error transferring stream',
|
|
|
|
{ bucket, key, format, style },
|
|
|
|
err
|
|
|
|
)
|
2020-04-09 12:11:19 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
2020-01-09 09:13:24 -05:00
|
|
|
})
|
2020-01-02 10:51:09 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-07 16:19:26 -05:00
|
|
|
function getFileHead(req, res, next) {
|
2020-01-02 10:51:09 -05:00
|
|
|
const { key, bucket } = req
|
2020-01-07 16:19:26 -05:00
|
|
|
|
2020-01-02 10:51:09 -05:00
|
|
|
metrics.inc('getFileSize')
|
2020-01-14 07:02:39 -05:00
|
|
|
req.requestLogger.setMessage('getting file size')
|
|
|
|
req.requestLogger.addFields({ key, bucket })
|
2020-01-07 16:19:26 -05:00
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
FileHandler.getFileSize(bucket, key, function (err, fileSize) {
|
2020-01-02 10:51:09 -05:00
|
|
|
if (err) {
|
|
|
|
if (err instanceof Errors.NotFoundError) {
|
|
|
|
res.sendStatus(404)
|
2019-12-16 05:42:31 -05:00
|
|
|
} else {
|
2020-01-07 16:19:26 -05:00
|
|
|
next(err)
|
2019-12-16 05:42:31 -05:00
|
|
|
}
|
2020-01-02 10:51:09 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
res.set('Content-Length', fileSize)
|
|
|
|
res.status(200).end()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-07 16:19:26 -05:00
|
|
|
function insertFile(req, res, next) {
|
2020-01-02 10:51:09 -05:00
|
|
|
metrics.inc('insertFile')
|
|
|
|
const { key, bucket } = req
|
2020-01-07 16:19:26 -05:00
|
|
|
|
2020-01-14 07:02:39 -05:00
|
|
|
req.requestLogger.setMessage('inserting file')
|
|
|
|
req.requestLogger.addFields({ key, bucket })
|
2020-01-07 16:19:26 -05:00
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
FileHandler.insertFile(bucket, key, req, function (err) {
|
2020-01-02 10:51:09 -05:00
|
|
|
if (err) {
|
2020-01-07 16:19:26 -05:00
|
|
|
next(err)
|
2020-01-02 10:51:09 -05:00
|
|
|
} else {
|
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-07 16:19:26 -05:00
|
|
|
function copyFile(req, res, next) {
|
2020-01-02 10:51:09 -05:00
|
|
|
metrics.inc('copyFile')
|
|
|
|
const { key, bucket } = req
|
|
|
|
const oldProjectId = req.body.source.project_id
|
|
|
|
const oldFileId = req.body.source.file_id
|
2020-01-07 16:19:26 -05:00
|
|
|
|
2020-01-14 07:02:39 -05:00
|
|
|
req.requestLogger.addFields({
|
2020-01-07 16:19:26 -05:00
|
|
|
key,
|
|
|
|
bucket,
|
|
|
|
oldProject_id: oldProjectId,
|
2021-07-13 07:04:46 -04:00
|
|
|
oldFile_id: oldFileId,
|
2020-01-10 12:08:47 -05:00
|
|
|
})
|
2020-01-14 07:02:39 -05:00
|
|
|
req.requestLogger.setMessage('copying file')
|
2020-01-02 10:51:09 -05:00
|
|
|
|
2024-10-24 07:42:38 -04:00
|
|
|
FileHandler.copyObject(bucket, `${oldProjectId}/${oldFileId}`, key, err => {
|
|
|
|
if (err) {
|
|
|
|
if (err instanceof Errors.NotFoundError) {
|
|
|
|
res.sendStatus(404)
|
|
|
|
} else {
|
|
|
|
next(err)
|
2019-12-16 05:42:31 -05:00
|
|
|
}
|
2024-10-24 07:42:38 -04:00
|
|
|
} else {
|
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
|
|
|
})
|
2020-01-02 10:51:09 -05:00
|
|
|
}
|
|
|
|
|
2020-01-07 16:19:26 -05:00
|
|
|
function deleteFile(req, res, next) {
|
2020-01-02 10:51:09 -05:00
|
|
|
metrics.inc('deleteFile')
|
|
|
|
const { key, bucket } = req
|
2020-01-07 16:19:26 -05:00
|
|
|
|
2020-01-14 07:02:39 -05:00
|
|
|
req.requestLogger.addFields({ key, bucket })
|
|
|
|
req.requestLogger.setMessage('deleting file')
|
2020-01-07 16:19:26 -05:00
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
FileHandler.deleteFile(bucket, key, function (err) {
|
2020-01-07 16:19:26 -05:00
|
|
|
if (err) {
|
|
|
|
next(err)
|
2019-12-16 05:42:31 -05:00
|
|
|
} else {
|
2020-01-07 16:19:26 -05:00
|
|
|
res.sendStatus(204)
|
2019-12-16 05:42:31 -05:00
|
|
|
}
|
2020-01-02 10:51:09 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-14 10:56:29 -04:00
|
|
|
function deleteProject(req, res, next) {
|
|
|
|
metrics.inc('deleteProject')
|
2020-03-16 07:35:01 -04:00
|
|
|
const { key, bucket } = req
|
2020-03-14 10:56:29 -04:00
|
|
|
|
2020-03-16 07:35:01 -04:00
|
|
|
req.requestLogger.setMessage('deleting project')
|
|
|
|
req.requestLogger.addFields({ key, bucket })
|
2020-03-14 10:56:29 -04:00
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
FileHandler.deleteProject(bucket, key, function (err) {
|
2020-03-14 10:56:29 -04:00
|
|
|
if (err) {
|
2020-03-16 07:35:01 -04:00
|
|
|
if (err instanceof Errors.InvalidParametersError) {
|
|
|
|
return res.sendStatus(400)
|
|
|
|
}
|
2020-03-14 10:56:29 -04:00
|
|
|
next(err)
|
|
|
|
} else {
|
|
|
|
res.sendStatus(204)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-07 16:19:26 -05:00
|
|
|
function directorySize(req, res, next) {
|
2020-01-02 10:51:09 -05:00
|
|
|
metrics.inc('projectSize')
|
|
|
|
const { project_id: projectId, bucket } = req
|
2020-01-07 16:19:26 -05:00
|
|
|
|
2020-01-14 07:02:39 -05:00
|
|
|
req.requestLogger.setMessage('getting project size')
|
|
|
|
req.requestLogger.addFields({ projectId, bucket })
|
2020-01-07 16:19:26 -05:00
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
FileHandler.getDirectorySize(bucket, projectId, function (err, size) {
|
2020-01-02 10:51:09 -05:00
|
|
|
if (err) {
|
2020-01-07 16:19:26 -05:00
|
|
|
return next(err)
|
2020-01-02 10:51:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
res.json({ 'total bytes': size })
|
2020-01-14 07:02:39 -05:00
|
|
|
req.requestLogger.addFields({ size })
|
2020-01-02 10:51:09 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function _getRange(header) {
|
|
|
|
const parsed = parseRange(maxSizeInBytes, header)
|
|
|
|
if (parsed === -1 || parsed === -2 || parsed.type !== 'bytes') {
|
|
|
|
return null
|
|
|
|
} else {
|
|
|
|
const range = parsed[0]
|
|
|
|
return { start: range.start, end: range.end }
|
2019-12-16 05:42:31 -05:00
|
|
|
}
|
|
|
|
}
|