From 3b011258d207102620d0388d8f0098c77062648c Mon Sep 17 00:00:00 2001 From: Simon Detheridge Date: Wed, 12 Feb 2020 12:02:06 +0000 Subject: [PATCH] Tidy up md5 hash generation --- services/filestore/app/js/FSPersistor.js | 19 +++++-------------- services/filestore/app/js/PersistorHelper.js | 11 +---------- 2 files changed, 6 insertions(+), 24 deletions(-) diff --git a/services/filestore/app/js/FSPersistor.js b/services/filestore/app/js/FSPersistor.js index a5b1a35c8c..973c670efd 100644 --- a/services/filestore/app/js/FSPersistor.js +++ b/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) +} diff --git a/services/filestore/app/js/PersistorHelper.js b/services/filestore/app/js/PersistorHelper.js index d8beb4a0a9..ea8132a9c9 100644 --- a/services/filestore/app/js/PersistorHelper.js +++ b/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