2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
|
|
|
handle-callback-err,
|
|
|
|
max-len,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
const fs = require('fs')
|
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
const uuid = require('uuid')
|
|
|
|
const _ = require('underscore')
|
|
|
|
const Settings = require('settings-sharelatex')
|
|
|
|
const request = require('request')
|
2020-03-04 04:37:43 -05:00
|
|
|
const { promisifyAll } = require('../util/promises')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-03-04 04:37:43 -05:00
|
|
|
const FileWriter = {
|
2019-05-29 05:21:06 -04:00
|
|
|
ensureDumpFolderExists(callback) {
|
|
|
|
if (callback == null) {
|
|
|
|
callback = function(error) {}
|
|
|
|
}
|
|
|
|
return fs.mkdir(Settings.path.dumpFolder, function(error) {
|
|
|
|
if (error != null && error.code !== 'EEXIST') {
|
|
|
|
// Ignore error about already existing
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
return callback(null)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
writeLinesToDisk(identifier, lines, callback) {
|
|
|
|
if (callback == null) {
|
|
|
|
callback = function(error, fsPath) {}
|
|
|
|
}
|
|
|
|
return FileWriter.writeContentToDisk(identifier, lines.join('\n'), callback)
|
|
|
|
},
|
|
|
|
|
|
|
|
writeContentToDisk(identifier, content, callback) {
|
|
|
|
if (callback == null) {
|
|
|
|
callback = function(error, fsPath) {}
|
|
|
|
}
|
|
|
|
callback = _.once(callback)
|
|
|
|
const fsPath = `${Settings.path.dumpFolder}/${identifier}_${uuid.v4()}`
|
|
|
|
return FileWriter.ensureDumpFolderExists(function(error) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
return fs.writeFile(fsPath, content, function(error) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
return callback(null, fsPath)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
writeStreamToDisk(identifier, stream, callback) {
|
|
|
|
if (callback == null) {
|
|
|
|
callback = function(error, fsPath) {}
|
|
|
|
}
|
|
|
|
callback = _.once(callback)
|
|
|
|
const fsPath = `${Settings.path.dumpFolder}/${identifier}_${uuid.v4()}`
|
|
|
|
|
|
|
|
stream.pause()
|
|
|
|
return FileWriter.ensureDumpFolderExists(function(error) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
stream.resume()
|
|
|
|
|
|
|
|
const writeStream = fs.createWriteStream(fsPath)
|
|
|
|
stream.pipe(writeStream)
|
|
|
|
|
|
|
|
stream.on('error', function(err) {
|
2019-07-01 09:48:09 -04:00
|
|
|
logger.warn(
|
2019-05-29 05:21:06 -04:00
|
|
|
{ err, identifier, fsPath },
|
|
|
|
'[writeStreamToDisk] something went wrong with incoming stream'
|
|
|
|
)
|
|
|
|
return callback(err)
|
|
|
|
})
|
|
|
|
writeStream.on('error', function(err) {
|
2019-07-01 09:48:09 -04:00
|
|
|
logger.warn(
|
2019-05-29 05:21:06 -04:00
|
|
|
{ err, identifier, fsPath },
|
|
|
|
'[writeStreamToDisk] something went wrong with writing to disk'
|
|
|
|
)
|
|
|
|
return callback(err)
|
|
|
|
})
|
|
|
|
return writeStream.on('finish', function() {
|
|
|
|
logger.log(
|
|
|
|
{ identifier, fsPath },
|
|
|
|
'[writeStreamToDisk] write stream finished'
|
|
|
|
)
|
|
|
|
return callback(null, fsPath)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
writeUrlToDisk(identifier, url, callback) {
|
|
|
|
if (callback == null) {
|
|
|
|
callback = function(error, fsPath) {}
|
|
|
|
}
|
|
|
|
callback = _.once(callback)
|
|
|
|
const stream = request.get(url)
|
2019-07-08 04:56:08 -04:00
|
|
|
stream.on('error', function(err) {
|
|
|
|
logger.warn(
|
|
|
|
{ err, identifier, url },
|
|
|
|
'[writeUrlToDisk] something went wrong with writing to disk'
|
|
|
|
)
|
|
|
|
callback(err)
|
|
|
|
})
|
|
|
|
stream.on('response', function(response) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
|
|
return FileWriter.writeStreamToDisk(identifier, stream, callback)
|
|
|
|
} else {
|
|
|
|
const err = new Error(`bad response from url: ${response.statusCode}`)
|
2019-07-08 04:56:08 -04:00
|
|
|
logger.warn({ err, identifier, url }, `[writeUrlToDisk] ${err.message}`)
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 04:37:43 -05:00
|
|
|
|
|
|
|
module.exports = FileWriter
|
|
|
|
module.exports.promises = promisifyAll(FileWriter)
|