overleaf/services/filestore/app/coffee/LocalFileWriter.coffee

41 lines
1.2 KiB
CoffeeScript
Raw Normal View History

2014-02-14 16:39:05 +00:00
fs = require("fs")
uuid = require('node-uuid')
path = require("path")
_ = require("underscore")
logger = require("logger-sharelatex")
metrics = require("metrics-sharelatex")
2014-05-17 20:01:48 +00:00
Settings = require("settings-sharelatex")
2014-02-14 16:39:05 +00:00
module.exports =
writeStream: (stream, key, callback)->
timer = new metrics.Timer("writingFile")
callback = _.once callback
fsPath = @_getPath(key)
logger.log fsPath:fsPath, "writing file locally"
writeStream = fs.createWriteStream(fsPath)
writeStream.on "finish", ->
timer.done()
logger.log fsPath:fsPath, "finished writing file locally"
callback(null, fsPath)
writeStream.on "error", (err)->
logger.err err:err, fsPath:fsPath, "problem writing file locally, with write stream"
callback err
stream.on "error", (err)->
logger.log err:err, fsPath:fsPath, "problem writing file locally, with read stream"
callback err
2015-05-08 14:19:45 +00:00
stream.pipe writeStream
2014-02-14 16:39:05 +00:00
deleteFile: (fsPath, callback)->
2016-12-19 14:48:09 +00:00
if !fsPath? or fsPath == ""
return callback()
logger.log fsPath:fsPath, "removing local temp file"
2014-02-14 16:39:05 +00:00
fs.unlink fsPath, callback
_getPath : (key)->
if !key?
key = uuid.v1()
key = key.replace(/\//g,"-")
console.log Settings.path.uploadFolder, key
2014-05-17 20:01:48 +00:00
path.join(Settings.path.uploadFolder, key)