0% found this document useful (0 votes)
2 views2 pages

V 3 Js

Uploaded by

Ravi Teja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

V 3 Js

Uploaded by

Ravi Teja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
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');
const sharp = require('sharp');
const getExif = require('exif-async');
const parseDMS = require('parse-dms');

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


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

const gcsFile = file;


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

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


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

const workingDir = path.join(os.tmpdir(), 'thumbnails');


await fs.ensureDir(workingDir);

// Extracting the new filename generated by v2 function


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

console.log(`Downloading file ${gcsFile.name} from final bucket to $


{tempFilePath}`);

await finalBucket.file(finalFileName).download({ destination:


tempFilePath });

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

// Read EXIF data from the downloaded image


const exifData = await getExif(tempFilePath);

if (exifData && exifData.gps) {


// Convert GPS coordinates to decimal format
const gpsDecimal = getGPSCoordinates(exifData.gps);
console.log("Latitude:", gpsDecimal.lat);
console.log("Longitude:", gpsDecimal.lon);

// Return the EXIF data


return {
latitude: gpsDecimal.lat,
longitude: gpsDecimal.lon
};
} else {
console.log("No EXIF data found or GPS data missing.");
return null;
}

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


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

// Helper function to convert GPS coordinates to decimal format


function getGPSCoordinates(g) {
const latstring = `${g.GPSLatitude[0]}:${g.GPSLatitude[1]}:${g.GPSLatitude[2]}$
{g.GPSLatitudeRef}`;
const lonstring = `${g.GPSLongitude[0]}:${g.GPSLongitude[1]}:$
{g.GPSLongitude[2]}${g.GPSLongitudeRef}`;

const degCoords = parseDMS(`${latstring} ${lonstring}`);

return degCoords;
}

You might also like