diff --git a/src/services/exif.js b/src/services/exif.js index d4ab296..341cf4d 100644 --- a/src/services/exif.js +++ b/src/services/exif.js @@ -5,7 +5,11 @@ const dayjs = require("dayjs"); const exiftool = new ExifTool({}); -const updateExifData = async (fileName, creationDateTimeString) => { +const updateExifData = async ( + fileName, + creationDateTimeString, + geolocationData +) => { const extension = extname(fileName); if (extension === ".mp4") { // mp4 files are not supported by exiftool @@ -14,9 +18,15 @@ const updateExifData = async (fileName, creationDateTimeString) => { const exifFormattedDate = dayjs .utc(creationDateTimeString, "YYYY-MM-DD HH:mm:ss Z") .format("YYYY:MM:DD HH:mm:ss"); + await exiftool.write(fileName, { DateTimeOriginal: exifFormattedDate, + GPSLatitude: geolocationData.latitude, + GPSLongitude: geolocationData.longitude, + GPSLatitudeRef: geolocationData.latitude > 0 ? "North" : "South", + GPSLongitudeRef: geolocationData.longitude > 0 ? "East" : "West", }); + await unlink(`${fileName}_original`); }; diff --git a/src/services/fileServices.js b/src/services/fileServices.js index 48c1daf..da4ed09 100644 --- a/src/services/fileServices.js +++ b/src/services/fileServices.js @@ -70,7 +70,16 @@ const writeFile = async (file, data) => { const updateFileMetadata = (file, memory) => { const date = new Date(memory.Date); fs.utimes(file, date, date, () => {}); - return updateExifData(file, memory.Date); + + // parse latitude and longitude 'x, y' from `Latitude, Longitude: x, y` string + const geolocationString = memory.Location.split(": ")[1]; + const [latitude, longitude] = geolocationString.split(", "); + const geolocationData = { + latitude: parseFloat(latitude), + longitude: parseFloat(longitude), + }; + + return updateExifData(file, memory.Date, geolocationData); }; const getOutputInfo = () => {