Bläddra i källkod

Move base64/hex methods to PersistorHelper

Also add some null-safety checks
Simon Detheridge 6 år sedan
förälder
incheckning
e58284aefe

+ 4 - 8
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,

+ 11 - 1
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')
+}

+ 1 - 5
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)
     }