diff --git a/src/components/import/ImportSingleFile.tsx b/src/components/import/ImportSingleFile.tsx index 78b32f4..4f71e35 100644 --- a/src/components/import/ImportSingleFile.tsx +++ b/src/components/import/ImportSingleFile.tsx @@ -20,6 +20,7 @@ import { Encoding, } from '@capacitor/filesystem'; import piexif from 'piexifjs'; +import { getDate, getGps } from '../../lib/exif'; interface Props { file: File; @@ -106,16 +107,9 @@ const ImportSingleFile: Component = ({ file: file }) => { async () => { const imageUrl = reader.result; const exifObj = piexif.load(imageUrl); - const id = ''; - const gps = {}; - const dateSegments = - exifObj.Exif[piexif.ExifIFD.DateTimeOriginal].split(' '); - const date = `${dateSegments[0].replaceAll(':', '-')}T${ - dateSegments[1] - }.${exifObj.Exif[piexif.ExifIFD.SubSecTimeOriginal] || '0'}${ - exifObj.Exif[piexif.ExifIFD.OffsetTimeOriginal] - }`; - // const id = exif?.ImageUniqueID?.value[0]; + const id = exifObj.Exif[piexif.ExifIFD.ImageUniqueID]; + const gps = getGps(exifObj); + const date = getDate(exifObj); // const gps = tags.gps; // const image = await Image.load(reader.result); // const thumbnail = image.resize({ diff --git a/src/lib/exif.ts b/src/lib/exif.ts new file mode 100644 index 0000000..50e0519 --- /dev/null +++ b/src/lib/exif.ts @@ -0,0 +1,26 @@ +import piexif from 'piexifjs'; + +export const getDate = (exifObj: any) => { + const dateSegments = exifObj.Exif[piexif.ExifIFD.DateTimeOriginal].split(' '); + const date = `${dateSegments[0].replaceAll(':', '-')}T${dateSegments[1]}.${ + exifObj.Exif[piexif.ExifIFD.SubSecTimeOriginal] || '0' + }${exifObj.Exif[piexif.ExifIFD.OffsetTimeOriginal]}`; + return new Date(date); +}; + +export const getGps = (exifObj: any) => { + const toNumber = (start: number) => { + const sign = ['N', 'E'].includes(exifObj.GPS[start]) ? 1 : -1; + const value = exifObj.GPS[start + 1]; + return ( + sign * + (value[0][0] / value[0][1] + + (value[1][0] / value[1][1] + value[2][0] / value[2][1] / 60) / 60) + ); + }; + return { + latitude: toNumber(1), + longitude: toNumber(3), + elevation: exifObj.GPS[6][0] / exifObj.GPS[6][1], + }; +};