fix: derive poster key from plan
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { test, expect } from "bun:test";
|
import { test, expect } from "bun:test";
|
||||||
import { computeImageVariantPlan } from "../variants";
|
import { computeImageVariantPlan, pickSmallestVariantSize } from "../variants";
|
||||||
|
|
||||||
test("computeImageVariantPlan includes 256 and 768 thumbs", () => {
|
test("computeImageVariantPlan includes 256 and 768 thumbs", () => {
|
||||||
expect(computeImageVariantPlan()).toEqual([
|
expect(computeImageVariantPlan()).toEqual([
|
||||||
@@ -7,3 +7,11 @@ test("computeImageVariantPlan includes 256 and 768 thumbs", () => {
|
|||||||
{ kind: "thumb", size: 768 },
|
{ 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);
|
||||||
|
});
|
||||||
|
|||||||
@@ -7,7 +7,11 @@ import { Readable } from "stream";
|
|||||||
|
|
||||||
import sharp from "sharp";
|
import sharp from "sharp";
|
||||||
|
|
||||||
import { computeImageVariantPlan, computeVideoPosterPlan } from "./variants";
|
import {
|
||||||
|
computeImageVariantPlan,
|
||||||
|
computeVideoPosterPlan,
|
||||||
|
pickSmallestVariantSize,
|
||||||
|
} from "./variants";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
CopyObjectCommand,
|
CopyObjectCommand,
|
||||||
@@ -499,6 +503,7 @@ export async function handleProcessAsset(raw: unknown) {
|
|||||||
rawTags = { ...rawTags, ffprobe: ffprobeData };
|
rawTags = { ...rawTags, ffprobe: ffprobeData };
|
||||||
|
|
||||||
const posterPlan = computeVideoPosterPlan();
|
const posterPlan = computeVideoPosterPlan();
|
||||||
|
const posterSmallest = pickSmallestVariantSize(posterPlan);
|
||||||
const posterKeys: Record<number, string> = {};
|
const posterKeys: Record<number, string> = {};
|
||||||
for (const item of posterPlan) {
|
for (const item of posterPlan) {
|
||||||
const size = item.size;
|
const size = item.size;
|
||||||
@@ -533,7 +538,7 @@ export async function handleProcessAsset(raw: unknown) {
|
|||||||
});
|
});
|
||||||
posterKeys[size] = posterKey;
|
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") {
|
if (asset.media_type === "video" && typeof updates.poster_key !== "string") {
|
||||||
|
|||||||
@@ -3,6 +3,11 @@ export type VariantPlanItem = {
|
|||||||
size: number;
|
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[] {
|
export function computeImageVariantPlan(): VariantPlanItem[] {
|
||||||
return [
|
return [
|
||||||
{ kind: "thumb", size: 256 },
|
{ kind: "thumb", size: 256 },
|
||||||
|
|||||||
Reference in New Issue
Block a user