Получать всегда один и тот же хэш с Node.js и Multer - PullRequest
0 голосов
/ 22 мая 2019

Я пытался изменить файл multer / storage / disk.js, чтобы я мог хранить хэш каждый раз, когда загружаю файл, но факт заключается в том, что я всегда получаю один и тот же хеш, даже загружая разные файлы.

Это то, что я сделал.

DiskStorage.prototype._handleFile = function _handleFile (req, file, cb) {

  var that = this
  var hash = crypto.createHash('sha256')

  that.getDestination(req, file, function (err, destination) {
    if (err) return cb(err)

    that.getFilename(req, file, function (err, filename) {
      if (err) return cb(err)

      var finalPath = path.join(destination, filename)
      var outStream = fs.createWriteStream(finalPath)

      file.stream.pipe(outStream)
      outStream.on('error', cb)
      outStream.on('data', function (chunk) {
        hash.update(chunk)
      })

      outStream.on('finish', function () {
        cb(null, {
          destination: destination,
          filename: filename,
          path: finalPath,
          size: outStream.bytesWritten,
          hash: hash.digest('hex')

        })
      })
    })
  })
}

Как будто этот раздел ничего не делает

outStream.on('data', function (chunk) {
        hash.update(chunk)
      })

1 Ответ

0 голосов
/ 13 июня 2019

Существует обсуждение этой проблемы в Multers github: https://github.com/expressjs/multer/issues/248

microacup опубликовал сообщение о коде suhaotion (который выглядит как ваш)

DiskStorage.prototype._handleFile = function _handleFile (req, file, cb) {
...
  file.stream.on('data', function (chunk) {
      hash.update(chunk)
  })

...

вместо

DiskStorage.prototype._handleFile = function _handleFile (req, file, cb) {
...
  outStream.on('data', function (chunk) {
      hash.update(chunk)
  })

...
...