Last active
April 2, 2026 01:36
-
-
Save nov05/154a3e08319fb52266ec6c35bf7cfb1e to your computer and use it in GitHub Desktop.
index.js
This file contains hidden or 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
| const functions = require('@google-cloud/functions-framework'); | |
| const { Storage } = require('@google-cloud/storage'); | |
| const { PubSub } = require('@google-cloud/pubsub'); | |
| const sharp = require('sharp'); | |
| // Read FUNCTION_NAME from env variable | |
| const FUNCTION_NAME = process.env.FUNCTION_NAME; | |
| // functions.cloudEvent('memories-thumbnail-creator', async cloudEvent => { | |
| functions.cloudEvent(FUNCTION_NAME, async cloudEvent => { | |
| const event = cloudEvent.data; | |
| console.log(`Event: ${JSON.stringify(event)}`); | |
| console.log(`Hello ${event.bucket}`); | |
| const fileName = event.name; | |
| const bucketName = event.bucket; | |
| const size = "64x64"; | |
| const bucket = new Storage().bucket(bucketName); | |
| // const topicName = "topic-memories-993"; | |
| const topicName = process.env.TOPIC_NAME; | |
| const pubsub = new PubSub(); | |
| if (fileName.search("64x64_thumbnail") === -1) { | |
| // doesn't have a thumbnail, get the filename extension | |
| const filename_split = fileName.split('.'); | |
| const filename_ext = filename_split[filename_split.length - 1].toLowerCase(); | |
| const filename_without_ext = fileName.substring(0, fileName.length - filename_ext.length - 1); // fix sub string to remove the dot | |
| if (filename_ext === 'png' || filename_ext === 'jpg' || filename_ext === 'jpeg') { | |
| // only support png and jpg at this point | |
| console.log(`Processing Original: gs://${bucketName}/${fileName}`); | |
| const gcsObject = bucket.file(fileName); | |
| const newFilename = `${filename_without_ext}_64x64_thumbnail.${filename_ext}`; | |
| const gcsNewObject = bucket.file(newFilename); | |
| try { | |
| const [buffer] = await gcsObject.download(); | |
| const resizedBuffer = await sharp(buffer) | |
| .resize(64, 64, { | |
| fit: 'inside', | |
| withoutEnlargement: true, | |
| }) | |
| .toFormat(filename_ext) | |
| .toBuffer(); | |
| await gcsNewObject.save(resizedBuffer, { | |
| metadata: { | |
| contentType: `image/${filename_ext}`, | |
| }, | |
| }); | |
| console.log(`Success: ${fileName} → ${newFilename}`); | |
| await pubsub | |
| .topic(topicName) | |
| .publishMessage({ data: Buffer.from(newFilename) }); | |
| console.log(`Message published to ${topicName}`); | |
| } catch (err) { | |
| console.error(`Error: ${err}`); | |
| } | |
| } else { | |
| console.log(`gs://${bucketName}/${fileName} is not an image I can handle`); | |
| } | |
| } else { | |
| console.log(`gs://${bucketName}/${fileName} already has a thumbnail`); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment