MCPcopy Create free account
hub / github.com/firebase/codelab-friendlychat-web / blurImage

Function blurImage

cloud-functions/functions/index.js:64–84  ·  view source on GitHub ↗
(filePath)

Source from the content-addressed store, hash-verified

62
63// Blurs the given image located in the given bucket using ImageMagick.
64async 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.
87exports.sendNotifications = functions.firestore.document('messages/{messageId}').onCreate(

Callers 1

index.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected