Open History to see the last commit
1const PREFERRED_SPLAT_KEYS = ['500k', '100k', '150k', 'full_res'] as const
2
3export function pickSplatUrl(
4 spzUrls: Record<string, string> | undefined,
5): string | undefined {
6 if (!spzUrls) return undefined
7 for (const key of PREFERRED_SPLAT_KEYS) {
8 const url = spzUrls[key]
9 if (typeof url === 'string' && url.length > 0) return url
10 }
11 for (const url of Object.values(spzUrls)) {
12 if (url.length > 0) return url
13 }
14 return undefined
15}
16
17/** Same-origin proxy allowlist. Marble asset URLs are signed CDN links and are not CORS-open. */
18export function isAllowedMarbleSplatUrl(value: string): boolean {
19 let url: URL
20 try {
21 url = new URL(value)
22 } catch {
23 return false
24 }
25 if (url.protocol !== 'https:') return false
26 if (url.username.length > 0 || url.password.length > 0) return false
27 const host = url.hostname.toLowerCase()
28 return (
29 host === 'worldlabs.ai' ||
30 host.endsWith('.worldlabs.ai') ||
31 host === 'storage.googleapis.com' ||
32 host.endsWith('.storage.googleapis.com') ||
33 host.endsWith('.googleusercontent.com')
34 )
35}
36
37export function marbleSplatProxyPath(
38 splatUrl: string,
39 filename?: string,
40): string {
41 const params = new URLSearchParams({ url: splatUrl })
42 const safe = filename ? safeDownloadName(filename) : undefined
43 if (safe) params.set('filename', safe)
44 return `/api/marble-splat?${params.toString()}`
45}
46
47export function safeDownloadName(name: string): string | undefined {
48 return /^[A-Za-z0-9._-]{1,128}$/.test(name) ? name : undefined
49}
50
51export class MarblePlanError extends Error {
52 constructor(
53 message = 'This World Labs plan cannot export that file. Open the world in Marble, or upgrade the plan.',
54 ) {
55 super(message)
56 this.name = 'MarblePlanError'
57 }
58}
59
60export async function downloadMarbleAsset(
61 url: string,
62 filename: string,
63): Promise<void> {
64 const safe = safeDownloadName(filename) ?? splatFileName(url)
65 const response = await fetch(marbleSplatProxyPath(url, safe))
66 if (response.status === 402 || response.status === 403) {
67 throw new MarblePlanError()
68 }
69 if (!response.ok) {
70 throw new Error('Download failed')
71 }
72 const blob = await response.blob()
73 if (blob.size === 0) {
74 throw new Error('Download failed')
75 }
76 const objectUrl = URL.createObjectURL(blob)
77 try {
78 const link = document.createElement('a')
79 link.href = objectUrl
80 link.download = safe
81 document.body.appendChild(link)
82 link.click()
83 link.remove()
84 } finally {
85 URL.revokeObjectURL(objectUrl)
86 }
87}
88
89export function splatFileName(splatUrl: string): string {
90 try {
91 const name = new URL(splatUrl).pathname.split('/').pop()
92 if (name && /\.(spz|ply|splat|ksplat|sog|rad)$/i.test(name)) return name
93 } catch {
94 // Use the default name when the URL is opaque.
95 }
96 return 'world.spz'
97}