exactOptionalPropertyTypes rejects undefined, and that is what fails the API build on main.
68 lines
3.6 KiB
TypeScript
68 lines
3.6 KiB
TypeScript
import { createReadStream, existsSync } from "node:fs";
|
|
import { stat } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
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 { notFound } from "../lib/errors.js";
|
|
import { modelsDir } from "../lib/models.js";
|
|
import type { LocalStorage } from "../storage/storage.js";
|
|
import { readFocusPoint } from "../ingest/exif.js";
|
|
import { focusFromExifJson } from "../ingest/focus.js";
|
|
import { compose, isReportage } from "./compose.js";
|
|
import { lazyDetector } from "./detect.js";
|
|
|
|
const Params = z.object({ photoId: z.uuid() });
|
|
const Body = z.object({ aspect: z.tuple([z.number().positive(), z.number().positive()]).nullable().optional() }).default({});
|
|
|
|
/**
|
|
* Automatic crop (Revelar → Recorte → Auto): who the photo is about and a news crop around them, from the 2048 px
|
|
* preview. The detector loads on first use; without its model the endpoint answers 503 and the button says so.
|
|
*/
|
|
export async function registerCompositionRoutes(app: FastifyInstance, deps: { db: Db; sessions: SessionService; storage: LocalStorage; storageRoot: string }) {
|
|
const { db, storage } = deps;
|
|
const authenticate = requireAuth(deps.sessions);
|
|
const load = lazyDetector(deps.storageRoot);
|
|
|
|
app.post("/v1/photos/:photoId/auto-crop", { preHandler: authenticate }, async (req, reply) => {
|
|
const { photoId } = Params.parse(req.params);
|
|
const { aspect } = Body.parse(req.body ?? {});
|
|
const photo = await db.photo.findUnique({
|
|
where: { id: photoId },
|
|
select: {
|
|
organizationId: true,
|
|
sha256: true,
|
|
status: true,
|
|
exif: true,
|
|
storageKey: true,
|
|
originalStored: true,
|
|
faces: { select: { x0: true, y0: true, x1: true, y1: true } },
|
|
event: { select: { away: true, fixtureId: true, sport: true } },
|
|
},
|
|
});
|
|
if (!photo || photo.status !== "READY") throw notFound("photo_not_found");
|
|
await requireMembership(db, req, photo.organizationId, "PHOTOGRAPHER");
|
|
const pending = load();
|
|
if (!pending) return reply.code(503).send({ error: "auto_crop_unavailable" });
|
|
const preview = storage.resolve(storage.derivedKey(photo.organizationId, photo.sha256, "preview"));
|
|
const detections = await (await pending).detect(preview);
|
|
const original = photo.originalStored ? storage.resolve(photo.storageKey) : preview;
|
|
const focus = focusFromExifJson(photo.exif) ?? (existsSync(original) ? await readFocusPoint(original) : null);
|
|
const context = isReportage(photo.event) ? "reportage" : "sport";
|
|
return { crop: compose(detections, aspect ? aspect[0] / aspect[1] : null, { focus, context, faces: photo.faces, sport: photo.event?.sport ?? null }) };
|
|
});
|
|
|
|
/** YOLOX-s, so the desktop can crop with the same detector. Not a face model: no biometrics gate. */
|
|
app.get("/v1/orgs/:orgId/models/yolox_s.onnx", { preHandler: authenticate }, async (req, reply) => {
|
|
const { orgId } = z.object({ orgId: z.uuid() }).parse(req.params);
|
|
await requireMembership(db, req, orgId, "PHOTOGRAPHER");
|
|
const dir = modelsDir(deps.storageRoot);
|
|
const file = dir ? join(dir, "yolox_s.onnx") : "";
|
|
const info = file ? await stat(file).catch(() => null) : null;
|
|
if (!info?.isFile()) return reply.code(404).send({ error: "models_missing" });
|
|
return reply.header("content-type", "application/octet-stream").header("content-length", String(info.size)).header("cache-control", "private, max-age=86400").send(createReadStream(file));
|
|
});
|
|
}
|