2018-02-14 10:12:46 -05:00
|
|
|
fs = require 'fs'
|
|
|
|
logger = require 'logger-sharelatex'
|
|
|
|
uuid = require 'uuid'
|
|
|
|
_ = require 'underscore'
|
|
|
|
Settings = require 'settings-sharelatex'
|
2018-03-08 12:24:54 -05:00
|
|
|
request = require 'request'
|
2018-02-14 10:12:46 -05:00
|
|
|
|
2018-03-08 12:24:54 -05:00
|
|
|
module.exports = FileWriter =
|
2018-05-03 09:29:03 -04:00
|
|
|
|
2019-05-01 05:23:35 -04:00
|
|
|
ensureDumpFolderExists: (callback=(error)->) ->
|
2018-05-03 09:29:03 -04:00
|
|
|
fs.mkdir Settings.path.dumpFolder, (error) ->
|
|
|
|
if error? and error.code != 'EEXIST'
|
|
|
|
# Ignore error about already existing
|
|
|
|
return callback(error)
|
2018-05-24 06:30:29 -04:00
|
|
|
callback(null)
|
|
|
|
|
|
|
|
writeLinesToDisk: (identifier, lines, callback = (error, fsPath)->) ->
|
2018-06-20 05:01:03 -04:00
|
|
|
FileWriter.writeContentToDisk(identifier, lines.join('\n'), callback)
|
|
|
|
|
|
|
|
writeContentToDisk: (identifier, content, callback = (error, fsPath)->) ->
|
2018-05-24 06:30:29 -04:00
|
|
|
callback = _.once(callback)
|
|
|
|
fsPath = "#{Settings.path.dumpFolder}/#{identifier}_#{uuid.v4()}"
|
2019-05-01 05:23:35 -04:00
|
|
|
FileWriter.ensureDumpFolderExists (error) ->
|
2018-05-24 06:30:29 -04:00
|
|
|
return callback(error) if error?
|
2018-06-20 05:01:03 -04:00
|
|
|
fs.writeFile fsPath, content, (error) ->
|
2018-05-03 09:29:03 -04:00
|
|
|
return callback(error) if error?
|
|
|
|
callback(null, fsPath)
|
|
|
|
|
2018-02-14 10:12:46 -05:00
|
|
|
writeStreamToDisk: (identifier, stream, callback = (error, fsPath) ->) ->
|
|
|
|
callback = _.once(callback)
|
|
|
|
fsPath = "#{Settings.path.dumpFolder}/#{identifier}_#{uuid.v4()}"
|
|
|
|
|
2018-03-16 08:21:07 -04:00
|
|
|
stream.pause()
|
2019-05-01 05:23:35 -04:00
|
|
|
FileWriter.ensureDumpFolderExists (error) ->
|
2018-05-24 06:30:29 -04:00
|
|
|
return callback(error) if error?
|
2018-03-16 08:21:07 -04:00
|
|
|
stream.resume()
|
2018-02-14 10:12:46 -05:00
|
|
|
|
2018-03-16 08:21:07 -04:00
|
|
|
writeStream = fs.createWriteStream(fsPath)
|
|
|
|
stream.pipe(writeStream)
|
|
|
|
|
|
|
|
stream.on 'error', (err)->
|
|
|
|
logger.err {err, identifier, fsPath}, "[writeStreamToDisk] something went wrong with incoming stream"
|
|
|
|
callback(err)
|
|
|
|
writeStream.on 'error', (err)->
|
|
|
|
logger.err {err, identifier, fsPath}, "[writeStreamToDisk] something went wrong with writing to disk"
|
|
|
|
callback(err)
|
|
|
|
writeStream.on "finish", ->
|
|
|
|
logger.log {identifier, fsPath}, "[writeStreamToDisk] write stream finished"
|
|
|
|
callback null, fsPath
|
2018-03-08 12:24:54 -05:00
|
|
|
|
|
|
|
writeUrlToDisk: (identifier, url, callback = (error, fsPath) ->) ->
|
|
|
|
callback = _.once(callback)
|
|
|
|
stream = request.get(url)
|
|
|
|
stream.on 'response', (response) ->
|
|
|
|
if 200 <= response.statusCode < 300
|
|
|
|
FileWriter.writeStreamToDisk identifier, stream, callback
|
|
|
|
else
|
|
|
|
err = new Error("bad response from url: #{response.statusCode}")
|
|
|
|
logger.err {err, identifier, url}, err.message
|
2018-05-03 09:29:03 -04:00
|
|
|
callback(err)
|