Skip to content

Instantly share code, notes, and snippets.

@azzeddineCH
Created July 17, 2017 15:13
Show Gist options
  • Save azzeddineCH/9408a9de9a37ce84f5cacdc8b3674098 to your computer and use it in GitHub Desktop.
Save azzeddineCH/9408a9de9a37ce84f5cacdc8b3674098 to your computer and use it in GitHub Desktop.
exports.generateProfileThumbnail = functions.storage.object().onChange(event => {
const data = event.data;
const filePath = data.name;
const fileName = filePath.split("/").pop();
const tmpFilePath = "/tmp/" + fileName;
const thumbFilePath = "/users/profileImages/thumb_" + fileName;
const metageneration = data.metageneration;
const resourceState = data.resourceState;
const fileBucket = data.bucket;
const bucket = gcs.bucket(fileBucket);
console.log("downloading the image to " + tmpFilePath);
/**
* if the image is already resize stop the process
*/
if (fileName.startsWith('thumb_')) {
console.log("the thumbnail is already created");
return
}
/**
* downloading the image to a temp folder at
* the google cloud storage bucket
*/
return bucket.file(filePath).download({
destination: tmpFilePath
}).then(() => {
console.log("resizing the image");
return spawn('convert', [tmpFilePath, '-thumbnail', '200x200>', tmpFilePath])
}).then(() => {
console.log("uploading the thumb to " + thumbFilePath);
/**
* uploading the thumbnail from the google cloud storage to
* the firebase storage
*/
return bucket.upload(tmpFilePath, {
destination: thumbFilePath
});
}).then(() => {
/**
* getting the signed url from the google cloud storage bucker
*/
const thumbFile = bucket.file(thumbFilePath);
const config = {
action: "read",
expires: "12-12-2222"
}
return thumbFile.getSignedUrl(config)
}).then(photoURL => {
/**
* updating the user profile image in the firebase
* realtime database
*/
console.log("updating the user profile picture" + photoURL);
const uid = fileName.split(".")[0];
var updates = {};
updates["/users/" + uid + "/photoURL"] = photoURL[0];
return admin.database().ref().update(updates)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment