mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
35 lines
1 KiB
CoffeeScript
35 lines
1 KiB
CoffeeScript
fs = require("fs")
|
|
uuid = require('node-uuid')
|
|
path = require("path")
|
|
_ = require("underscore")
|
|
logger = require("logger-sharelatex")
|
|
metrics = require("./metrics")
|
|
|
|
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)
|
|
stream.pipe writeStream
|
|
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
|
|
|
|
deleteFile: (fsPath, callback)->
|
|
fs.unlink fsPath, callback
|
|
|
|
_getPath : (key)->
|
|
if !key?
|
|
key = uuid.v1()
|
|
key = key.replace(/\//g,"-")
|
|
path.join(__dirname, "../../uploads/#{key}")
|