2014-02-12 10:23:40 +00:00
logger = require ( " logger-sharelatex " )
fs = require ( " fs " )
request = require ( " request " )
settings = require ( " settings-sharelatex " )
2014-05-08 09:55:44 +00:00
oneMinInMs = 60 * 1000
fiveMinsInMs = oneMinInMs * 5
2016-03-12 12:01:36 +00:00
module.exports = FileStoreHandler =
2014-02-12 10:23:40 +00:00
uploadFileFromDisk: (project_id, file_id, fsPath, callback)->
2016-03-12 12:01:36 +00:00
fs . lstat fsPath , (err, stat)->
if err ?
2016-03-12 15:05:29 +00:00
logger . err err : err , project_id : project_id , file_id : file_id , fsPath : fsPath , " error stating file "
callback ( err )
2017-03-17 13:03:16 +00:00
if ! stat ?
logger . err project_id : project_id , file_id : file_id , fsPath : fsPath , " stat is not available, can not check file from disk "
return callback ( new Error ( " error getting stat, not available " ) )
2016-03-12 15:05:29 +00:00
if ! stat . isFile ( )
logger . log project_id : project_id , file_id : file_id , fsPath : fsPath , " tried to upload symlink, not contining "
return callback ( new Error ( " can not upload symlink " ) )
2017-04-28 13:47:18 +00:00
_cb = callback
2017-11-10 15:47:47 +00:00
callback = (err, url) ->
2017-04-28 13:47:18 +00:00
callback = -> # avoid double callbacks
2017-11-10 15:47:47 +00:00
_cb ( err , url )
2017-04-28 13:47:18 +00:00
2016-03-12 12:01:36 +00:00
logger . log project_id : project_id , file_id : file_id , fsPath : fsPath , " uploading file from disk "
readStream = fs . createReadStream ( fsPath )
readStream . on " error " , (err)->
logger . err err : err , project_id : project_id , file_id : file_id , fsPath : fsPath , " something went wrong on the read stream of uploadFileFromDisk "
callback err
2017-04-28 13:47:18 +00:00
readStream . on " open " , () ->
2017-11-10 15:47:47 +00:00
url = FileStoreHandler . _buildUrl ( project_id , file_id )
2017-04-28 13:47:18 +00:00
opts =
method: " post "
2017-11-10 15:47:47 +00:00
uri: url
2017-04-28 13:47:18 +00:00
timeout : fiveMinsInMs
writeStream = request ( opts )
writeStream . on " error " , (err)->
logger . err err : err , project_id : project_id , file_id : file_id , fsPath : fsPath , " something went wrong on the write stream of uploadFileFromDisk "
callback err
writeStream . on ' response ' , (response) ->
if response . statusCode not in [ 200 , 201 ]
err = new Error ( " non-ok response from filestore for upload: #{ response . statusCode } " )
logger . err { err , statusCode: response . statusCode } , " error uploading to filestore "
callback ( err )
else
2017-11-10 15:47:47 +00:00
callback ( null , url )
2017-04-28 13:47:18 +00:00
readStream . pipe writeStream
2014-02-12 10:23:40 +00:00
getFileStream: (project_id, file_id, query, callback)->
logger . log project_id : project_id , file_id : file_id , query : query , " getting file stream from file store "
queryString = " "
if query ? and query [ " format " ] ?
queryString = " ?format= #{ query [ ' format ' ] } "
opts =
method : " get "
uri: " #{ @ _buildUrl ( project_id , file_id ) } #{ queryString } "
2014-05-08 09:55:44 +00:00
timeout : fiveMinsInMs
2016-05-17 15:59:47 +00:00
headers: { }
if query ? and query [ ' range ' ] ?
rangeText = query [ ' range ' ]
if rangeText && rangeText . match ? && rangeText . match ( /\d+-\d+/ )
opts . headers [ ' range ' ] = " bytes= #{ query [ ' range ' ] } "
2014-02-12 10:23:40 +00:00
readStream = request ( opts )
2016-03-23 14:38:11 +00:00
readStream . on " error " , (err) ->
logger . err { err , project_id , file_id , query } , " error in file stream "
2014-02-12 10:23:40 +00:00
callback ( null , readStream )
deleteFile: (project_id, file_id, callback)->
logger . log project_id : project_id , file_id : file_id , " telling file store to delete file "
opts =
method : " delete "
uri: @ _buildUrl ( project_id , file_id )
2014-05-08 09:55:44 +00:00
timeout : fiveMinsInMs
2014-02-12 10:23:40 +00:00
request opts , (err, response)->
if err ?
logger . err err : err , project_id : project_id , file_id : file_id , " something went wrong deleting file from filestore "
callback ( err )
copyFile: (oldProject_id, oldFile_id, newProject_id, newFile_id, callback)->
logger . log oldProject_id : oldProject_id , oldFile_id : oldFile_id , newProject_id : newProject_id , newFile_id : newFile_id , " telling filestore to copy a file "
opts =
method : " put "
json:
source:
project_id : oldProject_id
file_id : oldFile_id
uri: @ _buildUrl ( newProject_id , newFile_id )
2014-05-08 09:55:44 +00:00
timeout : fiveMinsInMs
2014-02-12 10:23:40 +00:00
request opts , (err)->
if err ?
logger . err err : err , oldProject_id : oldProject_id , oldFile_id : oldFile_id , newProject_id : newProject_id , newFile_id : newFile_id , " something went wrong telling filestore api to copy file "
2017-11-24 11:13:44 +00:00
callback ( err , opts . uri )
2014-02-12 10:23:40 +00:00
_buildUrl: (project_id, file_id)->
2017-03-09 10:11:45 +00:00
return " #{ settings . apis . filestore . url } /project/ #{ project_id } /file/ #{ file_id } "