LocalFileWriter.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* eslint-disable
  2. handle-callback-err,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS102: Remove unnecessary code created because of implicit returns
  9. * DS207: Consider shorter variations of null checks
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const fs = require('fs')
  13. const uuid = require('node-uuid')
  14. const path = require('path')
  15. const _ = require('underscore')
  16. const logger = require('logger-sharelatex')
  17. const metrics = require('metrics-sharelatex')
  18. const Settings = require('settings-sharelatex')
  19. const Errors = require('./Errors')
  20. module.exports = {
  21. writeStream(stream, key, callback) {
  22. const timer = new metrics.Timer('writingFile')
  23. callback = _.once(callback)
  24. const fsPath = this._getPath(key)
  25. logger.log({ fsPath }, 'writing file locally')
  26. const writeStream = fs.createWriteStream(fsPath)
  27. writeStream.on('finish', function() {
  28. timer.done()
  29. logger.log({ fsPath }, 'finished writing file locally')
  30. return callback(null, fsPath)
  31. })
  32. writeStream.on('error', function(err) {
  33. logger.err(
  34. { err, fsPath },
  35. 'problem writing file locally, with write stream'
  36. )
  37. return callback(err)
  38. })
  39. stream.on('error', function(err) {
  40. logger.log(
  41. { err, fsPath },
  42. 'problem writing file locally, with read stream'
  43. )
  44. return callback(err)
  45. })
  46. return stream.pipe(writeStream)
  47. },
  48. getStream(fsPath, _callback) {
  49. if (_callback == null) {
  50. _callback = function(err, res) {}
  51. }
  52. const callback = _.once(_callback)
  53. const timer = new metrics.Timer('readingFile')
  54. logger.log({ fsPath }, 'reading file locally')
  55. const readStream = fs.createReadStream(fsPath)
  56. readStream.on('end', function() {
  57. timer.done()
  58. return logger.log({ fsPath }, 'finished reading file locally')
  59. })
  60. readStream.on('error', function(err) {
  61. logger.err(
  62. { err, fsPath },
  63. 'problem reading file locally, with read stream'
  64. )
  65. if (err.code === 'ENOENT') {
  66. return callback(new Errors.NotFoundError(err.message), null)
  67. } else {
  68. return callback(err)
  69. }
  70. })
  71. return callback(null, readStream)
  72. },
  73. deleteFile(fsPath, callback) {
  74. if (fsPath == null || fsPath === '') {
  75. return callback()
  76. }
  77. logger.log({ fsPath }, 'removing local temp file')
  78. return fs.unlink(fsPath, callback)
  79. },
  80. _getPath(key) {
  81. if (key == null) {
  82. key = uuid.v1()
  83. }
  84. key = key.replace(/\//g, '-')
  85. return path.join(Settings.path.uploadFolder, key)
  86. }
  87. }