fix: revert some of the last changes
All checks were successful
Deploy to SFTP Server / build (push) Successful in 16m52s

This commit is contained in:
Max Richter
2025-10-24 19:44:42 +02:00
parent 71f778671d
commit 22781de3eb

View File

@@ -1,6 +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 { generateThumbHash, getImageBuffer, getExifData } from "@helpers/image"; import { generateThumbHash, getImageBuffer, getExifData } from "@helpers/image";
interface Props { interface Props {
src: ImageMetadata & { fsPath?: string; src?: string }; src: ImageMetadata & { fsPath?: string; src?: string };
@@ -13,6 +14,26 @@ interface Props {
maxWidth?: number; maxWidth?: number;
} }
async function checkImage(image: ImageMetadata) {
const src = typeof image === "string" ? image : image.src;
if (!src) return false;
try {
if (src.startsWith("/@fs") || src.startsWith("/_astro")) return true;
const res = await inferRemoteSize(src);
if (res.format) {
image.format = res.format;
return true;
} else {
console.log("Failed to load: ", src);
}
return false;
} catch (err) {
console.log("\n");
console.log("Failed to fetch: ", src);
return false;
}
}
const { const {
src: image, src: image,
loader = true, loader = true,
@@ -22,7 +43,10 @@ const {
maxWidth, maxWidth,
} = Astro.props; } = Astro.props;
const imageBuffer = await getImageBuffer(image);
const imageOk = await checkImage(image);
const imageBuffer = imageOk && await getImageBuffer(image);
let thumbhash = imageBuffer && (await generateThumbHash(imageBuffer)); let thumbhash = imageBuffer && (await generateThumbHash(imageBuffer));
let exif = imageBuffer && (await getExifData(imageBuffer)); let exif = imageBuffer && (await getExifData(imageBuffer));
@@ -45,19 +69,23 @@ const sizes = [
].filter((size) => !maxWidth || size.width <= maxWidth); ].filter((size) => !maxWidth || size.width <= maxWidth);
--- ---
<AstroImage {
src={image} imageOk ? (
alt={alt} <AstroImage
data-thumbhash={thumbhash} src={image}
data-exif={JSON.stringify(exif)} alt={alt}
inferSize={true} data-thumbhash={thumbhash}
pictureAttributes={{ data-exif={JSON.stringify(exif)}
class: `${hash ? "block h-full relative" : ""} ${loader ? "thumb" : ""} ${pictureClass}`, inferSize={true}
}} pictureAttributes={{
class={Astro.props.class} class: `${hash ? "block h-full relative" : ""} ${loader ? "thumb" : ""} ${pictureClass}`,
widths={sizes.map((size) => size.width)} }}
sizes={sizes class={Astro.props.class}
.map((size) => `${size.media || "100vw"} ${size.width}px`) widths={sizes.map((size) => size.width)}
.join(", ")}> sizes={sizes
<slot /> .map((size) => `${size.media || "100vw"} ${size.width}px`)
</AstroImage> .join(", ")}>
<slot />
</AstroImage>
) : undefined
}