mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-11-15 05:36:11 +01:00
feat: imageProxy
This commit is contained in:
1 parent
6f47f5cc99
commit
1598de9f5f
5 files changed
+29
-16
No files matched your search
@@ -0,0 +1,23 @@
|
||||
import sharp from "sharp";
|
||||
|
||||
export async function imageProxy(url: string) {
|
||||
if (import.meta.env.DEV) {
|
||||
return Response.redirect(url);
|
||||
}
|
||||
|
||||
let response: Response;
|
||||
|
||||
try {
|
||||
response = await fetch(url);
|
||||
} catch (error) {
|
||||
console.warn(`Failed to fetch image proxy ${url}:`, error);
|
||||
return new Response(null, { status: 404 });
|
||||
}
|
||||
|
||||
const buffer = await response.arrayBuffer();
|
||||
const image = sharp(buffer);
|
||||
const webp = await image.webp().toBuffer();
|
||||
const array = new Uint8Array(webp);
|
||||
|
||||
return new Response(array);
|
||||
}
|
||||
Reference in new issue
Block a user