diff --git a/apps/worker/src/__tests__/variants-sizes.test.ts b/apps/worker/src/__tests__/variants-sizes.test.ts index 5701667..7940f0d 100644 --- a/apps/worker/src/__tests__/variants-sizes.test.ts +++ b/apps/worker/src/__tests__/variants-sizes.test.ts @@ -1,5 +1,5 @@ import { test, expect } from "bun:test"; -import { computeImageVariantPlan } from "../variants"; +import { computeImageVariantPlan, pickSmallestVariantSize } from "../variants"; test("computeImageVariantPlan includes 256 and 768 thumbs", () => { expect(computeImageVariantPlan()).toEqual([ @@ -7,3 +7,11 @@ test("computeImageVariantPlan includes 256 and 768 thumbs", () => { { kind: "thumb", size: 768 }, ]); }); + +test("pickSmallestVariantSize returns smallest poster size", () => { + const size = pickSmallestVariantSize([ + { kind: "poster", size: 768 }, + { kind: "poster", size: 256 }, + ]); + expect(size).toBe(256); +}); diff --git a/apps/worker/src/jobs.ts b/apps/worker/src/jobs.ts index 6135f08..2f4465e 100644 --- a/apps/worker/src/jobs.ts +++ b/apps/worker/src/jobs.ts @@ -7,7 +7,11 @@ import { Readable } from "stream"; import sharp from "sharp"; -import { computeImageVariantPlan, computeVideoPosterPlan } from "./variants"; +import { + computeImageVariantPlan, + computeVideoPosterPlan, + pickSmallestVariantSize, +} from "./variants"; import { CopyObjectCommand, @@ -499,6 +503,7 @@ export async function handleProcessAsset(raw: unknown) { rawTags = { ...rawTags, ffprobe: ffprobeData }; const posterPlan = computeVideoPosterPlan(); + const posterSmallest = pickSmallestVariantSize(posterPlan); const posterKeys: Record = {}; for (const item of posterPlan) { const size = item.size; @@ -533,7 +538,7 @@ export async function handleProcessAsset(raw: unknown) { }); posterKeys[size] = posterKey; } - updates.poster_key = posterKeys[256] ?? null; + updates.poster_key = posterSmallest ? posterKeys[posterSmallest] ?? null : null; } if (asset.media_type === "video" && typeof updates.poster_key !== "string") { diff --git a/apps/worker/src/variants.ts b/apps/worker/src/variants.ts index 8e1296a..4559dc5 100644 --- a/apps/worker/src/variants.ts +++ b/apps/worker/src/variants.ts @@ -3,6 +3,11 @@ export type VariantPlanItem = { size: number; }; +export function pickSmallestVariantSize(plan: VariantPlanItem[]): number | null { + if (plan.length === 0) return null; + return plan.reduce((min, item) => (item.size < min ? item.size : min), plan[0].size); +} + export function computeImageVariantPlan(): VariantPlanItem[] { return [ { kind: "thumb", size: 256 },