1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-12-22 09:13:35 +01:00

fix failing exif extraction for some photos

This commit is contained in:
Matthias Kretschmann 2019-10-04 16:31:20 +02:00
parent df1438e6b3
commit cc15aa149b
Signed by: m
GPG Key ID: 606EEEF3C479A91F
2 changed files with 55 additions and 38 deletions

View File

@ -2,40 +2,22 @@ const fastExif = require('fast-exif')
const Fraction = require('fraction.js')
const dms2dec = require('dms2dec')
exports.createExifFields = (node, createNodeField) => {
fastExif
.read(node.absolutePath, true)
.then(exifData => {
if (!exifData) return
constructExifFields(exifData, createNodeField, node)
})
.catch(() => null) // just silently fail when exif can't be extracted
exports.createExifFields = async (node, createNodeField) => {
let exifData
try {
exifData = await fastExif.read(node.absolutePath, true)
if (!exifData) return
constructExifFields(exifData, createNodeField, node)
} catch (error) {
// console.error(`${node.name}: ${error.message}`)
return null // just silently fail when exif can't be extracted
}
}
const constructExifFields = (exifData, createNodeField, node) => {
const { Model } = exifData.image
const {
ISO,
FNumber,
ExposureTime,
FocalLength,
ExposureBiasValue
} = exifData.exif
const {
GPSLatitudeRef,
GPSLatitude,
GPSLongitudeRef,
GPSLongitude
} = exifData.gps
const getGps = gpsData => {
if (!gpsData) return
const { n, d } = new Fraction(ExposureTime)
const exposureShortened = parseFloat(ExposureBiasValue.toFixed(2))
const model = `${Model}`
const iso = `ISO ${ISO}`
const fstop = `ƒ ${FNumber}`
const shutterspeed = `${n}/${d}s`
const focalLength = `${FocalLength}mm`
const { GPSLatitudeRef, GPSLatitude, GPSLongitudeRef, GPSLongitude } = gpsData
const GPSdec = dms2dec(
GPSLatitude,
@ -47,16 +29,52 @@ const constructExifFields = (exifData, createNodeField, node) => {
const latitude = GPSdec[0]
const longitude = GPSdec[1]
return { latitude, longitude }
}
const getExposure = exposureMode => {
const exposureShortened = parseFloat(exposureMode.toFixed(2))
let exposure
if (ExposureBiasValue === 0) {
if (exposureMode === 0) {
exposure = `+/- ${exposureShortened} ev`
} else if (ExposureBiasValue > 0) {
} else if (exposureMode > 0) {
exposure = `+ ${exposureShortened} ev`
} else {
exposure = `${exposureShortened} ev`
}
return exposure
}
const constructExifFields = (exifData, createNodeField, node) => {
const { Model } = exifData.image
const {
ISO,
FNumber,
ExposureTime,
FocalLength,
ExposureBiasValue,
ExposureMode
} = exifData.exif
const model = `${Model}`
const iso = `ISO ${ISO}`
const fstop = `ƒ ${FNumber}`
const focalLength = `${FocalLength}mm`
// Shutter speed
const { n, d } = new Fraction(ExposureTime)
const shutterspeed = `${n}/${d}s`
// GPS
let latitude
let longitude
if (exifData.gps) ({ latitude, longitude } = getGps(exifData.gps))
// Exposure
const exposure = getExposure(ExposureBiasValue || ExposureMode)
// add exif fields to type File
createNodeField({
node,
@ -68,10 +86,7 @@ const constructExifFields = (exifData, createNodeField, node) => {
shutterspeed,
focalLength,
exposure,
gps: {
latitude,
longitude
}
gps: { latitude, longitude }
}
})
}

View File

@ -28,7 +28,9 @@ export default function Exif({ exif }: { exif: ExifProps }) {
{iso && <span title="ISO">{iso}</span>}
{focalLength && <span title="Focal length">{focalLength}</span>}
</div>
<div className={styles.map}>{gps && <ExifMap gps={gps} />}</div>
{gps.latitude && (
<div className={styles.map}>{gps && <ExifMap gps={gps} />}</div>
)}
</aside>
)
}