Templates (IPTC pad, folders, file names, rename on arrival) take every variable of Photo Mechanic's list under its names and short names, in any case, with its modifiers (substrings, lc/UC/PC, tr:find/repl). IPTC variables are the photo's pad, as in Photo Mechanic. The pad gains every Photo Mechanic field with the same IIM/XMP tags, read back from incoming files. Camera details (exposure compensation, flash, metering, program, white balance, 35mm, lens range, firmware, owner, artist, GPS) are kept in photos.exif at ingest and read from the file at delivery for older photos. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01HT6iXLYUUp9pJz5Kza9xCW
97 lines
5.8 KiB
TypeScript
97 lines
5.8 KiB
TypeScript
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import path from "node:path";
|
|
import sharp from "sharp";
|
|
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest";
|
|
import { deliveredName } from "../src/delivery/service.js";
|
|
import { IngestService } from "../src/ingest/service.js";
|
|
import { MemoryBroadcaster } from "../src/realtime.js";
|
|
import { LocalStorage, UrlSigner } from "../src/storage/storage.js";
|
|
import { bearer, createTestApp, login, resetDb, seedUser, type TestContext } from "./helpers.js";
|
|
|
|
const quiet = { info: () => {}, warn: () => {} };
|
|
|
|
describe("rename on arrival", () => {
|
|
let ctx: TestContext;
|
|
let dir: string;
|
|
let ingestRoot: string;
|
|
let ingest: IngestService;
|
|
|
|
beforeAll(async () => {
|
|
dir = await mkdtemp(path.join(tmpdir(), "pr-rename-"));
|
|
ingestRoot = path.join(dir, "ingest");
|
|
ctx = await createTestApp({ STORAGE_ROOT: path.join(dir, "storage"), INGEST_ROOT: ingestRoot });
|
|
ingest = new IngestService({ db: ctx.db, storage: new LocalStorage(ctx.config.STORAGE_ROOT), signer: new UrlSigner(ctx.config.JWT_SECRET, 3600), broadcaster: new MemoryBroadcaster(), ingestRoot, log: quiet });
|
|
});
|
|
afterAll(async () => {
|
|
await ctx.app.close();
|
|
await ctx.db.$disconnect();
|
|
await rm(dir, { recursive: true, force: true });
|
|
});
|
|
beforeEach(() => resetDb(ctx.db));
|
|
|
|
let shade = 0;
|
|
async function arrive(name: string, taken: string) {
|
|
const file = path.join(ingestRoot, "peter", name);
|
|
await mkdir(path.dirname(file), { recursive: true });
|
|
const data = await sharp({ create: { width: 64, height: 48, channels: 3, background: { r: ++shade, g: 10, b: 10 } } })
|
|
.jpeg()
|
|
.withExif({ IFD0: { Make: "Canon", Model: "Canon EOS R3" }, IFD2: { DateTimeOriginal: taken } })
|
|
.toBuffer();
|
|
await writeFile(file, data);
|
|
const out = await ingest.ingestFile(file);
|
|
if (out.status !== "ingested") throw new Error(JSON.stringify(out));
|
|
return ctx.db.photo.findUniqueOrThrow({ where: { id: out.photoId } });
|
|
}
|
|
|
|
it("names photos with the organisation's template, a login's own, or keeps the camera name", async () => {
|
|
const { org, user } = await seedUser(ctx.db, "[email protected]", { org: "pr", role: "ADMIN" });
|
|
await ctx.db.user.update({ where: { id: user.id }, data: { name: "Peter Spark" } });
|
|
const account = await ctx.db.ftpAccount.create({ data: { organizationId: org!.id, userId: user.id, login: "peter" } });
|
|
const token = (await login(ctx.app, user.email)).body.accessToken;
|
|
|
|
const bad = await ctx.app.inject({ method: "PUT", url: `/v1/orgs/${org!.id}/rename-template`, headers: bearer(token), payload: { template: "PS_{evento}" } });
|
|
expect(bad.json()).toMatchObject({ error: "invalid_template" });
|
|
const ok = await ctx.app.inject({ method: "PUT", url: `/v1/orgs/${org!.id}/rename-template`, headers: bearer(token), payload: { template: "{initials}_{datesort}_{timesort}" } });
|
|
expect(ok.json()).toEqual({ template: "{initials}_{datesort}_{timesort}", example: "PS_20261011_19310745.jpg" });
|
|
|
|
const a = await arrive("IMG_0001.JPG", "2026:10:11 19:31:07");
|
|
const b = await arrive("IMG_0002.JPG", "2026:10:11 19:31:07"); // same second, no sub-seconds: the second gets _2
|
|
expect([a.fileName, b.fileName]).toEqual(["PS_20261011_19310700", "PS_20261011_19310700_2"]);
|
|
expect(deliveredName(a)).toBe("PS_20261011_19310700.JPG");
|
|
const list = (await ctx.app.inject({ method: "GET", url: `/v1/orgs/${org!.id}/photos`, headers: bearer(token) })).json();
|
|
expect(list.photos.map((p: { name: string; originalName: string }) => `${p.originalName}>${p.name}`).sort()).toEqual([
|
|
"IMG_0001.JPG>PS_20261011_19310700.JPG",
|
|
"IMG_0002.JPG>PS_20261011_19310700_2.JPG",
|
|
]);
|
|
|
|
// The login's own template wins; "" keeps the camera names.
|
|
await ctx.app.inject({ method: "PATCH", url: `/v1/orgs/${org!.id}/ftp-accounts/${account.id}`, headers: bearer(token), payload: { renameTemplate: "" } });
|
|
expect((await arrive("IMG_0003.JPG", "2026:10:11 19:40:00")).fileName).toBeNull();
|
|
});
|
|
|
|
it("waits for the event when the template uses it, and follows the photo to another event", async () => {
|
|
const { org, user } = await seedUser(ctx.db, "[email protected]", { org: "pr", role: "ADMIN" });
|
|
await ctx.db.ftpAccount.create({ data: { organizationId: org!.id, userId: user.id, login: "peter", renameTemplate: "PS_{event}_{timesort}" } });
|
|
const token = (await login(ctx.app, user.email)).body.accessToken;
|
|
const photo = await arrive("IMG_0001.JPG", "2026:10:11 19:31:07");
|
|
expect(photo.eventId).toBeNull();
|
|
expect(photo.fileName).toBeNull();
|
|
const event = await ctx.db.event.create({
|
|
data: { organizationId: org!.id, home: "ABC Braga", away: "CDE Gil Eanes", startsAt: new Date("2026-10-11T17:00:00Z"), ingestFrom: new Date("2026-10-11T15:00:00Z"), ingestUntil: new Date("2026-10-11T21:00:00Z"), iptcTemplate: {} },
|
|
});
|
|
await ctx.app.inject({ method: "PATCH", url: `/v1/orgs/${org!.id}/photos`, headers: bearer(token), payload: { ids: [photo.id], patch: { eventId: event.id } } });
|
|
expect((await ctx.db.photo.findUniqueOrThrow({ where: { id: photo.id } })).fileName).toBe("PS_ABC Braga x CDE Gil Eanes_19310700");
|
|
});
|
|
});
|
|
|
|
describe("Photo Mechanic rename templates", () => {
|
|
it("accepts its variables in any case, with its modifiers", async () => {
|
|
const { renameExample } = await import("../src/library/ftp.js");
|
|
expect(renameExample("PS_{Event:0:lc:tr: /_}_{filenamebase:-4}_{IPTCDATE}")).toBe("PS_abc_braga_x_cde_gil_eanes_1234_20261011.jpg");
|
|
expect(renameExample("{model:0:UC}_{serialnum}_{timesort}")).toBe("CANON EOS R3_123_19310745.jpg");
|
|
expect(() => renameExample("{event:shout}")).toThrow(/invalid modifiers/);
|
|
expect(() => renameExample("{nope}")).toThrow(/unknown variables/);
|
|
});
|
|
});
|