From 9f9f5715af32e9b499c7323c58a98e2737b4fcf3 Mon Sep 17 00:00:00 2001 From: spark Date: Wed, 21 Oct 2026 04:37:33 +0100 Subject: [PATCH] Accept a camera clock with no timezone when grouping a property shoot. A Canon body stores the wall time without an offset. Rejecting that date rejected the whole folder, so the page showed a generic error after counting the sidecars. --- apps/api/src/realestate/clock.test.ts | 34 +++++++++++++++ apps/api/src/realestate/clock.ts | 61 +++++++++++++++++++++++++++ apps/api/src/realestate/routes.ts | 50 ++-------------------- apps/api/test/real-estate.test.ts | 16 +++++++ 4 files changed, 114 insertions(+), 47 deletions(-) create mode 100644 apps/api/src/realestate/clock.test.ts create mode 100644 apps/api/src/realestate/clock.ts diff --git a/apps/api/src/realestate/clock.test.ts b/apps/api/src/realestate/clock.test.ts new file mode 100644 index 0000000..15bd75a --- /dev/null +++ b/apps/api/src/realestate/clock.test.ts @@ -0,0 +1,34 @@ +import { groupBrackets } from "@photoreport/metadata"; +import { describe, expect, it } from "vitest"; +import { frameClock, groupFrames } from "./clock.js"; + +describe("camera clock", () => { + it("keeps a wall time with no timezone, and an offset when the file has one", () => { + expect(frameClock("2026-10-21T10:00:00")).toBe("2026-10-21T10:00:00"); + expect(frameClock("2026-10-21T10:00:00.200")).toBe("2026-10-21T10:00:00.200"); + expect(frameClock("2026-10-21T10:00:00+01:00")).toBe("2026-10-21T10:00:00+01:00"); + expect(frameClock("2026-10-21T10:00:00Z")).toBe("2026-10-21T10:00:00Z"); + }); + + it("drops a date that is not a time instead of rejecting the folder", () => { + const frames = groupFrames({ + frames: [ + { id: "a", capturedAt: "2026-10-21T10:00:00", exposureBias: -2 }, + { id: "b", capturedAt: "not a date", exposureBias: 0 }, + { id: "c", exposureBias: 2 }, + ], + }); + expect(frames.map((frame) => frame.capturedAt)).toEqual(["2026-10-21T10:00:00", null, null]); + }); + + it("groups a Canon bracket whose clock has no timezone", () => { + const frames = groupFrames({ + frames: [ + { id: "a", fileName: "a.CR3", capturedAt: "2026-10-21T10:00:00", cameraSerial: "body", focalLength: 16, aperture: 8, iso: 100, exposureBias: -2, exposureTime: 0.01 }, + { id: "b", fileName: "b.CR3", capturedAt: "2026-10-21T10:00:00.200", cameraSerial: "body", focalLength: 16, aperture: 8, iso: 100, exposureBias: 0, exposureTime: 0.04 }, + { id: "c", fileName: "c.CR3", capturedAt: "2026-10-21T10:00:00.400", cameraSerial: "body", focalLength: 16, aperture: 8, iso: 100, exposureBias: 2, exposureTime: 0.1 }, + ], + }); + expect(groupBrackets(frames)).toEqual({ stacks: [{ frameIds: ["a", "b", "c"] }], aside: [] }); + }); +}); diff --git a/apps/api/src/realestate/clock.ts b/apps/api/src/realestate/clock.ts new file mode 100644 index 0000000..0b43a33 --- /dev/null +++ b/apps/api/src/realestate/clock.ts @@ -0,0 +1,61 @@ +import type { BracketFrame } from "@photoreport/metadata"; +import { z } from "zod"; + +/** + * A camera clock. Canon (and most bodies) store wall time with no timezone, so `2026-10-21T10:00:00` + * is a real capture time. The offset is kept when the file recorded one. Anything else is dropped: + * one bad sidecar must not reject the whole folder. + */ +const cameraInstant = z.iso.datetime({ local: true, offset: true }); + +export function frameClock(value: string | null | undefined): string | null { + if (value == null) return null; + const trimmed = value.trim(); + if (trimmed === "" || trimmed.length > 64) return null; + return cameraInstant.safeParse(trimmed).success ? trimmed : null; +} + +const optionalText = z.string().trim().max(300).nullable().optional(); +const optionalNumber = z.number().nullable().optional(); + +const FrameBody = z.object({ + id: z.string().trim().min(1).max(2000), + fileName: optionalText, + capturedAt: z.string().nullable().optional().transform((value) => frameClock(value)), + cameraSerial: optionalText, + cameraMake: optionalText, + cameraModel: optionalText, + focalLength: optionalNumber, + aperture: optionalNumber, + iso: optionalNumber, + width: optionalNumber, + height: optionalNumber, + exposureBias: optionalNumber, + exposureTime: optionalNumber, +}); + +const GroupBody = z.object({ frames: z.array(FrameBody).max(4000) }); + +function finite(value: number | null | undefined): number | null { + if (value == null || !Number.isFinite(value)) return null; + return value; +} + +/** The exposure metadata of one shoot. A date the camera did not write as a time becomes empty. */ +export function groupFrames(body: unknown): BracketFrame[] { + return GroupBody.parse(body).frames.map((frame) => ({ + id: frame.id, + fileName: frame.fileName ?? null, + capturedAt: frame.capturedAt ?? null, + cameraSerial: frame.cameraSerial ?? null, + cameraMake: frame.cameraMake ?? null, + cameraModel: frame.cameraModel ?? null, + focalLength: finite(frame.focalLength), + aperture: finite(frame.aperture), + iso: finite(frame.iso), + width: finite(frame.width), + height: finite(frame.height), + exposureBias: finite(frame.exposureBias), + exposureTime: finite(frame.exposureTime), + })); +} diff --git a/apps/api/src/realestate/routes.ts b/apps/api/src/realestate/routes.ts index d69ee81..ba11190 100644 --- a/apps/api/src/realestate/routes.ts +++ b/apps/api/src/realestate/routes.ts @@ -1,35 +1,15 @@ -import { groupBrackets, type BracketFrame } from "@photoreport/metadata"; +import { groupBrackets } from "@photoreport/metadata"; import type { FastifyInstance } from "fastify"; import { z } from "zod"; import { requireAuth, requireMembership } from "../auth/guard.js"; import type { SessionService } from "../auth/sessions.js"; import type { Db } from "../db.js"; import { conflict } from "../lib/errors.js"; +import { groupFrames } from "./clock.js"; const OrgParams = z.object({ orgId: z.uuid() }); const SettingBody = z.object({ enabled: z.boolean() }); -const optionalText = z.string().trim().max(300).nullable().optional(); -const optionalNumber = z.number().nullable().optional(); - -const FrameBody = z.object({ - id: z.string().trim().min(1).max(400), - fileName: optionalText, - capturedAt: z.iso.datetime().nullable().optional(), - cameraSerial: optionalText, - cameraMake: optionalText, - cameraModel: optionalText, - focalLength: optionalNumber, - aperture: optionalNumber, - iso: optionalNumber, - width: optionalNumber, - height: optionalNumber, - exposureBias: optionalNumber, - exposureTime: optionalNumber, -}); - -const GroupBody = z.object({ frames: z.array(FrameBody).max(4000) }); - /** * The interior-bracket area. It never touches sports ingest: files stay on the photographer's * computer and only the exposure metadata is sent, to be grouped. Merging the frames is later. @@ -60,30 +40,6 @@ export async function registerRealEstateRoutes(app: FastifyInstance, deps: { db: await requireMembership(db, req, orgId, "PHOTOGRAPHER"); const org = await db.organization.findUniqueOrThrow({ where: { id: orgId }, select: { realEstateEnabled: true } }); if (!org.realEstateEnabled) throw conflict("real_estate_disabled"); - const body = GroupBody.parse(req.body); - return groupBrackets(body.frames.map(finiteFrame)); + return groupBrackets(groupFrames(req.body)); }); } - -function finite(value: number | null | undefined): number | null { - if (value == null || !Number.isFinite(value)) return null; - return value; -} - -function finiteFrame(frame: z.infer): BracketFrame { - return { - id: frame.id, - fileName: frame.fileName ?? null, - capturedAt: frame.capturedAt ?? null, - cameraSerial: frame.cameraSerial ?? null, - cameraMake: frame.cameraMake ?? null, - cameraModel: frame.cameraModel ?? null, - focalLength: finite(frame.focalLength), - aperture: finite(frame.aperture), - iso: finite(frame.iso), - width: finite(frame.width), - height: finite(frame.height), - exposureBias: finite(frame.exposureBias), - exposureTime: finite(frame.exposureTime), - }; -} diff --git a/apps/api/test/real-estate.test.ts b/apps/api/test/real-estate.test.ts index 3c35479..feec7c2 100644 --- a/apps/api/test/real-estate.test.ts +++ b/apps/api/test/real-estate.test.ts @@ -79,5 +79,21 @@ describe("real estate", () => { }); expect(grouped.statusCode).toBe(200); expect(grouped.json()).toEqual({ stacks: [{ frameIds: ["a", "b", "c"] }], aside: ["solo"] }); + + const localClock = await ctx.app.inject({ + method: "POST", + url: `${url}/groups`, + headers: bearer(photographer.body.accessToken), + payload: { + frames: [ + { ...shot("a", -2, 0), id: "local-a", capturedAt: "2026-10-21T10:00:00" }, + { ...shot("b", 0, 200), id: "local-b", capturedAt: "2026-10-21T10:00:00.200" }, + { ...shot("c", 2, 400), id: "local-c", capturedAt: "2026-10-21T10:00:00.400" }, + { id: "bad", fileName: "nota.XMP", capturedAt: "not a date", cameraSerial: "other", exposureBias: 0 }, + ], + }, + }); + expect(localClock.statusCode).toBe(200); + expect(localClock.json()).toEqual({ stacks: [{ frameIds: ["local-a", "local-b", "local-c"] }], aside: ["bad"] }); }); }); -- 2.47.3