exif() returns every EXIF tag in the file (closes #156)
check / check (push) Failing after 59s
check / check (push) Failing after 59s
`photo.exif()` now returns every EXIF tag in the file, as the owner ruled, typed `ExifTags`: each tag keyed by name with `exifreader`'s `id`, `value`, `description` and `computed`. A tag with no name is keyed `undefined-` plus its number, and the embedded thumbnail's tags sit under `Thumbnail`. The thirteen typed methods stay, with the same names and types; each now picks its field from `exif()`'s tags. GPS latitude and longitude are worked out from the GPS tags and their reference tags, and a position with no reference tags gives neither. `backup-metadata --exif` output is unchanged. Model: opus-5-5 Co-authored-by: clawbot <sneak+clawbot@sneak.cloud>
This commit was merged in pull request #159.
This commit is contained in:
+80
-31
@@ -1,19 +1,19 @@
|
||||
// EXIF in an original's bytes, read with exifreader, which reads it from JPEG,
|
||||
// HEIC/HEIF, AVIF, PNG, WebP and the other image formats it supports.
|
||||
// `backup-metadata --exif` records every EXIF tag it finds except the
|
||||
// thumbnail's; `Photo.exif()` returns the common fields picked from them here.
|
||||
// thumbnail's. `Photo.exif()` returns every tag, the thumbnail's included, and
|
||||
// `Photo`'s typed methods return the common fields picked from them here.
|
||||
|
||||
import ExifReader, { type ExpandedTags } from "exifreader";
|
||||
|
||||
// The EXIF tags in `bytes` (`exif`), the GPS position exifreader computes from
|
||||
// them (`gps`), and where the EXIF block lies in `bytes` (`metadataRange`).
|
||||
// The EXIF tags in `bytes` (`exif`), the embedded thumbnail's tags
|
||||
// (`Thumbnail`), and where the EXIF block lies in `bytes` (`metadataRange`).
|
||||
// Undefined when exifreader cannot read the file at all, such as a video. An
|
||||
// EXIF block it finds but reads no tag from comes back as an empty `exif`.
|
||||
// `exif` holds every tag except the thumbnail's; a tag exifreader has no name
|
||||
// for is keyed `undefined-<tag number>`. Each tag's `computed` holds its value
|
||||
// as a string or number, or as an array of them for a tag with several values,
|
||||
// such as `GPSLatitude`'s `[40, 26, 46]`. A fraction with a zero denominator
|
||||
// computes to null.
|
||||
// EXIF block it finds but reads no tag from comes back as an empty `exif`. A
|
||||
// tag exifreader has no name for is keyed `undefined-<tag number>`. Each tag's
|
||||
// `computed` holds its value as a string or number, or as an array of them for
|
||||
// a tag with several values, such as `GPSLatitude`'s `[40, 26, 46]`. A
|
||||
// fraction with a zero denominator computes to null.
|
||||
export const readExifTags = (bytes: Uint8Array): ExpandedTags | undefined => {
|
||||
try {
|
||||
return ExifReader.loadView(
|
||||
@@ -23,7 +23,7 @@ export const readExifTags = (bytes: Uint8Array): ExpandedTags | undefined => {
|
||||
computed: true,
|
||||
includeOffsets: true,
|
||||
includeUnknown: true,
|
||||
includeTags: { exif: true, gps: true },
|
||||
includeTags: { exif: true, thumbnail: true },
|
||||
},
|
||||
);
|
||||
} catch {
|
||||
@@ -31,7 +31,35 @@ export const readExifTags = (bytes: Uint8Array): ExpandedTags | undefined => {
|
||||
}
|
||||
};
|
||||
|
||||
// The common EXIF fields of an original. Each is absent when the file lacks it.
|
||||
// Every EXIF tag of an original, keyed by name, each as exifreader decodes it
|
||||
// (see `readExifTags`). The embedded thumbnail's own tags are under
|
||||
// `Thumbnail`, so its `Orientation` or `ImageWidth` cannot hide the main
|
||||
// image's.
|
||||
export type ExifTags = Omit<NonNullable<ExpandedTags["exif"]>, "Thumbnail"> & {
|
||||
Thumbnail?: Omit<
|
||||
NonNullable<ExpandedTags["Thumbnail"]>,
|
||||
"type" | "image" | "base64"
|
||||
>;
|
||||
};
|
||||
|
||||
// Every EXIF tag in `bytes`: `{}` when the file has no EXIF, exifreader cannot
|
||||
// read its EXIF, or it is not an image exifreader reads.
|
||||
export const readAllExifTags = (bytes: Uint8Array): ExifTags => {
|
||||
const tags = readExifTags(bytes);
|
||||
if (!tags?.Thumbnail) return tags?.exif ?? {};
|
||||
// exifreader puts the thumbnail's JPEG image beside its tags, as `type`,
|
||||
// `image` and `base64`. The image is not a tag, so it is left out.
|
||||
const {
|
||||
type: _type,
|
||||
image: _image,
|
||||
base64: _base64,
|
||||
...thumbnail
|
||||
} = tags.Thumbnail;
|
||||
return { ...tags.exif, Thumbnail: thumbnail };
|
||||
};
|
||||
|
||||
// The common EXIF fields of an original, one for each of `Photo`'s typed
|
||||
// methods. Each is absent when the file lacks it.
|
||||
export interface PhotoExif {
|
||||
make?: string;
|
||||
model?: string;
|
||||
@@ -80,30 +108,51 @@ const asDate = (v: unknown): Date | undefined => {
|
||||
return Number.isNaN(date.getTime()) ? undefined : date;
|
||||
};
|
||||
|
||||
// The common fields of an original's EXIF: `{}` when the file has no EXIF,
|
||||
// exifreader cannot read its EXIF, or it is not an image exifreader reads.
|
||||
export const readPhotoExif = (bytes: Uint8Array): PhotoExif => {
|
||||
const tags = readExifTags(bytes);
|
||||
const exif = tags?.exif;
|
||||
const gps = tags?.gps;
|
||||
const altitude = asNumber(exif?.GPSAltitude?.computed);
|
||||
// GPSLatitude and GPSLongitude hold degrees, minutes and seconds, computed as
|
||||
// three numbers. This is them in decimal degrees, negative when `ref`, the
|
||||
// GPSLatitudeRef or GPSLongitudeRef tag, is `negativeRef` ("S" or "W").
|
||||
// Without that tag the hemisphere is unknown, so it is undefined.
|
||||
const asDegrees = (
|
||||
dms: unknown,
|
||||
ref: unknown,
|
||||
negativeRef: string,
|
||||
): number | undefined => {
|
||||
if (!Array.isArray(dms) || ref === undefined) return undefined;
|
||||
const [d, m, s] = dms.map(asNumber);
|
||||
if (d === undefined || m === undefined || s === undefined) return undefined;
|
||||
const degrees = d + m / 60 + s / 3600;
|
||||
return ref === negativeRef ? -degrees : degrees;
|
||||
};
|
||||
|
||||
// The common fields picked from an original's EXIF tags, `readAllExifTags`'s
|
||||
// result: `{}` when there are none.
|
||||
export const readPhotoExif = (tags: ExifTags): PhotoExif => {
|
||||
const altitude = asNumber(tags.GPSAltitude?.computed);
|
||||
const fields: PhotoExif = {
|
||||
make: asString(exif?.Make?.computed),
|
||||
model: asString(exif?.Model?.computed),
|
||||
lensModel: asString(exif?.LensModel?.computed),
|
||||
dateTimeOriginal: asDate(exif?.DateTimeOriginal?.computed),
|
||||
offsetTimeOriginal: asString(exif?.OffsetTimeOriginal?.computed),
|
||||
exposureTime: asNumber(exif?.ExposureTime?.computed),
|
||||
fNumber: asNumber(exif?.FNumber?.computed),
|
||||
make: asString(tags.Make?.computed),
|
||||
model: asString(tags.Model?.computed),
|
||||
lensModel: asString(tags.LensModel?.computed),
|
||||
dateTimeOriginal: asDate(tags.DateTimeOriginal?.computed),
|
||||
offsetTimeOriginal: asString(tags.OffsetTimeOriginal?.computed),
|
||||
exposureTime: asNumber(tags.ExposureTime?.computed),
|
||||
fNumber: asNumber(tags.FNumber?.computed),
|
||||
// Only when the tag holds a single number, as most cameras write it.
|
||||
iso: asNumber(exif?.ISOSpeedRatings?.computed),
|
||||
focalLength: asNumber(exif?.FocalLength?.computed),
|
||||
orientation: asNumber(exif?.Orientation?.computed),
|
||||
gpsLatitude: asNumber(gps?.Latitude),
|
||||
gpsLongitude: asNumber(gps?.Longitude),
|
||||
iso: asNumber(tags.ISOSpeedRatings?.computed),
|
||||
focalLength: asNumber(tags.FocalLength?.computed),
|
||||
orientation: asNumber(tags.Orientation?.computed),
|
||||
gpsLatitude: asDegrees(
|
||||
tags.GPSLatitude?.computed,
|
||||
tags.GPSLatitudeRef?.computed,
|
||||
"S",
|
||||
),
|
||||
gpsLongitude: asDegrees(
|
||||
tags.GPSLongitude?.computed,
|
||||
tags.GPSLongitudeRef?.computed,
|
||||
"W",
|
||||
),
|
||||
// A GPSAltitudeRef of 1 means the altitude is below sea level.
|
||||
gpsAltitude:
|
||||
altitude !== undefined && exif?.GPSAltitudeRef?.value === 1
|
||||
altitude !== undefined && tags.GPSAltitudeRef?.value === 1
|
||||
? -altitude
|
||||
: altitude,
|
||||
};
|
||||
|
||||
+1
-1
@@ -85,7 +85,7 @@ export type {
|
||||
LibrarySnapshot,
|
||||
LibraryChange,
|
||||
} from "./library/records.js";
|
||||
export type { PhotoExif } from "./exif.js";
|
||||
export type { ExifTags, PhotoExif } from "./exif.js";
|
||||
export { decryptCollection, decryptFile } from "./model/index.js";
|
||||
export { downloadFile, downloadThumbnail } from "./download/index.js";
|
||||
export type {
|
||||
|
||||
+31
-25
@@ -13,14 +13,19 @@
|
||||
//
|
||||
// A `Photo` also fetches its own bytes: `original()`, `thumbnail()`,
|
||||
// `download()`, `content()`, `exif()` and the methods that each return one
|
||||
// field of `exif()` go through the on-disk content cache (issue #46), and are
|
||||
// EXIF field go through the on-disk content cache (issue #46), and are
|
||||
// the one place in this module that may touch the network. A library opened
|
||||
// without a content source leaves that cache absent, and those methods then
|
||||
// throw. `savePath` and `isLocal` look only at the disk and need no cache.
|
||||
|
||||
import { readFile } from "node:fs/promises";
|
||||
|
||||
import { readPhotoExif, type PhotoExif } from "../exif.js";
|
||||
import {
|
||||
readAllExifTags,
|
||||
readPhotoExif,
|
||||
type ExifTags,
|
||||
type PhotoExif,
|
||||
} from "../exif.js";
|
||||
import type { CollectionType, EnteFile, FileType } from "../model/types.js";
|
||||
import type { ContentOptions, ContentResult, PhotoContent } from "./content.js";
|
||||
import type { AlbumRecord, PhotoRecord, DerivedRecords } from "./records.js";
|
||||
@@ -156,74 +161,75 @@ export class Photo implements PhotoExifMethods {
|
||||
return readFile(path);
|
||||
}
|
||||
|
||||
// The common EXIF fields of the original, read from `content()`, so this
|
||||
// may download it. EXIF is read from any image format exifreader reads,
|
||||
// JPEG and HEIC/HEIF among them; any other file gives `{}`, and a video
|
||||
// gives it without fetching anything. Like the other content methods, it
|
||||
// throws when there is no content cache, video or not.
|
||||
async exif(opts?: ContentOptions): Promise<PhotoExif> {
|
||||
// Every EXIF tag of the original, keyed by name (see `ExifTags`), read
|
||||
// from `content()`, so this may download it. EXIF is read from any image
|
||||
// format exifreader reads, JPEG and HEIC/HEIF among them; any other file
|
||||
// gives `{}`, and a video gives it without fetching anything. Like the
|
||||
// other content methods, it throws when there is no content cache, video
|
||||
// or not.
|
||||
async exif(opts?: ContentOptions): Promise<ExifTags> {
|
||||
this.cacheOrThrow();
|
||||
if (this.rec.fileType === "video") return {};
|
||||
return readPhotoExif(await this.content(opts));
|
||||
return readAllExifTags(await this.content(opts));
|
||||
}
|
||||
|
||||
// One field of `exif()` each, named and typed as in `PhotoExif`, and
|
||||
// undefined when the file lacks it. Each call runs `exif()`, which reads
|
||||
// the original again.
|
||||
// One field each, named and typed as in `PhotoExif`, picked from the tags
|
||||
// `exif()` returns, and undefined when the file lacks it. Each call runs
|
||||
// `exif()`, which reads the original again.
|
||||
async make(opts?: ContentOptions): Promise<PhotoExif["make"]> {
|
||||
return (await this.exif(opts)).make;
|
||||
return readPhotoExif(await this.exif(opts)).make;
|
||||
}
|
||||
async model(opts?: ContentOptions): Promise<PhotoExif["model"]> {
|
||||
return (await this.exif(opts)).model;
|
||||
return readPhotoExif(await this.exif(opts)).model;
|
||||
}
|
||||
async lensModel(opts?: ContentOptions): Promise<PhotoExif["lensModel"]> {
|
||||
return (await this.exif(opts)).lensModel;
|
||||
return readPhotoExif(await this.exif(opts)).lensModel;
|
||||
}
|
||||
async dateTimeOriginal(
|
||||
opts?: ContentOptions,
|
||||
): Promise<PhotoExif["dateTimeOriginal"]> {
|
||||
return (await this.exif(opts)).dateTimeOriginal;
|
||||
return readPhotoExif(await this.exif(opts)).dateTimeOriginal;
|
||||
}
|
||||
async offsetTimeOriginal(
|
||||
opts?: ContentOptions,
|
||||
): Promise<PhotoExif["offsetTimeOriginal"]> {
|
||||
return (await this.exif(opts)).offsetTimeOriginal;
|
||||
return readPhotoExif(await this.exif(opts)).offsetTimeOriginal;
|
||||
}
|
||||
async exposureTime(
|
||||
opts?: ContentOptions,
|
||||
): Promise<PhotoExif["exposureTime"]> {
|
||||
return (await this.exif(opts)).exposureTime;
|
||||
return readPhotoExif(await this.exif(opts)).exposureTime;
|
||||
}
|
||||
async fNumber(opts?: ContentOptions): Promise<PhotoExif["fNumber"]> {
|
||||
return (await this.exif(opts)).fNumber;
|
||||
return readPhotoExif(await this.exif(opts)).fNumber;
|
||||
}
|
||||
async iso(opts?: ContentOptions): Promise<PhotoExif["iso"]> {
|
||||
return (await this.exif(opts)).iso;
|
||||
return readPhotoExif(await this.exif(opts)).iso;
|
||||
}
|
||||
async focalLength(
|
||||
opts?: ContentOptions,
|
||||
): Promise<PhotoExif["focalLength"]> {
|
||||
return (await this.exif(opts)).focalLength;
|
||||
return readPhotoExif(await this.exif(opts)).focalLength;
|
||||
}
|
||||
async orientation(
|
||||
opts?: ContentOptions,
|
||||
): Promise<PhotoExif["orientation"]> {
|
||||
return (await this.exif(opts)).orientation;
|
||||
return readPhotoExif(await this.exif(opts)).orientation;
|
||||
}
|
||||
async gpsLatitude(
|
||||
opts?: ContentOptions,
|
||||
): Promise<PhotoExif["gpsLatitude"]> {
|
||||
return (await this.exif(opts)).gpsLatitude;
|
||||
return readPhotoExif(await this.exif(opts)).gpsLatitude;
|
||||
}
|
||||
async gpsLongitude(
|
||||
opts?: ContentOptions,
|
||||
): Promise<PhotoExif["gpsLongitude"]> {
|
||||
return (await this.exif(opts)).gpsLongitude;
|
||||
return readPhotoExif(await this.exif(opts)).gpsLongitude;
|
||||
}
|
||||
async gpsAltitude(
|
||||
opts?: ContentOptions,
|
||||
): Promise<PhotoExif["gpsAltitude"]> {
|
||||
return (await this.exif(opts)).gpsAltitude;
|
||||
return readPhotoExif(await this.exif(opts)).gpsAltitude;
|
||||
}
|
||||
|
||||
private cacheOrThrow(): PhotoContent {
|
||||
|
||||
Reference in New Issue
Block a user