PersistorHelper.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. const crypto = require('crypto')
  2. const meter = require('stream-meter')
  3. const Stream = require('stream')
  4. const logger = require('logger-sharelatex')
  5. const { WriteError, ReadError, NotFoundError } = require('./Errors')
  6. const { promisify } = require('util')
  7. const pipeline = promisify(Stream.pipeline)
  8. module.exports = {
  9. calculateStreamMd5,
  10. verifyMd5,
  11. getMeteredStream,
  12. waitForStreamReady,
  13. wrapError
  14. }
  15. // returns a promise which resolves with the md5 hash of the stream
  16. function calculateStreamMd5(stream) {
  17. const hash = crypto.createHash('md5')
  18. hash.setEncoding('hex')
  19. return pipeline(stream, hash).then(() => hash.read())
  20. }
  21. // verifies the md5 hash of a file against the supplied md5 or the one stored in
  22. // storage if not supplied - deletes the new file if the md5 does not match and
  23. // throws an error
  24. async function verifyMd5(persistor, bucket, key, sourceMd5, destMd5 = null) {
  25. if (!destMd5) {
  26. destMd5 = await persistor.promises.getFileMd5Hash(bucket, key)
  27. }
  28. if (sourceMd5 !== destMd5) {
  29. try {
  30. await persistor.promises.deleteFile(bucket, key)
  31. } catch (err) {
  32. logger.warn(err, 'error deleting file for invalid upload')
  33. }
  34. throw new WriteError({
  35. message: 'source and destination hashes do not match',
  36. info: {
  37. sourceMd5,
  38. destMd5,
  39. bucket,
  40. key
  41. }
  42. })
  43. }
  44. }
  45. // returns the next stream in the pipeline, and calls the callback with the byte count
  46. // when the stream finishes or receives an error
  47. function getMeteredStream(stream, callback) {
  48. const meteredStream = meter()
  49. pipeline(stream, meteredStream)
  50. .then(() => {
  51. callback(null, meteredStream.bytes)
  52. })
  53. .catch(err => {
  54. // on error, just send how many bytes we received before the stream stopped
  55. callback(err, meteredStream.bytes)
  56. })
  57. return meteredStream
  58. }
  59. // resolves when a stream is 'readable', or rejects if the stream throws an error
  60. // before that happens - this lets us handle protocol-level errors before trying
  61. // to read them
  62. function waitForStreamReady(stream) {
  63. return new Promise((resolve, reject) => {
  64. const onError = function(err) {
  65. reject(wrapError(err, 'error before stream became ready', {}, ReadError))
  66. }
  67. const onStreamReady = function() {
  68. stream.removeListener('readable', onStreamReady)
  69. stream.removeListener('error', onError)
  70. resolve(stream)
  71. }
  72. stream.on('readable', onStreamReady)
  73. stream.on('error', onError)
  74. })
  75. }
  76. function wrapError(error, message, params, ErrorType) {
  77. if (
  78. error instanceof NotFoundError ||
  79. ['NoSuchKey', 'NotFound', 404, 'AccessDenied', 'ENOENT'].includes(
  80. error.code
  81. )
  82. ) {
  83. return new NotFoundError({
  84. message: 'no such file',
  85. info: params
  86. }).withCause(error)
  87. } else {
  88. return new ErrorType({
  89. message: message,
  90. info: params
  91. }).withCause(error)
  92. }
  93. }