From a8dacb06cd2be95c0e3ab037242464214f60973d Mon Sep 17 00:00:00 2001 From: AYM1607 Date: Sat, 13 Apr 2019 16:27:04 -0500 Subject: [PATCH] Changed the structure for the firebase functions directory --- functions/src/generate_thumb.ts | 56 +++++++++++++++++++++++++++++++++ functions/src/index.ts | 55 ++------------------------------ 2 files changed, 58 insertions(+), 53 deletions(-) create mode 100644 functions/src/generate_thumb.ts diff --git a/functions/src/generate_thumb.ts b/functions/src/generate_thumb.ts new file mode 100644 index 0000000..57c7768 --- /dev/null +++ b/functions/src/generate_thumb.ts @@ -0,0 +1,56 @@ +import * as functions from 'firebase-functions'; +import { Storage } from '@google-cloud/storage'; + +const gcs = new Storage(); + +import { tmpdir } from 'os'; +import { join, dirname } from 'path'; + +import * as sharp from 'sharp'; +import * as fs from 'fs-extra'; + +export const generateThumb: functions.CloudFunction = functions.storage.object().onFinalize(async object => { + // Find the bucket where the uploaded file resides. + const bucket = gcs.bucket(object.bucket); + + // Find the path of the file inside the bucket. + const filePathGcs = object.name; + + // Save the name of the file. + const fileName = filePathGcs!.split('/').pop(); + + // Directory where the file is stored inside the bucket. + const bucketDirectory = dirname(filePathGcs!); + const workingDirectory = join(tmpdir(), 'thumbnails'); + const tmpFilePath = join(workingDirectory, fileName!); + + if (fileName!.includes('thumb@') || !object.contentType!.includes('image')) { + console.log('Exiting function, already compressed or no image.'); + return false; + } + + // Ensure that our directory exists. + await fs.ensureDir(workingDirectory); + + // Download the source file to the temporary directory. + await bucket.file(filePathGcs!).download({ + destination: tmpFilePath, + }); + + // Create the thumnail in 124 size. + const thumbnailName = `thumb@${fileName}`; + const thumbnailLocalPath = join(workingDirectory, thumbnailName); + + await sharp(tmpFilePath) + .resize(256, 256) + .toFile(thumbnailLocalPath); + + // Upload the resulting thumnail to the same directory as the original file. + await bucket.upload(thumbnailLocalPath, { + destination: join(bucketDirectory, thumbnailName), + }); + console.log('Thumbnail created successfully') + + // Exit the function deleting all the temprorary files. + return fs.remove(workingDirectory); +}); \ No newline at end of file diff --git a/functions/src/index.ts b/functions/src/index.ts index 56a5855..9675750 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -1,56 +1,5 @@ import * as functions from 'firebase-functions'; -import { Storage } from '@google-cloud/storage'; -const gcs = new Storage(); +import { generateThumb } from './generate_thumb'; -import { tmpdir } from 'os'; -import { join, dirname } from 'path'; - -import * as sharp from 'sharp'; -import * as fs from 'fs-extra'; - -export const generateThumb = functions.storage.object().onFinalize(async object => { - // Find the bucket where the uploaded file resides. - const bucket = gcs.bucket(object.bucket); - - // Find the path of the file inside the bucket. - const filePathGcs = object.name; - - // Save the name of the file. - const fileName = filePathGcs!.split('/').pop(); - - // Directory where the file is stored inside the bucket. - const bucketDirectory = dirname(filePathGcs!); - const workingDirectory = join(tmpdir(), 'thumbnails'); - const tmpFilePath = join(workingDirectory, fileName!); - - if (fileName!.includes('thumb@') || !object.contentType!.includes('image')) { - console.log('Exiting function, already compressed or no image.'); - return false; - } - - // Ensure that our directory exists. - await fs.ensureDir(workingDirectory); - - // Download the source file to the temporary directory. - await bucket.file(filePathGcs!).download({ - destination: tmpFilePath, - }); - - // Create the thumnail in 124 size. - const thumbnailName = `thumb@${fileName}`; - const thumbnailLocalPath = join(workingDirectory, thumbnailName); - - await sharp(tmpFilePath) - .resize(256, 256) - .toFile(thumbnailLocalPath); - - // Upload the resulting thumnail to the same directory as the original file. - await bucket.upload(thumbnailLocalPath, { - destination: join(bucketDirectory, thumbnailName), - }); - console.log('Thumbnail created successfully') - - // Exit the function deleting all the temprorary files. - return fs.remove(workingDirectory); -}); \ No newline at end of file +export const generate_thumb: functions.CloudFunction = generateThumb; \ No newline at end of file