LocalFileWriter.coffee 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. fs = require("fs")
  2. uuid = require('node-uuid')
  3. path = require("path")
  4. _ = require("underscore")
  5. logger = require("logger-sharelatex")
  6. metrics = require("metrics-sharelatex")
  7. Settings = require("settings-sharelatex")
  8. module.exports =
  9. writeStream: (stream, key, callback)->
  10. timer = new metrics.Timer("writingFile")
  11. callback = _.once callback
  12. fsPath = @_getPath(key)
  13. logger.log fsPath:fsPath, "writing file locally"
  14. writeStream = fs.createWriteStream(fsPath)
  15. writeStream.on "finish", ->
  16. timer.done()
  17. logger.log fsPath:fsPath, "finished writing file locally"
  18. callback(null, fsPath)
  19. writeStream.on "error", (err)->
  20. logger.err err:err, fsPath:fsPath, "problem writing file locally, with write stream"
  21. callback err
  22. stream.on "error", (err)->
  23. logger.log err:err, fsPath:fsPath, "problem writing file locally, with read stream"
  24. callback err
  25. stream.pipe writeStream
  26. deleteFile: (fsPath, callback)->
  27. logger.log fsPath:fsPath, "removing local temp file"
  28. fs.unlink fsPath, callback
  29. _getPath : (key)->
  30. if !key?
  31. key = uuid.v1()
  32. key = key.replace(/\//g,"-")
  33. console.log Settings.path.uploadFolder, key
  34. path.join(Settings.path.uploadFolder, key)