Changed the structure for the firebase functions directory

This commit is contained in:
Mariano Uvalle 2019-04-13 16:27:04 -05:00
parent 34e7756e80
commit a8dacb06cd
2 changed files with 58 additions and 53 deletions

View file

@ -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.ObjectMetadata> = 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);
});

View file

@ -1,56 +1,5 @@
import * as functions from 'firebase-functions'; 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'; export const generate_thumb: functions.CloudFunction<functions.storage.ObjectMetadata> = generateThumb;
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);
});