Skip to content

Commit 333cd9e

Browse files
committed
Update image serice.
1 parent 6dadaae commit 333cd9e

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

src/image.service.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,25 @@ const service: LocalImageService = {
4040
let buffer = inputBuffer;
4141

4242
if (/^https?:\/\//.test(transform.src)) {
43-
const response = await fetch(transform.src);
44-
if (!response.ok) {
45-
throw new Error(`Failed to fetch image from ${transform.src}`);
43+
try {
44+
const response = await fetch(transform.src);
45+
if (!response.ok) {
46+
console.warn(
47+
`⚠️ Failed to fetch image: ${transform.src} (status ${response.status})`
48+
);
49+
return {
50+
data: buffer, // fallback to original input
51+
format: transform.format ?? ("webp" as OutputFormat),
52+
};
53+
}
54+
buffer = new Uint8Array(await response.arrayBuffer());
55+
} catch (err) {
56+
console.warn(`⚠️ Error fetching image from ${transform.src}:`, err);
57+
return {
58+
data: buffer, // fallback to original input
59+
format: transform.format ?? ("webp" as OutputFormat),
60+
};
4661
}
47-
buffer = new Uint8Array(await response.arrayBuffer());
4862
}
4963

5064
const sharp = (await import("sharp")).default;
@@ -62,12 +76,9 @@ const service: LocalImageService = {
6276

6377
const outputBuffer = await image.toBuffer();
6478

65-
// Convert Buffer to Uint8Array
66-
const uint8Array = Uint8Array.from(outputBuffer);
67-
6879
return {
69-
data: uint8Array,
70-
format: transform.format ?? ("webp" as OutputFormat), // Adjust the format here
80+
data: Uint8Array.from(outputBuffer),
81+
format: transform.format ?? ("webp" as OutputFormat),
7182
};
7283
},
7384

src/pages/speaker/[slug].astro

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ console.log(entry.data.avatar);
2020
2121
let avatar: any;
2222
23-
try {
24-
25-
avatar = getImage({ src: entry.data.avatar , width:600, height:400, alt: 'User avatar' });
26-
} catch (e) {
27-
//TODO: improve placeholders and offline
28-
//avatar = await getImage({ src: 'https://placehold.co/600x400?text=x', width: '600', height:'400', alt: 'Default avatar' });
29-
//avatar = await getImage({ src: '@assets/prague.png', width: '600', height:'400', alt: 'Default avatar' });
23+
if (entry.data.avatar){
24+
try {
25+
avatar = getImage({ src: entry.data.avatar , width:600, height:400, alt: 'User avatar' });
26+
} catch (e) {
27+
//TODO: improve placeholders and offline
28+
//avatar = await getImage({ src: 'https://placehold.co/600x400?text=x', width: '600', height:'400', alt: 'Default avatar' });
29+
//avatar = await getImage({ src: '@assets/prague.png', width: '600', height:'400', alt: 'Default avatar' });
30+
avatar = Placeholder;
31+
}
32+
} else {
3033
avatar = Placeholder;
3134
}
3235

0 commit comments

Comments
 (0)