Skip to content

Commit

Permalink
Fix preview size calculation for undefined target sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmz committed Jan 25, 2025
1 parent 042f516 commit d44ab0a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
15 changes: 13 additions & 2 deletions app/support/media-files/geometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,25 @@ export function getVideoPreviewSizes(
*/
export function getBestVariant(
variants: { [variant: string]: { w: number; h: number } },
targetWidth: number,
targetHeight: number,
targetWidth?: number,
targetHeight?: number,
): {
variant: string;
width: number;
height: number;
} {
const entries = Object.entries(variants).sort((a, b) => a[1].w - b[1].w); // Sort by ascending size

// If no dimensions are required, use the maximum available variant
if (!targetWidth || !targetHeight) {
const bestEntry = entries[entries.length - 1];
return {
variant: bestEntry[0],
width: bestEntry[1].w,
height: bestEntry[1].h,
};
}

const bestEntry =
entries.find(([, { w, h }]) => w >= targetWidth && h >= targetHeight) ??
entries[entries.length - 1]; // Or the last entry as the biggest one
Expand Down
5 changes: 5 additions & 0 deletions test/unit/support/media-files/geometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ describe('getBestVariant', () => {
height: 400,
result: { variant: 'x3', width: 300, height: 300 }, // Don't upscale
},
{
width: undefined,
height: undefined,
result: { variant: 'x3', width: 300, height: 600 }, // Max variant
},
] as const;

for (const { width, height, result } of testData) {
Expand Down

0 comments on commit d44ab0a

Please sign in to comment.