2014-02-26 10:10:55 -05:00
|
|
|
logger = require("logger-sharelatex")
|
|
|
|
fs = require("fs")
|
|
|
|
LocalFileWriter = require("./LocalFileWriter")
|
|
|
|
|
2014-03-01 10:10:47 -05:00
|
|
|
filterName = (key) ->
|
2014-03-01 10:23:11 -05:00
|
|
|
return key.replace /\//, "_"
|
2014-03-01 10:10:47 -05: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"
|
|
|
|
fs.rename source, "#{location}/#{filteredTarget}", (err) ->
|
2014-03-01 10:23:11 -05:00
|
|
|
if err!=null
|
|
|
|
logger.err err:err, location:location, target:filteredTarget, source:source, "Error on put of file"
|
2014-02-26 10:10:55 -05:00
|
|
|
callback err
|
|
|
|
|
|
|
|
sendStream: ( location, target, sourceStream, callback = (err)->) ->
|
|
|
|
logger.log location:location, target:target, source:sourceStream, "sending file stream"
|
|
|
|
sourceStream.on "error", (err)->
|
|
|
|
logger.err location:location, target:target, source:sourceStream, err:err "error on stream to send"
|
|
|
|
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
|
|
|
|
|
|
|
|
getFileStream: (location, name, callback = (err, res)->)->
|
2014-03-01 10:10:47 -05:00
|
|
|
filteredName = filterName name
|
|
|
|
logger.log location:location, name:filteredName, "getting file"
|
|
|
|
sourceStream = fs.createReadStream "#{location}/#{filteredName}"
|
2014-02-26 10:10:55 -05:00
|
|
|
sourceStream.on 'error', (err) ->
|
|
|
|
logger.err err:err, location:location, name:name, "Error reading from file"
|
|
|
|
callback err
|
|
|
|
callback null,sourceStream
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
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) ->
|
|
|
|
logger.err err:err, location:location, name:filteredName, "Error on delete."
|
2014-02-26 10:10:55 -05:00
|
|
|
callback err
|
|
|
|
|
|
|
|
deleteDirectory: (location, name, callback = (err)->)->
|
2014-03-01 10:10:47 -05:00
|
|
|
filteredName = filterName name
|
|
|
|
fs.rmdir "#{location}/#{filteredName}", (err) ->
|
|
|
|
logger.err err:err, location:location, name:filteredName, "Error on rmdir."
|
2014-02-26 10:10:55 -05:00
|
|
|
callback err
|
|
|
|
|
|
|
|
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
|