Files
otunes/apps/web/lib/convex-server.ts
T
joshuafhigginsandClaude Opus 5.5 Verified 140dddc383 feat: playlist cover artwork
Playlists draw a 2x2 of their first albums automatically. Owners can pick
from random grids, reshuffle, or upload an image on web and mobile.
Covers are rendered once into Convex storage and redrawn only when the
playlist's albums change. Web and iPad sidebars show playlist covers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 18:02:10 -05:00

38 lines
1.1 KiB
TypeScript

import { ConvexHttpClient } from "convex/browser"
let client: ConvexHttpClient | null = null
/**
* URL the Next.js server uses to reach Convex. Never derive this from request
* headers: auth tokens are sent to it, so a forged Host would leak them.
*/
export function getConvexServerUrl() {
return (
process.env.CONVEX_INTERNAL_URL ??
process.env.NEXT_PUBLIC_CONVEX_URL ??
"http://127.0.0.1:3210"
)
}
export function convexServerOptions(token?: string) {
return { url: getConvexServerUrl(), token }
}
export function getConvexClient() {
if (!client) {
client = new ConvexHttpClient(getConvexServerUrl())
}
return client
}
/**
* Downloads a Convex storage file. The backend builds storage URLs from its
* public origin, which may not be reachable from here, so the same path is
* fetched from the internal URL. `null` when it is missing.
*/
export async function fetchConvexStorageFile(storageUrl: string) {
const { pathname, search } = new URL(storageUrl)
const response = await fetch(new URL(pathname + search, getConvexServerUrl()))
return response.ok ? Buffer.from(await response.arrayBuffer()) : null
}