2014-02-26 10:10:55 -05:00
|
|
|
logger = require("logger-sharelatex")
|
|
|
|
fs = require("fs")
|
|
|
|
LocalFileWriter = require("./LocalFileWriter")
|
2015-08-31 11:47:16 -04:00
|
|
|
Errors = require('./Errors')
|
2014-12-19 11:58:04 -05:00
|
|
|
rimraf = require("rimraf")
|
2015-11-16 03:59:27 -05:00
|
|
|
_ = require "underscore"
|
2014-02-26 10:10:55 -05:00
|
|
|
|
2014-03-01 10:10:47 -05:00
|
|
|
filterName = (key) ->
|
2014-03-31 10:15:00 -04:00
|
|
|
return key.replace /\//g, "_"
|
2015-08-31 11:47:16 -04:00
|
|
|
|
2014-02-26 10:10:55 -05:00
|
|
|
|
2014-03-01 10:10:47 -05:00
|
|
|
module.exports =
|
2014-02-26 10:10:55 -05:00
|
|
|
sendFile: ( location, target, source, callback = (err)->) ->
|
2014-03-01 10:10:47 -05:00
|
|
|
filteredTarget = filterName target
|
|
|
|
logger.log location:location, target:filteredTarget, source:source, "sending file"
|
2018-11-07 11:22:09 -05:00
|
|
|
done = _.once (err) ->
|
|
|
|
if err?
|
2014-03-01 10:23:11 -05:00
|
|
|
logger.err err:err, location:location, target:filteredTarget, source:source, "Error on put of file"
|
2018-11-07 11:22:09 -05:00
|
|
|
callback(err)
|
|
|
|
# actually copy the file (instead of moving it) to maintain consistent behaviour
|
|
|
|
# between the different implementations
|
|
|
|
sourceStream = fs.createReadStream source
|
|
|
|
sourceStream.on 'error', done
|
|
|
|
targetStream = fs.createWriteStream "#{location}/#{filteredTarget}"
|
|
|
|
targetStream.on 'error', done
|
|
|
|
targetStream.on 'finish', () ->
|
|
|
|
done()
|
|
|
|
sourceStream.pipe targetStream
|
2014-02-26 10:10:55 -05:00
|
|
|
|
|
|
|
sendStream: ( location, target, sourceStream, callback = (err)->) ->
|
2014-03-04 09:45:32 -05:00
|
|
|
logger.log location:location, target:target, "sending file stream"
|
2014-02-26 10:10:55 -05:00
|
|
|
sourceStream.on "error", (err)->
|
2014-03-04 09:45:32 -05:00
|
|
|
logger.err location:location, target:target, err:err "error on stream to send"
|
2014-02-26 10:10:55 -05:00
|
|
|
LocalFileWriter.writeStream sourceStream, null, (err, fsPath)=>
|
|
|
|
if err?
|
|
|
|
logger.err location:location, target:target, fsPath:fsPath, err:err, "something went wrong writing stream to disk"
|
|
|
|
return callback err
|
2018-11-07 11:22:09 -05:00
|
|
|
@sendFile location, target, fsPath, (err) ->
|
|
|
|
# delete the temporary file created above and return the original error
|
|
|
|
LocalFileWriter.deleteFile fsPath, () ->
|
|
|
|
callback(err)
|
2014-02-26 10:10:55 -05:00
|
|
|
|
2015-08-28 05:09:41 -04:00
|
|
|
# opts may be {start: Number, end: Number}
|
2019-05-01 20:04:59 -04:00
|
|
|
getFileStream: (location, name, opts, callback = (err, res)->) ->
|
2014-03-01 10:10:47 -05:00
|
|
|
filteredName = filterName name
|
|
|
|
logger.log location:location, name:filteredName, "getting file"
|
2019-05-01 20:04:59 -04:00
|
|
|
fs.open "#{location}/#{filteredName}", 'r', (err, fd) ->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, location:location, name:name, "Error reading from file"
|
|
|
|
if err.code == 'ENOENT'
|
|
|
|
return callback new Errors.NotFoundError(err.message), null
|
|
|
|
else
|
|
|
|
return callback err, null
|
|
|
|
opts.fd = fd
|
|
|
|
sourceStream = fs.createReadStream null, opts
|
2019-02-05 06:32:02 -05:00
|
|
|
return callback null, sourceStream
|
2014-05-23 08:57:18 -04:00
|
|
|
|
2014-02-26 10:10:55 -05:00
|
|
|
|
|
|
|
copyFile: (location, fromName, toName, callback = (err)->)->
|
2014-03-01 10:10:47 -05:00
|
|
|
filteredFromName=filterName fromName
|
|
|
|
filteredToName=filterName toName
|
|
|
|
logger.log location:location, fromName:filteredFromName, toName:filteredToName, "copying file"
|
|
|
|
sourceStream = fs.createReadStream "#{location}/#{filteredFromName}"
|
2014-02-26 10:10:55 -05:00
|
|
|
sourceStream.on 'error', (err) ->
|
2014-03-01 10:10:47 -05:00
|
|
|
logger.err err:err, location:location, key:filteredFromName, "Error reading from file"
|
2014-02-26 10:10:55 -05:00
|
|
|
callback err
|
2014-03-01 10:10:47 -05:00
|
|
|
targetStream = fs.createWriteStream "#{location}/#{filteredToName}"
|
2014-02-26 10:10:55 -05:00
|
|
|
targetStream.on 'error', (err) ->
|
2014-03-01 10:10:47 -05:00
|
|
|
logger.err err:err, location:location, key:filteredToName, "Error writing to file"
|
2014-02-26 10:10:55 -05:00
|
|
|
callback err
|
2014-12-22 06:27:00 -05:00
|
|
|
targetStream.on 'finish', () ->
|
|
|
|
callback null
|
2014-02-26 10:10:55 -05:00
|
|
|
sourceStream.pipe targetStream
|
|
|
|
|
|
|
|
deleteFile: (location, name, callback)->
|
2014-03-01 10:10:47 -05:00
|
|
|
filteredName = filterName name
|
|
|
|
logger.log location:location, name:filteredName, "delete file"
|
|
|
|
fs.unlink "#{location}/#{filteredName}", (err) ->
|
2014-12-22 06:11:31 -05:00
|
|
|
if err?
|
|
|
|
logger.err err:err, location:location, name:filteredName, "Error on delete."
|
|
|
|
callback err
|
|
|
|
else
|
|
|
|
callback()
|
2014-02-26 10:10:55 -05:00
|
|
|
|
|
|
|
deleteDirectory: (location, name, callback = (err)->)->
|
2014-12-19 11:58:04 -05:00
|
|
|
filteredName = filterName name.replace(/\/$/,'')
|
|
|
|
rimraf "#{location}/#{filteredName}", (err) ->
|
2014-12-22 06:11:31 -05:00
|
|
|
if err?
|
2014-12-19 11:58:04 -05:00
|
|
|
logger.err err:err, location:location, name:filteredName, "Error on rimraf rmdir."
|
2014-06-05 08:10:05 -04:00
|
|
|
callback err
|
|
|
|
else
|
|
|
|
callback()
|
2014-02-26 10:10:55 -05:00
|
|
|
|
|
|
|
checkIfFileExists:(location, name, callback = (err,exists)->)->
|
2014-03-01 10:10:47 -05:00
|
|
|
filteredName = filterName name
|
|
|
|
logger.log location:location, name:filteredName, "checking if file exists"
|
|
|
|
fs.exists "#{location}/#{filteredName}", (exists) ->
|
|
|
|
logger.log location:location, name:filteredName, exists:exists, "checked if file exists"
|
2014-02-26 10:10:55 -05:00
|
|
|
callback null, exists
|
2016-03-12 02:35:49 -05:00
|
|
|
|
2016-03-13 15:22:14 -04:00
|
|
|
directorySize:(location, name, callback)->
|
2016-03-12 09:08:07 -05:00
|
|
|
filteredName = filterName name.replace(/\/$/,'')
|
|
|
|
logger.log location:location, name:filteredName, "get project size in file system"
|
|
|
|
fs.readdir "#{location}/#{filteredName}", (err, files) ->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, location:location, name:filteredName, "something went wrong listing prefix in aws"
|
|
|
|
return callback(err)
|
|
|
|
totalSize = 0
|
|
|
|
_.each files, (entry)->
|
|
|
|
fd = fs.openSync "#{location}/#{filteredName}/#{entry}", 'r'
|
|
|
|
fileStats = fs.fstatSync(fd)
|
|
|
|
totalSize += fileStats.size
|
|
|
|
fs.closeSync fd
|
|
|
|
logger.log totalSize:totalSize, "total size", files:files
|
|
|
|
callback null, totalSize
|