diff --git a/apps/api/src/archive/service.ts b/apps/api/src/archive/service.ts index 01a8bae..62e6691 100644 --- a/apps/api/src/archive/service.ts +++ b/apps/api/src/archive/service.ts @@ -878,7 +878,15 @@ export class ArchiveService { eventId = await this.eventForFixture(folder.source, fixture); } const previous = folder.eventId; - const updated = await db.cloudFolder.update({ where: { id: folderId }, data: { eventId, matchedBy } }); + let updated: CloudFolder; + try { + updated = await db.cloudFolder.update({ where: { id: folderId }, data: { eventId, matchedBy } }); + } catch (err) { + // The event just checked above was deleted in the gap before this write landed: same outcome as it + // never having been found, a clean rejection instead of a raw foreign key error bubbling up. + if ((err as { code?: string }).code === "P2003") throw new ArchiveRejected("event_not_found"); + throw err; + } const files = await db.cloudFile.findMany({ where: { folderId, photoId: { not: null } }, select: { photoId: true } }); const ids = files.map((f) => f.photoId!); if (ids.length) { diff --git a/apps/api/src/ingest/service.ts b/apps/api/src/ingest/service.ts index f3680b3..40d0c1b 100644 --- a/apps/api/src/ingest/service.ts +++ b/apps/api/src/ingest/service.ts @@ -164,7 +164,7 @@ export class IngestService { const at = capturedAt ?? new Date(); // Archive imports name their own event. A file with no camera time, or a time outside that event's window, // must not fall through onto whatever is live today. - const eventId = origin.inbox + const requestedEventId = origin.inbox ? null : origin.held && origin.eventId ? origin.eventId @@ -176,39 +176,49 @@ export class IngestService { await storage.adopt(file, storageKey); let photo; + // EXIF/IPTC text can carry NUL bytes, which PostgreSQL text columns refuse (#91). + const data = stripNulBytes({ + organizationId: orgId, + eventId: requestedEventId, + uploaderId: origin.userId, + source: origin.source, + originalName: origin.originalName, + sha256, + byteSize: origin.original?.size ?? size, + storageKey, + originalStored: !origin.original, + capturedAt: capturedAt ?? null, + cameraMake: camera.cameraMake ?? null, + cameraModel: camera.cameraModel ?? null, + cameraSerial: camera.cameraSerial ?? null, + lens: camera.lens ?? null, + focalLength: camera.focalLength ?? null, + aperture: camera.aperture ?? null, + exposureTime: camera.exposureTime ?? null, + iso: camera.iso ?? null, + ...(camera.extra && Object.keys(camera.extra).length && { exif: camera.extra }), + ...(Object.keys(embedded).length && { embeddedIptc: embedded }), + }); try { - photo = await db.photo.create({ - data: stripNulBytes({ - organizationId: orgId, - eventId, - uploaderId: origin.userId, - source: origin.source, - originalName: origin.originalName, - sha256, - byteSize: origin.original?.size ?? size, - storageKey, - originalStored: !origin.original, - capturedAt: capturedAt ?? null, - cameraMake: camera.cameraMake ?? null, - cameraModel: camera.cameraModel ?? null, - cameraSerial: camera.cameraSerial ?? null, - lens: camera.lens ?? null, - focalLength: camera.focalLength ?? null, - aperture: camera.aperture ?? null, - exposureTime: camera.exposureTime ?? null, - iso: camera.iso ?? null, - ...(camera.extra && Object.keys(camera.extra).length && { exif: camera.extra }), - ...(Object.keys(embedded).length && { embeddedIptc: embedded }), - }), - }); + photo = await db.photo.create({ data }); } catch (err) { // Two copies of the same file finishing at once: the other one won. if (err instanceof Prisma.PrismaClientKnownRequestError && err.code === "P2002") { const winner = await db.photo.findUniqueOrThrow({ where: { organizationId_sha256: { organizationId: orgId, sha256 } } }); return { status: "duplicate", photoId: winner.id }; } - throw err; + // The event picked a moment ago (archive match, FTP target, the day's routing) was put away or deleted + // while this photo was being prepared (hashing, EXIF, the file's move into storage can take a while): + // same outcome as no event being open, the photo goes to "Sem evento" instead of failing outright. + if (err instanceof Prisma.PrismaClientKnownRequestError && err.code === "P2003" && requestedEventId) { + log.info({ eventId: requestedEventId }, "ingest: event was gone by the time the photo was ready, filed under no event"); + photo = await db.photo.create({ data: { ...data, eventId: null } }); + } else { + throw err; + } } + // The event actually stored on the photo: the same id asked for, or `null` when it vanished just above. + const eventId = photo.eventId; let outcome: IngestOutcome; try { diff --git a/apps/api/test/archive.test.ts b/apps/api/test/archive.test.ts index de135e4..0d290ae 100644 --- a/apps/api/test/archive.test.ts +++ b/apps/api/test/archive.test.ts @@ -1416,8 +1416,8 @@ describe("archive from a Nextcloud", () => { expect((await get(owner, `/v1/archive/${source.id}/directory`)).json()).toEqual({ ready: true, teams: [ - { name: "ABC de Braga", galleries: 1 }, - { name: "Sporting CP", galleries: 1 }, + { name: "ABC de Braga", galleries: 1, kind: "club" }, + { name: "Sporting CP", galleries: 1, kind: "club" }, ], }); const none = await signIn("ninguem"); @@ -1425,7 +1425,7 @@ describe("archive from a Nextcloud", () => { await ctx.db.archiveClubAlias.create({ data: { sourceId: source.id, aliasKey: "abc de braga", alias: "ABC de Braga", name: "Sporting CP" } }); await worker.markDirectoryDirty(source.id); expect(await worker.rebuildDue()).toBe(true); - expect((await get(owner, `/v1/archive/${source.id}/directory`)).json()).toEqual({ ready: true, teams: [{ name: "Sporting CP", galleries: 1 }] }); + expect((await get(owner, `/v1/archive/${source.id}/directory`)).json()).toEqual({ ready: true, teams: [{ name: "Sporting CP", galleries: 1, kind: "club" }] }); expect(await worker.rebuildDue()).toBe(false); const games = await get(owner, `/v1/archive/${source.id}/galleries?team=${encodeURIComponent("Sporting CP")}`); expect(games.statusCode).toBe(200);