2014-02-26 15:10:55 +00:00
|
|
|
logger = require("logger-sharelatex")
|
|
|
|
fs = require("fs")
|
|
|
|
LocalFileWriter = require("./LocalFileWriter")
|
2014-12-19 16:58:04 +00:00
|
|
|
rimraf = require("rimraf")
|
2014-12-22 11:10:05 +00:00
|
|
|
response = require ("response")
|
2014-02-26 15:10:55 +00:00
|
|
|
|
2014-03-01 15:10:47 +00:00
|
|
|
filterName = (key) ->
|
2014-03-31 14:15:00 +00:00
|
|
|
return key.replace /\//g, "_"
|
2014-03-01 15:10:47 +00:00
|
|
|
|
2014-02-26 15:10:55 +00:00
|
|
|
|
2014-03-01 15:10:47 +00:00
|
|
|
module.exports =
|
2014-02-26 15:10:55 +00:00
|
|
|
sendFile: ( location, target, source, callback = (err)->) ->
|
2014-03-01 15:10:47 +00:00
|
|
|
filteredTarget = filterName target
|
|
|
|
logger.log location:location, target:filteredTarget, source:source, "sending file"
|
|
|
|
fs.rename source, "#{location}/#{filteredTarget}", (err) ->
|
2014-03-01 15:23:11 +00:00
|
|
|
if err!=null
|
|
|
|
logger.err err:err, location:location, target:filteredTarget, source:source, "Error on put of file"
|
2014-02-26 15:10:55 +00:00
|
|
|
callback err
|
|
|
|
|
|
|
|
sendStream: ( location, target, sourceStream, callback = (err)->) ->
|
2014-03-04 14:45:32 +00:00
|
|
|
logger.log location:location, target:target, "sending file stream"
|
2014-02-26 15:10:55 +00:00
|
|
|
sourceStream.on "error", (err)->
|
2014-03-04 14:45:32 +00:00
|
|
|
logger.err location:location, target:target, err:err "error on stream to send"
|
2014-02-26 15:10:55 +00: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
|
|
|
|
@sendFile location, target, fsPath, callback
|
|
|
|
|
2015-02-26 11:32:05 +00:00
|
|
|
getFileStream: (location, name, _callback = (err, res)->) ->
|
|
|
|
callback = (args...) ->
|
|
|
|
_callback(args...)
|
|
|
|
_callback = () ->
|
2014-03-01 15:10:47 +00:00
|
|
|
filteredName = filterName name
|
|
|
|
logger.log location:location, name:filteredName, "getting file"
|
2014-05-23 12:57:18 +00:00
|
|
|
sourceStream = fs.createReadStream "#{location}/#{filteredName}"
|
|
|
|
sourceStream.on 'error', (err) ->
|
|
|
|
logger.err err:err, location:location, name:name, "Error reading from file"
|
2014-12-22 11:10:05 +00:00
|
|
|
if err.code = 'ENOENT'
|
|
|
|
callback null, response().html('NoSuchKey: file not found\n')
|
|
|
|
else
|
|
|
|
callback err
|
|
|
|
sourceStream.on 'readable', () ->
|
2015-02-26 11:32:05 +00:00
|
|
|
# This can be called multiple times, but the callback wrapper
|
|
|
|
# ensures the callback is only called once
|
2014-12-22 11:10:05 +00:00
|
|
|
callback null, sourceStream
|
2014-05-23 12:57:18 +00:00
|
|
|
|
2014-02-26 15:10:55 +00:00
|
|
|
|
|
|
|
copyFile: (location, fromName, toName, callback = (err)->)->
|
2014-03-01 15:10:47 +00: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 15:10:55 +00:00
|
|
|
sourceStream.on 'error', (err) ->
|
2014-03-01 15:10:47 +00:00
|
|
|
logger.err err:err, location:location, key:filteredFromName, "Error reading from file"
|
2014-02-26 15:10:55 +00:00
|
|
|
callback err
|
2014-03-01 15:10:47 +00:00
|
|
|
targetStream = fs.createWriteStream "#{location}/#{filteredToName}"
|
2014-02-26 15:10:55 +00:00
|
|
|
targetStream.on 'error', (err) ->
|
2014-03-01 15:10:47 +00:00
|
|
|
logger.err err:err, location:location, key:filteredToName, "Error writing to file"
|
2014-02-26 15:10:55 +00:00
|
|
|
callback err
|
2014-12-22 11:27:00 +00:00
|
|
|
targetStream.on 'finish', () ->
|
|
|
|
callback null
|
2014-02-26 15:10:55 +00:00
|
|
|
sourceStream.pipe targetStream
|
|
|
|
|
|
|
|
deleteFile: (location, name, callback)->
|
2014-03-01 15:10:47 +00:00
|
|
|
filteredName = filterName name
|
|
|
|
logger.log location:location, name:filteredName, "delete file"
|
|
|
|
fs.unlink "#{location}/#{filteredName}", (err) ->
|
2014-12-22 11:11:31 +00:00
|
|
|
if err?
|
|
|
|
logger.err err:err, location:location, name:filteredName, "Error on delete."
|
|
|
|
callback err
|
|
|
|
else
|
|
|
|
callback()
|
2014-02-26 15:10:55 +00:00
|
|
|
|
|
|
|
deleteDirectory: (location, name, callback = (err)->)->
|
2014-12-19 16:58:04 +00:00
|
|
|
filteredName = filterName name.replace(/\/$/,'')
|
|
|
|
rimraf "#{location}/#{filteredName}", (err) ->
|
2014-12-22 11:11:31 +00:00
|
|
|
if err?
|
2014-12-19 16:58:04 +00:00
|
|
|
logger.err err:err, location:location, name:filteredName, "Error on rimraf rmdir."
|
2014-06-05 12:10:05 +00:00
|
|
|
callback err
|
|
|
|
else
|
|
|
|
callback()
|
2014-02-26 15:10:55 +00:00
|
|
|
|
|
|
|
checkIfFileExists:(location, name, callback = (err,exists)->)->
|
2014-03-01 15:10:47 +00: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 15:10:55 +00:00
|
|
|
callback null, exists
|