fix: revert some of the last changes
All checks were successful
Deploy to SFTP Server / build (push) Successful in 18m18s
All checks were successful
Deploy to SFTP Server / build (push) Successful in 18m18s
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
---
|
||||
import type { ImageMetadata } from "astro";
|
||||
import { Picture as AstroImage } from "astro:assets";
|
||||
import { inferRemoteSize } from "astro/assets/utils";
|
||||
import { generateThumbHash, getImageBuffer, getExifData } from "@helpers/image";
|
||||
interface Props {
|
||||
src: ImageMetadata & { fsPath?: string; src?: string };
|
||||
@@ -13,6 +14,26 @@ interface Props {
|
||||
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 {
|
||||
src: image,
|
||||
loader = true,
|
||||
@@ -22,7 +43,10 @@ const {
|
||||
maxWidth,
|
||||
} = 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 exif = imageBuffer && (await getExifData(imageBuffer));
|
||||
|
||||
@@ -45,19 +69,23 @@ const sizes = [
|
||||
].filter((size) => !maxWidth || size.width <= maxWidth);
|
||||
---
|
||||
|
||||
<AstroImage
|
||||
src={image}
|
||||
alt={alt}
|
||||
data-thumbhash={thumbhash}
|
||||
data-exif={JSON.stringify(exif)}
|
||||
inferSize={true}
|
||||
pictureAttributes={{
|
||||
class: `${hash ? "block h-full relative" : ""} ${loader ? "thumb" : ""} ${pictureClass}`,
|
||||
}}
|
||||
class={Astro.props.class}
|
||||
widths={sizes.map((size) => size.width)}
|
||||
sizes={sizes
|
||||
.map((size) => `${size.media || "100vw"} ${size.width}px`)
|
||||
.join(", ")}>
|
||||
<slot />
|
||||
</AstroImage>
|
||||
{
|
||||
imageOk ? (
|
||||
<AstroImage
|
||||
src={image}
|
||||
alt={alt}
|
||||
data-thumbhash={thumbhash}
|
||||
data-exif={JSON.stringify(exif)}
|
||||
inferSize={true}
|
||||
pictureAttributes={{
|
||||
class: `${hash ? "block h-full relative" : ""} ${loader ? "thumb" : ""} ${pictureClass}`,
|
||||
}}
|
||||
class={Astro.props.class}
|
||||
widths={sizes.map((size) => size.width)}
|
||||
sizes={sizes
|
||||
.map((size) => `${size.media || "100vw"} ${size.width}px`)
|
||||
.join(", ")}>
|
||||
<slot />
|
||||
</AstroImage>
|
||||
) : undefined
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user