Simon Detheridge 6 лет назад
Родитель
Сommit
3b011258d2

+ 5 - 14
services/filestore/app/js/FSPersistor.js

@@ -1,7 +1,6 @@
 const fs = require('fs')
 const glob = require('glob')
 const path = require('path')
-const crypto = require('crypto')
 const rimraf = require('rimraf')
 const Stream = require('stream')
 const { promisify, callbackify } = require('util')
@@ -105,19 +104,6 @@ async function getFileMd5Hash(location, filename) {
   }
 }
 
-async function _getFileMd5HashForPath(fullPath) {
-  return new Promise((resolve, reject) => {
-    const readStream = fs.createReadStream(fullPath)
-    const hash = crypto.createHash('md5')
-    hash.setEncoding('hex')
-    readStream.on('end', () => {
-      hash.end()
-      resolve(hash.read())
-    })
-    pipeline(readStream, hash).catch(reject)
-  })
-}
-
 async function copyFile(location, fromName, toName) {
   const filteredFromName = filterName(fromName)
   const filteredToName = filterName(toName)
@@ -245,3 +231,8 @@ module.exports = {
     directorySize
   }
 }
+
+async function _getFileMd5HashForPath(fullPath) {
+  const stream = fs.createReadStream(fullPath)
+  return PersistorHelper.calculateStreamMd5(stream)
+}

+ 1 - 10
services/filestore/app/js/PersistorHelper.js

@@ -20,16 +20,7 @@ function calculateStreamMd5(stream) {
   const hash = crypto.createHash('md5')
   hash.setEncoding('hex')
 
-  return new Promise((resolve, reject) => {
-    pipeline(stream, hash)
-      .then(() => {
-        hash.end()
-        resolve(hash.read())
-      })
-      .catch(err => {
-        reject(err)
-      })
-  })
+  return pipeline(stream, hash).then(() => hash.read())
 }
 
 // verifies the md5 hash of a file against the supplied md5 or the one stored in