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"
|
2015-08-28 07:16:41 -04:00
|
|
|
logger.log key:key, bucket:bucket, format:format, style: style, "reciving 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?
|
|
|
|
logger.err err:err, key:key, bucket:bucket, format:format, style:style, "problem getting file"
|
2015-08-31 11:47:16 -04:00
|
|
|
if err instanceof Errors.NotFoundError
|
|
|
|
return res.send 404
|
2015-08-28 06:45:16 -04:00
|
|
|
if !res.finished and res?.send?
|
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
|
|
|
|
|
|
|
|
insertFile: (req, res)->
|
|
|
|
metrics.inc "insertFile"
|
|
|
|
{key, bucket} = req
|
|
|
|
logger.log key:key, bucket:bucket, "reciving request to insert file"
|
|
|
|
FileHandler.insertFile bucket, key, req, (err)->
|
|
|
|
res.send 200
|
|
|
|
|
|
|
|
copyFile: (req, res)->
|
|
|
|
metrics.inc "copyFile"
|
|
|
|
{key, bucket} = req
|
|
|
|
oldProject_id = req.body.source.project_id
|
|
|
|
oldFile_id = req.body.source.file_id
|
|
|
|
logger.log key:key, bucket:bucket, oldProject_id:oldProject_id, oldFile_id:oldFile_id, "reciving 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?
|
2014-02-25 04:10:22 -05:00
|
|
|
logger.log err:err, oldProject_id:oldProject_id, oldFile_id:oldFile_id, "something went wrong copying file"
|
2014-02-14 11:39:05 -05:00
|
|
|
res.send 500
|
|
|
|
else
|
|
|
|
res.send 200
|
|
|
|
|
|
|
|
deleteFile: (req, res)->
|
|
|
|
metrics.inc "deleteFile"
|
|
|
|
{key, bucket} = req
|
|
|
|
logger.log key:key, bucket:bucket, "reciving request to delete file"
|
|
|
|
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}
|