2014-02-26 04:15:37 -05:00
|
|
|
PersistorManager = require("./PersistorManager")
|
2014-02-14 11:39:05 -05:00
|
|
|
settings = require("settings-sharelatex")
|
|
|
|
logger = require("logger-sharelatex")
|
|
|
|
FileHandler = require("./FileHandler")
|
2014-05-12 10:45:15 -04:00
|
|
|
metrics = require("metrics-sharelatex")
|
2015-08-28 06:45:16 -04:00
|
|
|
parseRange = require('range-parser')
|
2015-08-31 11:47:16 -04:00
|
|
|
Errors = require('./Errors')
|
2015-08-28 06:45:16 -04:00
|
|
|
|
2014-02-14 11:39:05 -05:00
|
|
|
oneDayInSeconds = 60 * 60 * 24
|
2015-08-28 06:45:16 -04:00
|
|
|
maxSizeInBytes = 1024 * 1024 * 1024 # 1GB
|
|
|
|
|
2015-08-28 07:02:50 -04:00
|
|
|
module.exports = FileController =
|
2014-02-14 11:39:05 -05:00
|
|
|
|
|
|
|
getFile: (req, res)->
|
|
|
|
{key, bucket} = req
|
|
|
|
{format, style} = req.query
|
2015-08-28 07:16:41 -04:00
|
|
|
options = {
|
2015-08-28 06:45:16 -04:00
|
|
|
key: key,
|
|
|
|
bucket: bucket,
|
|
|
|
format: format,
|
|
|
|
style: style,
|
|
|
|
}
|
|
|
|
metrics.inc "getFile"
|
2019-06-13 16:57:49 -04:00
|
|
|
logger.log key:key, bucket:bucket, format:format, style: style, "receiving request to get file"
|
2015-08-28 06:45:16 -04:00
|
|
|
if req.headers.range?
|
2015-08-28 07:02:50 -04:00
|
|
|
range = FileController._get_range(req.headers.range)
|
2015-08-28 07:16:41 -04:00
|
|
|
options.start = range.start
|
|
|
|
options.end = range.end
|
|
|
|
logger.log start: range.start, end: range.end, "getting range of bytes from file"
|
|
|
|
FileHandler.getFile bucket, key, options, (err, fileStream)->
|
2014-02-14 11:39:05 -05:00
|
|
|
if err?
|
2015-08-31 11:47:16 -04:00
|
|
|
if err instanceof Errors.NotFoundError
|
|
|
|
return res.send 404
|
2017-03-08 09:59:34 -05:00
|
|
|
else
|
2018-11-09 09:04:26 -05:00
|
|
|
logger.err err:err, key:key, bucket:bucket, format:format, style:style, "problem getting file"
|
2015-08-31 11:47:16 -04:00
|
|
|
return res.send 500
|
2014-02-26 04:06:47 -05:00
|
|
|
else if req.query.cacheWarm
|
2014-02-14 11:39:05 -05:00
|
|
|
logger.log key:key, bucket:bucket, format:format, style:style, "request is only for cache warm so not sending stream"
|
|
|
|
res.send 200
|
|
|
|
else
|
|
|
|
logger.log key:key, bucket:bucket, format:format, style:style, "sending file to response"
|
|
|
|
fileStream.pipe res
|
|
|
|
|
2019-06-13 16:57:49 -04:00
|
|
|
getFileHead: (req, res) ->
|
|
|
|
{key, bucket} = req
|
|
|
|
metrics.inc("getFileSize")
|
|
|
|
logger.log({ key: key, bucket: bucket }, "receiving request to get file metadata")
|
|
|
|
FileHandler.getFileSize bucket, key, (err, fileSize) ->
|
|
|
|
if err?
|
|
|
|
if err instanceof Errors.NotFoundError
|
|
|
|
res.status(404).end()
|
|
|
|
else
|
|
|
|
res.status(500).end()
|
|
|
|
return
|
|
|
|
res.set("Content-Length", fileSize)
|
|
|
|
res.status(200).end()
|
|
|
|
|
2014-02-14 11:39:05 -05:00
|
|
|
insertFile: (req, res)->
|
|
|
|
metrics.inc "insertFile"
|
|
|
|
{key, bucket} = req
|
2019-06-13 16:57:49 -04:00
|
|
|
logger.log key:key, bucket:bucket, "receiving request to insert file"
|
2014-02-14 11:39:05 -05:00
|
|
|
FileHandler.insertFile bucket, key, req, (err)->
|
2015-09-10 06:10:30 -04:00
|
|
|
if err?
|
|
|
|
logger.log err: err, key: key, bucket: bucket, "error inserting file"
|
|
|
|
res.send 500
|
2015-11-25 11:07:51 -05:00
|
|
|
else
|
|
|
|
res.send 200
|
2014-02-14 11:39:05 -05:00
|
|
|
|
|
|
|
copyFile: (req, res)->
|
|
|
|
metrics.inc "copyFile"
|
|
|
|
{key, bucket} = req
|
|
|
|
oldProject_id = req.body.source.project_id
|
|
|
|
oldFile_id = req.body.source.file_id
|
2019-06-13 16:57:49 -04:00
|
|
|
logger.log key:key, bucket:bucket, oldProject_id:oldProject_id, oldFile_id:oldFile_id, "receiving request to copy file"
|
2014-02-26 04:15:37 -05:00
|
|
|
PersistorManager.copyFile bucket, "#{oldProject_id}/#{oldFile_id}", key, (err)->
|
2015-08-28 06:45:16 -04:00
|
|
|
if err?
|
2019-01-09 05:31:59 -05:00
|
|
|
if err instanceof Errors.NotFoundError
|
|
|
|
res.send 404
|
|
|
|
else
|
|
|
|
logger.log err:err, oldProject_id:oldProject_id, oldFile_id:oldFile_id, "something went wrong copying file"
|
|
|
|
res.send 500
|
2014-02-14 11:39:05 -05:00
|
|
|
else
|
|
|
|
res.send 200
|
|
|
|
|
|
|
|
deleteFile: (req, res)->
|
|
|
|
metrics.inc "deleteFile"
|
|
|
|
{key, bucket} = req
|
2019-06-13 16:57:49 -04:00
|
|
|
logger.log key:key, bucket:bucket, "receiving request to delete file"
|
2014-02-14 11:39:05 -05:00
|
|
|
FileHandler.deleteFile bucket, key, (err)->
|
|
|
|
if err?
|
2014-02-25 04:10:22 -05:00
|
|
|
logger.log err:err, key:key, bucket:bucket, "something went wrong deleting file"
|
2014-02-14 11:39:05 -05:00
|
|
|
res.send 500
|
|
|
|
else
|
|
|
|
res.send 204
|
2015-08-28 07:02:50 -04:00
|
|
|
|
|
|
|
_get_range: (header) ->
|
|
|
|
parsed = parseRange(maxSizeInBytes, header)
|
|
|
|
if parsed == -1 or parsed == -2 or parsed.type != 'bytes'
|
|
|
|
null
|
|
|
|
else
|
|
|
|
range = parsed[0]
|
|
|
|
{start: range.start, end: range.end}
|
2016-03-13 15:22:14 -04:00
|
|
|
|
|
|
|
directorySize: (req, res)->
|
|
|
|
metrics.inc "projectSize"
|
|
|
|
{project_id, bucket} = req
|
2019-06-13 16:57:49 -04:00
|
|
|
logger.log project_id:project_id, bucket:bucket, "receiving request to project size"
|
2016-03-13 15:22:14 -04:00
|
|
|
FileHandler.getDirectorySize bucket, project_id, (err, size)->
|
|
|
|
if err?
|
|
|
|
logger.log err: err, project_id: project_id, bucket: bucket, "error inserting file"
|
|
|
|
res.send 500
|
|
|
|
else
|
|
|
|
res.json {'total bytes' : size}
|