diff --git a/apps/api/src/composition/detect.ts b/apps/api/src/composition/detect.ts index 25e0e44..5bd4c66 100644 --- a/apps/api/src/composition/detect.ts +++ b/apps/api/src/composition/detect.ts @@ -37,14 +37,14 @@ export class SubjectDetector { /** Detections in `file` (any JPEG/PNG), upright, as fractions of the photo. */ async detect(file: string | Buffer): Promise { - const img = sharp(file).rotate(); + const img = sharp(file, { failOn: "none" }).rotate(); const meta = await img.metadata(); const width = meta.autoOrient?.width ?? meta.width ?? 1; const height = meta.autoOrient?.height ?? meta.height ?? 1; const scale = Math.min(SIZE / width, SIZE / height); const rw = Math.round(width * scale); const rh = Math.round(height * scale); - const { data } = await sharp(file) + const { data } = await sharp(file, { failOn: "none" }) .rotate() .resize(rw, rh, { fit: "fill" }) .removeAlpha() @@ -131,7 +131,7 @@ function nms(list: Raw[], threshold = 0.45): Detection[] { async function addSharpness(file: string | Buffer, people: Detection[]): Promise { if (!people.length) return; const W = 1024; - const { data, info } = await sharp(file).rotate().resize({ width: W }).greyscale().raw().toBuffer({ resolveWithObject: true }); + const { data, info } = await sharp(file, { failOn: "none" }).rotate().resize({ width: W }).greyscale().raw().toBuffer({ resolveWithObject: true }); const H = info.height; const values = people.map((p) => { const x0 = Math.max(1, Math.floor(p.x0 * W)); diff --git a/apps/api/src/ingest/service.ts b/apps/api/src/ingest/service.ts index f905c01..2844c4c 100644 --- a/apps/api/src/ingest/service.ts +++ b/apps/api/src/ingest/service.ts @@ -260,12 +260,15 @@ export class IngestService { private async makeDerivatives(orgId: string, sha256: string, storageKey: string): Promise<{ width: number; height: number }> { const { storage } = this.deps; const source = storage.resolve(storageKey); - const meta = await sharp(source).metadata(); + // A truncated upload (interrupted FTP) still has a usable image: decode what is there + // instead of failing the whole photo, as Photo Mechanic does. Genuinely unreadable + // files still throw below (no width/height). + const meta = await sharp(source, { failOn: "none" }).metadata(); if (!meta.width || !meta.height) throw new Error("not a readable image"); const rotated = (meta.orientation ?? 1) >= 5; for (const variant of ["thumb", "preview"] as const) { const side = VARIANT_SIZE[variant]; - const data = await sharp(source) + const data = await sharp(source, { failOn: "none" }) .rotate() .resize({ width: side, height: side, fit: "inside", withoutEnlargement: true }) .jpeg({ quality: variant === "thumb" ? 80 : 88, mozjpeg: true }) diff --git a/apps/api/src/jersey/crop.ts b/apps/api/src/jersey/crop.ts index 3e742a0..da1d93e 100644 --- a/apps/api/src/jersey/crop.ts +++ b/apps/api/src/jersey/crop.ts @@ -69,7 +69,7 @@ export function numberBoxes(face: Box, width: number, height: number): { region: * sharp as the photo has it; the model scales it itself). */ export async function cropJpeg(original: string, box: Box, size = 512): Promise { - const img = sharp(original).rotate(); + const img = sharp(original, { failOn: "none" }).rotate(); const meta = await img.metadata(); const w = meta.autoOrient?.width ?? meta.width ?? 1; const h = meta.autoOrient?.height ?? meta.height ?? 1; @@ -77,7 +77,7 @@ export async function cropJpeg(original: string, box: Box, size = 512): Promise< const top = Math.round(box.y0 * h); const width = Math.max(1, Math.min(w - left, Math.round((box.x1 - box.x0) * w))); const height = Math.max(1, Math.min(h - top, Math.round((box.y1 - box.y0) * h))); - return sharp(original).rotate().extract({ left, top, width, height }).resize({ width: size, height: size, fit: "inside", withoutEnlargement: true }).jpeg({ quality: 92 }).toBuffer(); + return sharp(original, { failOn: "none" }).rotate().extract({ left, top, width, height }).resize({ width: size, height: size, fit: "inside", withoutEnlargement: true }).jpeg({ quality: 92 }).toBuffer(); } /** A photo decoded once, small: enough to tell the colour of a kit. */ @@ -89,7 +89,7 @@ export interface SmallImage { } export async function smallImage(original: string, side = 512): Promise { - const { data, info } = await sharp(original).rotate().resize(side, side, { fit: "inside" }).removeAlpha().raw().toBuffer({ resolveWithObject: true }); + const { data, info } = await sharp(original, { failOn: "none" }).rotate().resize(side, side, { fit: "inside" }).removeAlpha().raw().toBuffer({ resolveWithObject: true }); return { data, width: info.width, height: info.height, channels: info.channels }; } diff --git a/apps/api/src/jersey/model.ts b/apps/api/src/jersey/model.ts index 36d9ec6..9390e78 100644 --- a/apps/api/src/jersey/model.ts +++ b/apps/api/src/jersey/model.ts @@ -37,7 +37,7 @@ export class JerseyModel { /** Reads the number in `box` (fractions of the upright photo). `number` is null when it can't be read. */ async read(original: string, box: Box): Promise { const { size, mean, std, letterbox, labels } = this.meta; - const img = sharp(original).rotate(); + const img = sharp(original, { failOn: "none" }).rotate(); const m = await img.metadata(); const w = m.autoOrient?.width ?? m.width ?? 1; const h = m.autoOrient?.height ?? m.height ?? 1; @@ -45,7 +45,7 @@ export class JerseyModel { const top = Math.round(box.y0 * h); const width = Math.max(1, Math.min(w - left, Math.round((box.x1 - box.x0) * w))); const height = Math.max(1, Math.min(h - top, Math.round((box.y1 - box.y0) * h))); - const { data } = await sharp(original) + const { data } = await sharp(original, { failOn: "none" }) .rotate() .extract({ left, top, width, height }) .resize({ width: size, height: size, fit: "contain", background: { r: letterbox, g: letterbox, b: letterbox } })