FileWriter.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* eslint-disable
  2. handle-callback-err,
  3. max-len,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const fs = require('fs')
  14. const logger = require('logger-sharelatex')
  15. const uuid = require('uuid')
  16. const _ = require('underscore')
  17. const Settings = require('settings-sharelatex')
  18. const request = require('request')
  19. const { promisifyAll } = require('../util/promises')
  20. const FileWriter = {
  21. ensureDumpFolderExists(callback) {
  22. if (callback == null) {
  23. callback = function(error) {}
  24. }
  25. return fs.mkdir(Settings.path.dumpFolder, function(error) {
  26. if (error != null && error.code !== 'EEXIST') {
  27. // Ignore error about already existing
  28. return callback(error)
  29. }
  30. return callback(null)
  31. })
  32. },
  33. writeLinesToDisk(identifier, lines, callback) {
  34. if (callback == null) {
  35. callback = function(error, fsPath) {}
  36. }
  37. return FileWriter.writeContentToDisk(identifier, lines.join('\n'), callback)
  38. },
  39. writeContentToDisk(identifier, content, callback) {
  40. if (callback == null) {
  41. callback = function(error, fsPath) {}
  42. }
  43. callback = _.once(callback)
  44. const fsPath = `${Settings.path.dumpFolder}/${identifier}_${uuid.v4()}`
  45. return FileWriter.ensureDumpFolderExists(function(error) {
  46. if (error != null) {
  47. return callback(error)
  48. }
  49. return fs.writeFile(fsPath, content, function(error) {
  50. if (error != null) {
  51. return callback(error)
  52. }
  53. return callback(null, fsPath)
  54. })
  55. })
  56. },
  57. writeStreamToDisk(identifier, stream, callback) {
  58. if (callback == null) {
  59. callback = function(error, fsPath) {}
  60. }
  61. callback = _.once(callback)
  62. const fsPath = `${Settings.path.dumpFolder}/${identifier}_${uuid.v4()}`
  63. stream.pause()
  64. return FileWriter.ensureDumpFolderExists(function(error) {
  65. if (error != null) {
  66. return callback(error)
  67. }
  68. stream.resume()
  69. const writeStream = fs.createWriteStream(fsPath)
  70. stream.pipe(writeStream)
  71. stream.on('error', function(err) {
  72. logger.warn(
  73. { err, identifier, fsPath },
  74. '[writeStreamToDisk] something went wrong with incoming stream'
  75. )
  76. return callback(err)
  77. })
  78. writeStream.on('error', function(err) {
  79. logger.warn(
  80. { err, identifier, fsPath },
  81. '[writeStreamToDisk] something went wrong with writing to disk'
  82. )
  83. return callback(err)
  84. })
  85. return writeStream.on('finish', function() {
  86. logger.log(
  87. { identifier, fsPath },
  88. '[writeStreamToDisk] write stream finished'
  89. )
  90. return callback(null, fsPath)
  91. })
  92. })
  93. },
  94. writeUrlToDisk(identifier, url, callback) {
  95. if (callback == null) {
  96. callback = function(error, fsPath) {}
  97. }
  98. callback = _.once(callback)
  99. const stream = request.get(url)
  100. stream.on('error', function(err) {
  101. logger.warn(
  102. { err, identifier, url },
  103. '[writeUrlToDisk] something went wrong with writing to disk'
  104. )
  105. callback(err)
  106. })
  107. stream.on('response', function(response) {
  108. if (response.statusCode >= 200 && response.statusCode < 300) {
  109. return FileWriter.writeStreamToDisk(identifier, stream, callback)
  110. } else {
  111. const err = new Error(`bad response from url: ${response.statusCode}`)
  112. logger.warn({ err, identifier, url }, `[writeUrlToDisk] ${err.message}`)
  113. return callback(err)
  114. }
  115. })
  116. }
  117. }
  118. module.exports = FileWriter
  119. module.exports.promises = promisifyAll(FileWriter)