2019-05-29 05:21:06 -04:00
|
|
|
const logger = require('logger-sharelatex')
|
2019-06-20 07:17:55 -04:00
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
const FileStoreHandler = require('./FileStoreHandler')
|
|
|
|
const ProjectLocator = require('../Project/ProjectLocator')
|
2019-06-20 07:17:55 -04:00
|
|
|
const Errors = require('../Errors/Errors')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getFile(req, res) {
|
2019-06-20 07:17:55 -04:00
|
|
|
const projectId = req.params.Project_id
|
|
|
|
const fileId = req.params.File_id
|
2019-05-29 05:21:06 -04:00
|
|
|
const queryString = req.query
|
2019-06-20 07:17:55 -04:00
|
|
|
const userAgent = req.get('User-Agent')
|
|
|
|
ProjectLocator.findElement(
|
|
|
|
{ project_id: projectId, element_id: fileId, type: 'file' },
|
2021-04-14 09:17:21 -04:00
|
|
|
function (err, file) {
|
2019-06-20 07:17:55 -04:00
|
|
|
if (err) {
|
2019-05-29 05:21:06 -04:00
|
|
|
logger.err(
|
2019-06-20 07:17:55 -04:00
|
|
|
{ err, projectId, fileId, queryString },
|
2019-05-29 05:21:06 -04:00
|
|
|
'error finding element for downloading file'
|
|
|
|
)
|
|
|
|
return res.sendStatus(500)
|
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
FileStoreHandler.getFileStream(
|
|
|
|
projectId,
|
|
|
|
fileId,
|
|
|
|
queryString,
|
|
|
|
function (err, stream) {
|
|
|
|
if (err) {
|
|
|
|
logger.err(
|
|
|
|
{ err, projectId, fileId, queryString },
|
|
|
|
'error getting file stream for downloading file'
|
|
|
|
)
|
|
|
|
return res.sendStatus(500)
|
|
|
|
}
|
|
|
|
// mobile safari will try to render html files, prevent this
|
|
|
|
if (isMobileSafari(userAgent) && isHtml(file)) {
|
|
|
|
res.setHeader('Content-Type', 'text/plain')
|
|
|
|
}
|
|
|
|
res.setContentDisposition('attachment', { filename: file.name })
|
|
|
|
stream.pipe(res)
|
2019-06-20 07:17:55 -04:00
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
2019-06-20 07:17:55 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
getFileHead(req, res) {
|
|
|
|
const projectId = req.params.Project_id
|
|
|
|
const fileId = req.params.File_id
|
|
|
|
FileStoreHandler.getFileSize(projectId, fileId, (err, fileSize) => {
|
|
|
|
if (err) {
|
|
|
|
if (err instanceof Errors.NotFoundError) {
|
|
|
|
res.status(404).end()
|
|
|
|
} else {
|
|
|
|
logger.err({ err, projectId, fileId }, 'error getting file size')
|
|
|
|
res.status(500).end()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
res.set('Content-Length', fileSize)
|
|
|
|
res.status(200).end()
|
|
|
|
})
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-06-20 07:17:55 -04:00
|
|
|
|
|
|
|
function isHtml(file) {
|
|
|
|
return (
|
|
|
|
fileEndsWith(file, '.html') ||
|
|
|
|
fileEndsWith(file, '.htm') ||
|
|
|
|
fileEndsWith(file, '.xhtml')
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function fileEndsWith(file, ext) {
|
|
|
|
return (
|
|
|
|
file.name != null &&
|
|
|
|
file.name.length > ext.length &&
|
|
|
|
file.name.lastIndexOf(ext) === file.name.length - ext.length
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function isMobileSafari(userAgent) {
|
|
|
|
return (
|
|
|
|
userAgent &&
|
|
|
|
(userAgent.indexOf('iPhone') >= 0 || userAgent.indexOf('iPad') >= 0)
|
|
|
|
)
|
|
|
|
}
|