frontend/src/utils/imgproxy.ts

34 lines
967 B
TypeScript

export const buildImgproxyUrl = (sourceUrl: string, width?: number): string => {
const baseUrl = import.meta.env.VITE_IMGPROXY_BASE_URL;
if (!baseUrl) {
// Fallback to original URL logic
const originalBase = import.meta.env.VITE_OMV_BASE_URL;
if (sourceUrl.startsWith("http")) {
return sourceUrl;
}
return `${originalBase}/${sourceUrl}`;
}
// rs:fit:${width}:0:0 to resize preserving aspect ratio, capping width at `width`
// If width is provided, we resize, otherwise we keep original dimensions.
const parts = [];
if (width) {
parts.push(`rs:fit:${width}:0:0`);
}
parts.push("f:avif");
const options = parts.join("/");
return `${baseUrl}/insecure/${options}/plain/s3://${import.meta.env.VITE_S3_BUCKET}/${sourceUrl}`;
};
export const generateSrcSet = (
sourceUrl: string,
widths: number[] = [400, 800, 1200, 1800],
): string => {
return widths
.map((width) => `${buildImgproxyUrl(sourceUrl, width)} ${width}w`)
.join(", ");
};