overleaf/services/web/app/src/infrastructure/FileWriter.js
Shane Kilkelly 26af3e091f Merge pull request #1940 from overleaf/sk-catch-gitbridge-content-errors
GitBridge: catch 404 errors when fetching content from git-bridge api
GitOrigin-RevId: 411759ebae1fb81ebfa942e117d1ef3c47215d0e
2019-07-08 09:06:35 +00:00

125 lines
3.7 KiB
JavaScript

/* 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
*/
let FileWriter
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')
module.exports = FileWriter = {
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) {
logger.warn(
{ err, identifier, fsPath },
'[writeStreamToDisk] something went wrong with incoming stream'
)
return callback(err)
})
writeStream.on('error', function(err) {
logger.warn(
{ 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)
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) {
if (response.statusCode >= 200 && response.statusCode < 300) {
return FileWriter.writeStreamToDisk(identifier, stream, callback)
} else {
const err = new Error(`bad response from url: ${response.statusCode}`)
logger.warn({ err, identifier, url }, `[writeUrlToDisk] ${err.message}`)
return callback(err)
}
})
}
}