your personal website on atproto - mirror blento.app
at mobile-editing 44 lines 962 B view raw
1import { error } from '@sveltejs/kit'; 2 3export async function GET({ url }) { 4 const imageUrl = url.searchParams.get('url'); 5 if (!imageUrl) { 6 throw error(400, 'No URL provided'); 7 } 8 9 try { 10 new URL(imageUrl); 11 } catch { 12 throw error(400, 'Invalid URL'); 13 } 14 15 try { 16 const response = await fetch(imageUrl); 17 18 if (!response.ok) { 19 throw error(response.status, 'Failed to fetch image'); 20 } 21 22 const contentType = response.headers.get('content-type'); 23 24 // Only allow image content types 25 if (!contentType?.startsWith('image/')) { 26 throw error(400, 'URL does not point to an image'); 27 } 28 29 const blob = await response.blob(); 30 31 return new Response(blob, { 32 headers: { 33 'Content-Type': contentType, 34 'Cache-Control': 'public, max-age=86400' 35 } 36 }); 37 } catch (err) { 38 if (err && typeof err === 'object' && 'status' in err) { 39 throw err; 40 } 41 console.error('Error proxying image:', err); 42 throw error(500, 'Failed to proxy image'); 43 } 44}