mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
fix(journey): keep an iPhone photo's capture time and place through the HEIC conversion
The browser converts a HEIC to a JPEG before it uploads it, and that JPEG carries no EXIF, so iPhone photos arrived without their capture time and GPS. A small lazily loaded reader now takes DateTimeOriginal with its offset and the GPS pair from the HEIC's Exif item before the conversion, and journey photo uploads (gallery, entry, Studio) send them as optional taken_at, lat and lng fields. The server reads those fields leniently (an ISO instant between 1900 and tomorrow, a pair inside the globe that is not 0,0) and hands them to the capture backfill only when the request carried one file. They fill in only what the file's own EXIF lacks and go through the existing COALESCE write, so a stored value is never replaced. Covers and place images keep the plain conversion and never carry a location.
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* captureHintFrom: the capture time and place a client read from a HEIC before
|
||||
* converting it (#3120), held to the rules the backfill holds a file's own EXIF
|
||||
* to. Anything that is not a real time or place is dropped, never refused.
|
||||
*/
|
||||
import { captureHintFrom } from '../../../src/nest/journey/journey-capture-hint';
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
const NOW = Date.UTC(2026, 10, 30, 12, 0, 0);
|
||||
|
||||
describe('captureHintFrom', () => {
|
||||
it('CHINT-001: reads an ISO instant and a pair of coordinates', () => {
|
||||
const fields = { taken_at: '2026-05-30T15:52:19+02:00', lat: '64.1466000', lng: '-21.9426000' };
|
||||
expect(captureHintFrom(fields, NOW)).toEqual({
|
||||
takenAt: '2026-05-30T13:52:19.000Z',
|
||||
lat: 64.1466,
|
||||
lng: -21.9426,
|
||||
});
|
||||
});
|
||||
|
||||
it('CHINT-002: no fields, or nothing usable in them, is no hint', () => {
|
||||
expect(captureHintFrom(undefined, NOW)).toBeNull();
|
||||
expect(captureHintFrom({}, NOW)).toBeNull();
|
||||
expect(captureHintFrom({ taken_at: 'yesterday', lat: 'north', lng: '2' }, NOW)).toBeNull();
|
||||
});
|
||||
|
||||
it('CHINT-003: a time before 1900 or more than a day ahead is dropped, the place kept', () => {
|
||||
for (const taken_at of ['1899-12-31T23:59:59Z', '2026-12-02T12:00:00Z', '2026-02-30T10:00:00Z']) {
|
||||
expect(captureHintFrom({ taken_at, lat: '48.8584', lng: '2.2945' }, NOW)).toEqual({
|
||||
takenAt: null,
|
||||
lat: 48.8584,
|
||||
lng: 2.2945,
|
||||
});
|
||||
}
|
||||
expect(captureHintFrom({ taken_at: '2026-12-01T11:00:00Z' }, NOW)?.takenAt).toBe('2026-12-01T11:00:00.000Z');
|
||||
});
|
||||
|
||||
it('CHINT-004: half a pair, a pair outside the globe, 0,0 or an exponent is no place, the time kept', () => {
|
||||
const taken_at = '2026-05-30T13:52:19.000Z';
|
||||
for (const [lat, lng] of [
|
||||
['48.8584', undefined],
|
||||
['91', '2.2945'],
|
||||
['48.8584', '-180.5'],
|
||||
['0', '0'],
|
||||
['1e-7', '2'],
|
||||
[' 48.8', '2'],
|
||||
] as Array<[string | undefined, string | undefined]>) {
|
||||
expect(captureHintFrom({ taken_at, lat, lng }, NOW)).toEqual({ takenAt: taken_at, lat: null, lng: null });
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -204,7 +204,7 @@ describe('JourneyPhotoCaptureService', () => {
|
||||
svc.scheduleUpload([{ id: 1, photo_id: 11 }, null, { id: 2 }], 3);
|
||||
await vi.waitFor(() => expect(placeEntriesFromPhotos).toHaveBeenCalledWith([11]));
|
||||
|
||||
expect(run).toHaveBeenCalledWith([11], 3);
|
||||
expect(run).toHaveBeenCalledWith([11], 3, undefined);
|
||||
expect(journeyIdOfEntry).not.toHaveBeenCalled();
|
||||
expect(broadcastJourneyEvent).not.toHaveBeenCalled();
|
||||
|
||||
@@ -214,6 +214,19 @@ describe('JourneyPhotoCaptureService', () => {
|
||||
expect(run).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('JPCAP-009: a capture hint goes to the backfill for the one photo it was read from (#3120)', async () => {
|
||||
const { svc, run, placeEntriesFromPhotos } = build(true);
|
||||
const hint = { takenAt: '2026-05-30T13:52:19.000Z', lat: 64.1466, lng: -21.9426 };
|
||||
|
||||
svc.scheduleUpload([{ id: 1, photo_id: 11 }], 3, hint);
|
||||
await vi.waitFor(() => expect(placeEntriesFromPhotos).toHaveBeenCalledWith([11]));
|
||||
expect(run).toHaveBeenLastCalledWith([11], 3, new Map([[11, hint]]));
|
||||
|
||||
// Two photos from one request: the hint names neither, so it is dropped.
|
||||
svc.scheduleUpload([{ photo_id: 12 }, { photo_id: 13 }], 3, hint);
|
||||
await vi.waitFor(() => expect(run).toHaveBeenLastCalledWith([12, 13], 3, undefined));
|
||||
});
|
||||
|
||||
it('JPCAP-1003: an entry placed from its photo gets a name and the journey hears about it', async () => {
|
||||
const { svc, placeEntriesFromPhotos, nameEntryLocation, reverseGeocode, broadcastJourneyEvent } = build(false);
|
||||
placeEntriesFromPhotos.mockReturnValueOnce([
|
||||
|
||||
@@ -433,6 +433,51 @@ describe('PhotoCaptureBackfillService — local files', () => {
|
||||
expect(recordCaptureMetadata).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('CAPTURE-026: a converted HEIC without EXIF takes the capture the client read before converting it (#3120)', async () => {
|
||||
vi.mocked(exifr.parse).mockResolvedValue(undefined as never);
|
||||
const { svc, recordCaptureMetadata } = localBuild([{ id: 7, provider: 'local', file_path: 'journey/a.jpg' }]);
|
||||
const hint = { takenAt: '2026-05-30T13:52:19.000Z', lat: 64.1466, lng: -21.9426 };
|
||||
|
||||
await svc.run([7], 1, new Map([[7, hint]]));
|
||||
|
||||
expect(recordCaptureMetadata).toHaveBeenCalledWith(7, hint);
|
||||
});
|
||||
|
||||
it("CAPTURE-027: the file's own EXIF wins, and the hint only fills in what the file lacks", async () => {
|
||||
vi.mocked(exifr.parse).mockResolvedValue({ DateTimeOriginal: '2026:03:15 11:20:00', OffsetTimeOriginal: '+01:00' });
|
||||
const { svc, recordCaptureMetadata } = localBuild([
|
||||
{ id: 7, provider: 'local', file_path: 'journey/a.jpg' },
|
||||
{ id: 8, provider: 'local', file_path: 'journey/b.jpg' },
|
||||
]);
|
||||
const hint = { takenAt: '2026-05-30T13:52:19.000Z', lat: 64.1466, lng: -21.9426 };
|
||||
|
||||
await svc.run([7], 1, new Map([[7, hint]]));
|
||||
expect(recordCaptureMetadata).toHaveBeenLastCalledWith(7, {
|
||||
takenAt: '2026-03-15T10:20:00.000Z',
|
||||
lat: 64.1466,
|
||||
lng: -21.9426,
|
||||
});
|
||||
|
||||
vi.mocked(exifr.parse).mockResolvedValue({ latitude: 48.8584, longitude: 2.2945 });
|
||||
await svc.run([8], 1, new Map([[8, { takenAt: null, lat: 1, lng: 2 }]]));
|
||||
expect(recordCaptureMetadata).toHaveBeenLastCalledWith(8, { takenAt: null, lat: 48.8584, lng: 2.2945 });
|
||||
});
|
||||
|
||||
it('CAPTURE-028: a hint names one photo; the others of the batch read their file alone', async () => {
|
||||
vi.mocked(exifr.parse).mockResolvedValue({});
|
||||
const { svc, recordCaptureMetadata } = localBuild([
|
||||
{ id: 7, provider: 'local', file_path: 'journey/a.jpg' },
|
||||
{ id: 8, provider: 'local', file_path: 'journey/b.jpg' },
|
||||
]);
|
||||
|
||||
const hint = { takenAt: '2026-05-30T13:52:19.000Z', lat: null, lng: null };
|
||||
|
||||
await svc.run([7, 8], 1, new Map([[7, hint]]));
|
||||
|
||||
expect(recordCaptureMetadata).toHaveBeenCalledTimes(1);
|
||||
expect(recordCaptureMetadata).toHaveBeenCalledWith(7, hint);
|
||||
});
|
||||
|
||||
it('CAPTURE-009: a file with nothing readable is left alone', async () => {
|
||||
vi.mocked(exifr.parse).mockResolvedValue({});
|
||||
const { svc, recordCaptureMetadata } = localBuild([{ id: 7, provider: 'local', file_path: 'journey/a.jpg' }]);
|
||||
|
||||
Reference in New Issue
Block a user