fix: derive poster key from plan

This commit is contained in:
William Valentin
2026-02-01 14:16:30 -08:00
parent d6e6f275b7
commit 0bf2f2d827
3 changed files with 21 additions and 3 deletions

View File

@@ -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);
});

View File

@@ -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<number, string> = {};
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") {

View File

@@ -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 },