45 lines
1.4 KiB
TypeScript
45 lines
1.4 KiB
TypeScript
import type { RequestHandler } from './$types';
|
|
|
|
export const GET: RequestHandler = async ({ url }) => {
|
|
const imageUrl = url.searchParams.get('url');
|
|
|
|
if (!imageUrl) {
|
|
return new Response('Image URL is required', { status: 400 });
|
|
}
|
|
|
|
try {
|
|
// Fetch the image with proper headers to avoid blocking
|
|
const response = await fetch(imageUrl, {
|
|
headers: {
|
|
'User-Agent':
|
|
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
|
'Accept': 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
|
|
'Accept-Language': 'en-US,en;q=0.9',
|
|
'Referer': new URL(imageUrl).origin,
|
|
'Sec-Fetch-Dest': 'image',
|
|
'Sec-Fetch-Mode': 'no-cors',
|
|
'Sec-Fetch-Site': 'cross-site'
|
|
}
|
|
});
|
|
|
|
if (!response.ok) {
|
|
return new Response('Failed to fetch image', { status: response.status });
|
|
}
|
|
|
|
const contentType = response.headers.get('content-type') || 'image/jpeg';
|
|
const imageBuffer = await response.arrayBuffer();
|
|
|
|
// Return the image with appropriate headers
|
|
return new Response(imageBuffer, {
|
|
headers: {
|
|
'Content-Type': contentType,
|
|
'Cache-Control': 'public, max-age=86400', // Cache for 1 day
|
|
'Access-Control-Allow-Origin': '*'
|
|
}
|
|
});
|
|
} catch (error) {
|
|
console.error('Image proxy error:', error);
|
|
return new Response('Failed to proxy image', { status: 500 });
|
|
}
|
|
};
|