fix: make image loading more robust
All checks were successful
Deploy to SFTP Server / build (push) Successful in 6m38s
All checks were successful
Deploy to SFTP Server / build (push) Successful in 6m38s
This commit is contained in:
@@ -2,6 +2,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 { generateThumbHash, getExifData } from "@helpers/image";
|
import { generateThumbHash, getExifData } from "@helpers/image";
|
||||||
|
import sharp from "sharp";
|
||||||
interface Props {
|
interface Props {
|
||||||
src: ImageMetadata & { fsPath?: string };
|
src: ImageMetadata & { fsPath?: string };
|
||||||
alt: string;
|
alt: string;
|
||||||
@@ -16,12 +17,12 @@ interface Props {
|
|||||||
async function checkImage(src: string) {
|
async function checkImage(src: string) {
|
||||||
try {
|
try {
|
||||||
if (src.startsWith("/@fs") || src.startsWith("/_astro")) return true;
|
if (src.startsWith("/@fs") || src.startsWith("/_astro")) return true;
|
||||||
const res = await fetch(src);
|
const res = await sharp(src).metadata();
|
||||||
if (res.ok) {
|
if (res.format) {
|
||||||
return true;
|
return true;
|
||||||
}else {
|
}else {
|
||||||
console.log("Failed to fetch: ", src);
|
console.log("Failed to fetch: ", src);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log("Failed to fetch: ", src);
|
console.log("Failed to fetch: ", src);
|
||||||
|
|||||||
Reference in New Issue
Block a user