Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

api.marble-splat.ts

47 lines1.6 KB

1import { createFileRoute } from '@tanstack/react-router'
2import { isAllowedMarbleSplatUrl, safeDownloadName } from '@/lib/marble-splat'
3​
4async function proxySplat(request: Request): Promise<Response> {
5 const query = new URL(request.url).searchParams
6 const target = query.get('url')
7 if (!target || !isAllowedMarbleSplatUrl(target)) {
8 return Response.json({ error: 'Invalid splat URL' }, { status: 400 })
9 }
10​
11 const upstream = await fetch(target, { redirect: 'follow' })
12 if (!isAllowedMarbleSplatUrl(upstream.url)) {
13 return Response.json({ error: 'Invalid splat URL' }, { status: 400 })
14 }
15 if (!upstream.ok || !upstream.body) {
16 const status = upstream.status || 502
17 if (status === 402 || status === 403) {
18 return Response.json(
19 { error: 'This World Labs plan cannot export that file' },
20 { status },
21 )
22 }
23 return Response.json({ error: 'Splat download failed' }, { status })
24 }
25​
26 const headers = new Headers()
27 headers.set(
28 'content-type',
29 upstream.headers.get('content-type') ?? 'application/octet-stream',
30 )
31 const length = upstream.headers.get('content-length')
32 if (length) headers.set('content-length', length)
33 headers.set('cache-control', 'private, max-age=3600')
34 const filename = safeDownloadName(query.get('filename') ?? '')
35 if (filename) {
36 headers.set('content-disposition', `attachment; filename="${filename}"`)
37 }
38 return new Response(upstream.body, { status: 200, headers })
40​
41export const Route = createFileRoute('/api/marble-splat')({
42 server: {
43 handlers: {
44 GET: ({ request }) => proxySplat(request),
45 },
46 },
47})