Created
February 3, 2019 19:22
-
-
Save gantrim/6aa480162818e0a9f6108e7d3513d73a to your computer and use it in GitHub Desktop.
Firebase Part 6 - Blur Image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function blurImage(object, userDocRef) { | |
const filePath = object.name; | |
const bucket = gcs.bucket(object.bucket); | |
const fileName = filePath.split('/').pop(); | |
const tempLocalFile = `/tmp/${fileName}`; | |
// Download file from bucket. | |
return bucket.file(filePath) | |
.download({ | |
destination: tempLocalFile | |
}) | |
.then(() => { | |
console.log('Image has been downloaded to', tempLocalFile); | |
// Blur the image using ImageMagick. | |
return exec(`convert ${tempLocalFile} -channel RGBA -blur 0x24 ${tempLocalFile}`); | |
}) | |
.then(() => { | |
console.log('Image has been blurred'); | |
// Uploading the Blurred image back into the bucket. | |
return bucket.upload(tempLocalFile, { | |
destination: filePath | |
}); | |
}) | |
.then(() => { | |
console.log('Blurred image has been uploaded to', filePath); | |
// Indicate that the message has been moderated. | |
return userDocRef.update({ | |
locked: true | |
}); | |
}) | |
.then(() => { | |
console.log('Marked the image as moderated in the database.'); | |
return fs.unlinkSync(tempLocalFile); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment