From e58284aefeec7ded2d00764e7f0a0f434fc4a0ad Mon Sep 17 00:00:00 2001 From: Simon Detheridge Date: Thu, 13 Feb 2020 16:55:01 +0000 Subject: [PATCH] Move base64/hex methods to PersistorHelper Also add some null-safety checks --- services/filestore/app/js/GcsPersistor.js | 12 ++++-------- services/filestore/app/js/PersistorHelper.js | 12 +++++++++++- services/filestore/app/js/S3Persistor.js | 6 +----- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/services/filestore/app/js/GcsPersistor.js b/services/filestore/app/js/GcsPersistor.js index 350b0d451c..690df70252 100644 --- a/services/filestore/app/js/GcsPersistor.js +++ b/services/filestore/app/js/GcsPersistor.js @@ -10,16 +10,12 @@ const PersistorHelper = require('./PersistorHelper') const pipeline = promisify(Stream.pipeline) -function base64ToHex(base64) { - return Buffer.from(base64, 'base64').toString('hex') -} - // both of these settings will be null by default except for tests // that's OK - GCS uses the locally-configured service account by default const storage = new Storage(settings.filestore.gcs) // workaround for broken uploads with custom endpoints: // https://github.com/googleapis/nodejs-storage/issues/898 -if (settings.filestore.gcs.apiEndpoint) { +if (settings.filestore.gcs && settings.filestore.gcs.apiEndpoint) { storage.interceptors.push({ request: function(reqOpts) { const url = new URL(reqOpts.uri) @@ -95,7 +91,7 @@ async function sendStream(bucket, key, readStream, sourceMd5) { if (sourceMd5) { writeOptions.validation = 'md5' writeOptions.metadata = { - md5Hash: sourceMd5 + md5Hash: PersistorHelper.hexToBase64(sourceMd5) } } @@ -123,7 +119,7 @@ async function sendStream(bucket, key, readStream, sourceMd5) { } } -async function getFileStream(bucket, key, opts) { +async function getFileStream(bucket, key, opts = {}) { if (opts.end) { // S3 (and http range headers) treat 'end' as inclusive, so increase this by 1 opts.end++ @@ -174,7 +170,7 @@ async function getFileMd5Hash(bucket, key) { .bucket(bucket) .file(key) .getMetadata() - return base64ToHex(metadata[0].md5Hash) + return PersistorHelper.base64ToHex(metadata[0].md5Hash) } catch (err) { throw PersistorHelper.wrapError( err, diff --git a/services/filestore/app/js/PersistorHelper.js b/services/filestore/app/js/PersistorHelper.js index ea8132a9c9..409f3182f1 100644 --- a/services/filestore/app/js/PersistorHelper.js +++ b/services/filestore/app/js/PersistorHelper.js @@ -12,7 +12,9 @@ module.exports = { verifyMd5, getMeteredStream, waitForStreamReady, - wrapError + wrapError, + hexToBase64, + base64ToHex } // returns a promise which resolves with the md5 hash of the stream @@ -103,3 +105,11 @@ function wrapError(error, message, params, ErrorType) { }).withCause(error) } } + +function base64ToHex(base64) { + return Buffer.from(base64, 'base64').toString('hex') +} + +function hexToBase64(hex) { + return Buffer.from(hex, 'hex').toString('base64') +} diff --git a/services/filestore/app/js/S3Persistor.js b/services/filestore/app/js/S3Persistor.js index fc505ccfbb..7b69ad5d26 100644 --- a/services/filestore/app/js/S3Persistor.js +++ b/services/filestore/app/js/S3Persistor.js @@ -46,10 +46,6 @@ const S3Persistor = { module.exports = S3Persistor -function hexToBase64(hex) { - return Buffer.from(hex, 'hex').toString('base64') -} - async function sendFile(bucketName, key, fsPath) { let readStream try { @@ -72,7 +68,7 @@ async function sendStream(bucketName, key, readStream, sourceMd5) { let b64Hash if (sourceMd5) { - b64Hash = hexToBase64(sourceMd5) + b64Hash = PersistorHelper.hexToBase64(sourceMd5) } else { hashPromise = PersistorHelper.calculateStreamMd5(readStream) }