From 40d2067733145d9e4c0755714fa6ccda453e414f Mon Sep 17 00:00:00 2001 From: Sef Kloninger Date: Thu, 19 Apr 2018 23:11:39 -0700 Subject: [PATCH 1/3] Fix exif Firebase exif function example This example has been broken since this pull request on Feb 2018: https://github.com/firebase/functions-samples/pull/306/files. Fixes "result not defined" and "metadata not defined" errors but the overall control flow was also pretty broken. --- exif-images/functions/index.js | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/exif-images/functions/index.js b/exif-images/functions/index.js index 28f1082ef8..5517fb3151 100644 --- a/exif-images/functions/index.js +++ b/exif-images/functions/index.js @@ -47,18 +47,22 @@ exports.metadata = functions.storage.object().onFinalize((object) => { const bucket = gcs.bucket(object.bucket); return bucket.file(filePath).download({destination: tempLocalFile}).then(() => { // Get Metadata from image. - return spawn('identify', ['-verbose', tempLocalFile], {capture: ['stdout', 'stderr']}); - }).then(() => { - const metadata = imageMagickOutputToObject(result.stdout); - // Save metadata to realtime datastore. - return admin.database().ref(makeKeyFirebaseCompatible(filePath)).set(metadata); - }).then(() => { - return console.log('Wrote to:', filePath, 'data:', metadata); - }).then(() => { - // Cleanup temp directory after metadata is extracted - // Remove the file from temp directory - fs.unlinkSync(tempLocalFile); - return console.log('cleanup successful!'); + return spawn('identify', ['-verbose', tempLocalFile], {capture: ['stdout', 'stderr']}) + .then((result) => { + // Save metadata to realtime datastore. + const metadata = imageMagickOutputToObject(result.stdout); + const safeKey = makeKeyFirebaseCompatible(filePath); + return admin.database().ref(safeKey).set(metadata).then(() => { + return console.log('Wrote to:', filePath, 'data:', metadata); + }) + }) + .then(() => { + // Cleanup temp directory after metadata is extracted + // Remove the file from temp directory + return fs.unlinkSync(tempLocalFile).then(() => { + return console.log('cleanup successful!'); + }); + }); }); }); From d33e35a84467e588a0946a09c1e7f940f4986984 Mon Sep 17 00:00:00 2001 From: Sef Kloninger Date: Fri, 20 Apr 2018 09:52:07 -0700 Subject: [PATCH 2/3] Avoid nested promises in exif function example. --- exif-images/functions/index.js | 38 +++++++++++++++++----------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/exif-images/functions/index.js b/exif-images/functions/index.js index 5517fb3151..92b3edb83f 100644 --- a/exif-images/functions/index.js +++ b/exif-images/functions/index.js @@ -45,25 +45,25 @@ exports.metadata = functions.storage.object().onFinalize((object) => { // Download file from bucket. const bucket = gcs.bucket(object.bucket); - return bucket.file(filePath).download({destination: tempLocalFile}).then(() => { - // Get Metadata from image. - return spawn('identify', ['-verbose', tempLocalFile], {capture: ['stdout', 'stderr']}) - .then((result) => { - // Save metadata to realtime datastore. - const metadata = imageMagickOutputToObject(result.stdout); - const safeKey = makeKeyFirebaseCompatible(filePath); - return admin.database().ref(safeKey).set(metadata).then(() => { - return console.log('Wrote to:', filePath, 'data:', metadata); - }) - }) - .then(() => { - // Cleanup temp directory after metadata is extracted - // Remove the file from temp directory - return fs.unlinkSync(tempLocalFile).then(() => { - return console.log('cleanup successful!'); - }); - }); - }); + return bucket.file(filePath).download({destination: tempLocalFile}) + .then(() => { + // Get Metadata from image. + return spawn('identify', ['-verbose', tempLocalFile], {capture: ['stdout', 'stderr']}) + }).then((result) => { + // Save metadata to realtime datastore. + const metadata = imageMagickOutputToObject(result.stdout); + const safeKey = makeKeyFirebaseCompatible(filePath); + admin.database().ref(safeKey).set(metadata); + return [filePath, metadata]; + }).then(([filePath, metadata]) => { + console.log('Wrote to:', filePath, 'data:', metadata); + }).then(() => { + // Cleanup temp directory after metadata is extracted + // Remove the file from temp directory + return fs.unlinkSync(tempLocalFile) + }).then(() => { + console.log('cleanup successful!'); + }); }); /** From 343df4d1e239bfc108265364b4d5d8c01e22ba63 Mon Sep 17 00:00:00 2001 From: Nicolas Garnier Date: Fri, 20 Apr 2018 20:39:47 +0200 Subject: [PATCH 3/3] Corrected styling and promise chaining. --- exif-images/functions/index.js | 39 +++++++++++++++++----------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/exif-images/functions/index.js b/exif-images/functions/index.js index 92b3edb83f..d48f92227a 100644 --- a/exif-images/functions/index.js +++ b/exif-images/functions/index.js @@ -43,27 +43,28 @@ exports.metadata = functions.storage.object().onFinalize((object) => { return null; } + let metadata; // Download file from bucket. const bucket = gcs.bucket(object.bucket); - return bucket.file(filePath).download({destination: tempLocalFile}) - .then(() => { - // Get Metadata from image. - return spawn('identify', ['-verbose', tempLocalFile], {capture: ['stdout', 'stderr']}) - }).then((result) => { - // Save metadata to realtime datastore. - const metadata = imageMagickOutputToObject(result.stdout); - const safeKey = makeKeyFirebaseCompatible(filePath); - admin.database().ref(safeKey).set(metadata); - return [filePath, metadata]; - }).then(([filePath, metadata]) => { - console.log('Wrote to:', filePath, 'data:', metadata); - }).then(() => { - // Cleanup temp directory after metadata is extracted - // Remove the file from temp directory - return fs.unlinkSync(tempLocalFile) - }).then(() => { - console.log('cleanup successful!'); - }); + return bucket.file(filePath).download({destination: tempLocalFile}).then(() => { + // Get Metadata from image. + return spawn('identify', ['-verbose', tempLocalFile], {capture: ['stdout', 'stderr']}) + }).then((result) => { + // Save metadata to realtime datastore. + metadata = imageMagickOutputToObject(result.stdout); + const safeKey = makeKeyFirebaseCompatible(filePath); + return admin.database().ref(safeKey).set(metadata); + }).then(() => { + console.log('Wrote to:', filePath, 'data:', metadata); + return null; + }).then(() => { + // Cleanup temp directory after metadata is extracted + // Remove the file from temp directory + return fs.unlinkSync(tempLocalFile) + }).then(() => { + console.log('cleanup successful!'); + return null; + }); }); /**