Changing sources to remoteImage

This commit is contained in:
Eclypsed
2024-04-09 14:22:14 -04:00
parent 998dc81143
commit 8cff75bc9e
4 changed files with 34 additions and 14 deletions

View File

@@ -1,13 +1,27 @@
import type { RequestHandler } from '@sveltejs/kit'
// This endpoint exists to act as a proxy for images, bypassing any CORS or other issues
// that could arise from using images from another origin
export const GET: RequestHandler = async ({ url }) => {
// const connectionId = url.searchParams.get('connection')
// const id = url.searchParams.get('id')
// if (!(connectionId && id)) return new Response('Missing query parameter', { status: 400 })
const imageUrl = url.searchParams.get('url')
if (!imageUrl) return new Response('Missing url', { status: 400 })
if (!imageUrl || !URL.canParse(imageUrl)) return new Response('Missing or invalid url parameter', { status: 400 })
const image = await fetch(imageUrl).then((response) => response.arrayBuffer())
const MAX_TRIES = 3
return new Response(image)
const fetchImage = async (): Promise<ArrayBuffer> => {
let tryCount = 0
while (tryCount < MAX_TRIES) {
++tryCount
try {
return await fetch(imageUrl).then((response) => response.arrayBuffer())
} catch (error) {
console.error(error)
continue
}
}
throw new Error('Exceed Max Retires')
}
return new Response(await fetchImage())
}