| 62 | |
| 63 | // Blurs the given image located in the given bucket using ImageMagick. |
| 64 | async function blurImage(filePath) { |
| 65 | const tempLocalFile = path.join(os.tmpdir(), path.basename(filePath)); |
| 66 | const messageId = filePath.split(path.sep)[1]; |
| 67 | const bucket = admin.storage().bucket(); |
| 68 | |
| 69 | // Download file from bucket. |
| 70 | await bucket.file(filePath).download({destination: tempLocalFile}); |
| 71 | functions.logger.log('Image has been downloaded to', tempLocalFile); |
| 72 | // Blur the image using ImageMagick. |
| 73 | await exec(`convert "${tempLocalFile}" -channel RGBA -blur 0x24 "${tempLocalFile}"`); |
| 74 | functions.logger.log('Image has been blurred'); |
| 75 | // Uploading the Blurred image back into the bucket. |
| 76 | await bucket.upload(tempLocalFile, {destination: filePath}); |
| 77 | functions.logger.log('Blurred image has been uploaded to', filePath); |
| 78 | // Deleting the local file to free up disk space. |
| 79 | fs.unlinkSync(tempLocalFile); |
| 80 | functions.logger.log('Deleted local file.'); |
| 81 | // Indicate that the message has been moderated. |
| 82 | await admin.firestore().collection('messages').doc(messageId).update({moderated: true}); |
| 83 | functions.logger.log('Marked the image as moderated in the database.'); |
| 84 | } |
| 85 | |
| 86 | // Sends a notifications to all users when a new message is posted. |
| 87 | exports.sendNotifications = functions.firestore.document('messages/{messageId}').onCreate( |