fix: actually check image sizes
Some checks failed
Deploy to SFTP Server / build (push) Failing after 2m24s

This commit is contained in:
Max Richter
2025-10-28 15:35:43 +01:00
parent 71074a8b49
commit c3299868c0

View File

@@ -1,7 +1,7 @@
--- ---
import type { ImageMetadata } from "astro"; import type { ImageMetadata } from "astro";
import { Picture as AstroImage } from "astro:assets"; import { Picture as AstroImage } from "astro:assets";
import { inferRemoteSize } from "astro/assets/utils"; import { inferRemoteSize, inferSize } from "astro/assets/utils";
import { getProcessedImage } from "@helpers/image"; import { getProcessedImage } from "@helpers/image";
interface Props { interface Props {
src: ImageMetadata & { fsPath?: string; src?: string }; src: ImageMetadata & { fsPath?: string; src?: string };
@@ -15,11 +15,13 @@ interface Props {
thumbnail?: boolean; thumbnail?: boolean;
} }
async function checkImage(image: ImageMetadata):Promise<({height:number,width:number}|undefined)> { async function checkImage(
image: ImageMetadata,
): Promise<{ height: number; width: number } | undefined> {
const src = typeof image === "string" ? image : image.src; const src = typeof image === "string" ? image : image.src;
if (!src) return ; if (!src) return;
try { try {
if (src.startsWith("/@fs") || src.startsWith("/_astro")) return; if (src.startsWith("/@fs") || src.startsWith("/_astro")) return {};
const res = await inferRemoteSize(src); const res = await inferRemoteSize(src);
if (res.format) { if (res.format) {
image.format = res.format; image.format = res.format;
@@ -27,11 +29,11 @@ async function checkImage(image: ImageMetadata):Promise<({height:number,width:nu
} else { } else {
console.log("Failed to load: ", src); console.log("Failed to load: ", src);
} }
return ; return;
} catch (err) { } catch (err) {
console.log("\n"); console.log("\n");
console.log("Failed to fetch: ", src); console.log("Failed to fetch: ", src);
return ; return;
} }
} }
@@ -81,11 +83,10 @@ const sizes = thumbnail
alt={alt} alt={alt}
data-thumbhash={thumbhash} data-thumbhash={thumbhash}
data-exif={JSON.stringify(exif)} data-exif={JSON.stringify(exif)}
inferSize={true}
width={imageOk?.width} width={imageOk?.width}
height={imageOk?.height} height={imageOk?.height}
pictureAttributes={{ pictureAttributes={{
class: `${hash ? "block h-full relative" : ""} ${loader ? "thumb" : ""} ${pictureClass}`, class: `${hash ? "block h-full relative" : ""} ${loader ? "thumb" : ""} ${pictureClass}`,
}} }}
class={`${Astro.props.class} h-full w-full`} class={`${Astro.props.class} h-full w-full`}
widths={sizes.map((size) => size.width)} widths={sizes.map((size) => size.width)}
@@ -94,5 +95,7 @@ const sizes = thumbnail
.join(", ")}> .join(", ")}>
<slot /> <slot />
</AstroImage> </AstroImage>
) : <div>{JSON.stringify({ image })}</div> ) : (
<div>{JSON.stringify({ "imageOk":imageOk, image })}</div>
)
} }