|
| 1 | +/** |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for t`he specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +'use strict'; |
| 17 | + |
| 18 | +// [START import] |
| 19 | +const functions = require('firebase-functions'); |
| 20 | +const gcs = require('@google-cloud/storage')(); |
| 21 | +const sharp = require('sharp'); |
| 22 | +// [END import] |
| 23 | + |
| 24 | +const THUMB_MAX_WIDTH = 200; |
| 25 | +const THUMB_MAX_HEIGHT = 200; |
| 26 | + |
| 27 | +// [START generateThumbnail] |
| 28 | +/** |
| 29 | + * When an image is uploaded in the Storage bucket We generate a thumbnail automatically using |
| 30 | + * ImageMagick. |
| 31 | + */ |
| 32 | +// [START generateThumbnailTrigger] |
| 33 | +exports.generateThumbnail = functions.storage.object().onChange(event => { |
| 34 | +// [END generateThumbnailTrigger] |
| 35 | + // [START eventAttributes] |
| 36 | + const object = event.data; // The Storage object. |
| 37 | + |
| 38 | + const fileBucket = object.bucket; // The Storage bucket that contains the file. |
| 39 | + const filePath = object.name; // File path in the bucket. |
| 40 | + const contentType = object.contentType; // File content type. |
| 41 | + const resourceState = object.resourceState; // The resourceState is 'exists' or 'not_exists' (for file/folder deletions). |
| 42 | + const metageneration = object.metageneration; // Number of times metadata has been generated. New objects have a value of 1. |
| 43 | + // [END eventAttributes] |
| 44 | + |
| 45 | + // [START stopConditions] |
| 46 | + // Exit if this is triggered on a file that is not an image. |
| 47 | + if (!contentType.startsWith('image/')) { |
| 48 | + console.log('This is not an image.'); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + // Get the file name. |
| 53 | + const fileName = filePath.split('/').pop(); |
| 54 | + // Exit if the image is already a thumbnail. |
| 55 | + if (fileName.startsWith('thumb_')) { |
| 56 | + console.log('Already a Thumbnail.'); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + // Exit if this is a move or deletion event. |
| 61 | + if (resourceState === 'not_exists') { |
| 62 | + console.log('This is a deletion event.'); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + // Exit if file exists but is not new and is only being triggered |
| 67 | + // because of a metadata change. |
| 68 | + if (resourceState === 'exists' && metageneration > 1) { |
| 69 | + console.log('This is a metadata change event.'); |
| 70 | + return; |
| 71 | + } |
| 72 | + // [END stopConditions] |
| 73 | + |
| 74 | + // [START thumbnailGeneration] |
| 75 | + // Download file from bucket. |
| 76 | + const bucket = gcs.bucket(fileBucket); |
| 77 | + |
| 78 | + const metadata = { |
| 79 | + contentType: contentType |
| 80 | + }; |
| 81 | + // We add a 'thumb_' prefix to thumbnails file name. That's where we'll upload the thumbnail |
| 82 | + const thumbFilePath = filePath.replace(/(\/)?([^\/]*)$/, '$1thumb_$2'); |
| 83 | + // Create write stream for uploading thumbnail |
| 84 | + const thumbnailUploadStream = bucket.file(thumbFilePath).createWriteStream({metadata}); |
| 85 | + |
| 86 | + // Create Sharp pipeline for resizing the image and use pipe to read from bucket read stream |
| 87 | + const pipeline = sharp(); |
| 88 | + pipeline |
| 89 | + .resize(THUMB_MAX_WIDTH, THUMB_MAX_HEIGHT) |
| 90 | + .max() |
| 91 | + .pipe(thumbnailUploadStream); |
| 92 | + |
| 93 | + bucket.file(filePath).createReadStream().pipe(pipeline); |
| 94 | + |
| 95 | + const streamAsPromise = new Promise((resolve, reject) => |
| 96 | + thumbnailUploadStream.on('finish', resolve).on('error', reject)); |
| 97 | + return streamAsPromise.then(() => { |
| 98 | + console.log('Thumbnail created successfully'); |
| 99 | + }); |
| 100 | + // [END thumbnailGeneration] |
| 101 | +}); |
| 102 | +// [END generateThumbnail] |
0 commit comments