0% found this document useful (0 votes)
3 views

v2js

Uploaded by

Ravi Teja
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

v2js

Uploaded by

Ravi Teja
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

const fs = require('fs-extra');

const path = require('path');


const os = require('os');
const { Storage } = require('@google-cloud/storage');

exports.generate_thumb_data_v2 = async (file, context) => {


try {
console.log("Running v2 function...");

const gcsFile = file;


const storage = new Storage();
const sourceBucket = storage.bucket(gcsFile.bucket);
const finalBucket = storage.bucket('sp24-50100-rpillala-gj-final');

console.log(`File name: ${gcsFile.name}`);


console.log(`Content type: ${gcsFile.contentType}`);

// Check if the file exists


const [exists] = await sourceBucket.file(gcsFile.name).exists();
if (!exists) {
console.log(`File ${gcsFile.name} does not exist.`);
return;
}

console.log(`File ${gcsFile.name} exists in bucket ${sourceBucket.name}.


Continuing processing...`);

// Check content type


if (gcsFile.contentType !== 'image/jpeg' && gcsFile.contentType !==
'image/png') {
console.log('Invalid file type. Deleting from bucket.');
await sourceBucket.file(gcsFile.name).delete();
return;
}

// Create a new filename for the final image


const finalFileName = `${gcsFile.generation}.${gcsFile.contentType ===
'image/jpeg' ? 'jpg' : 'png'}`;
const tempFilePath = path.join(os.tmpdir(), finalFileName);

console.log(`Downloading file ${gcsFile.name} to ${tempFilePath}`);

// Download the original file


await sourceBucket.file(gcsFile.name).download({ destination:
tempFilePath });

console.log(`File downloaded to ${tempFilePath}`);

// Upload the file to the final bucket


await finalBucket.upload(tempFilePath, {
destination: finalFileName,
});

console.log(`File uploaded to final bucket`);

console.log("v2 function completed successfully.");


} catch (error) {
console.error("Error in v2 function:", error);
throw error;
}
};

You might also like