Files
otunes/apps/mobile/lib/urls.ts
T
joshuafhigginsandClaude Opus 5.5 Verified 886c8f0643 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 13:15:21 -05:00

84 lines
2.2 KiB
TypeScript

import {
type PlaybackQuality,
songHlsPlaylistPath,
songPlayPath,
} from "@otunes/convex/playback-stream"
import { Platform } from "react-native"
import type { AudioSource } from "expo-audio"
export const coverImageSizes = [64, 128, 192, 256, 384, 512, 768, 1024] as const
export type CoverImageSize = (typeof coverImageSizes)[number]
export function withCoverImageSize(uri: string, size: CoverImageSize) {
if (!uri.includes("?") && !uri.includes("#")) {
return `${uri}?size=${size}`
}
const url = new URL(uri)
url.searchParams.set("size", String(size))
return url.toString()
}
function sizedCoverUrl(uri: string, size?: CoverImageSize) {
return size ? withCoverImageSize(uri, size) : uri
}
export function artistCoverUrl(
serverUrl: string,
artistId: string,
size?: CoverImageSize
) {
return sizedCoverUrl(`${serverUrl}/api/artist/${artistId}/cover`, size)
}
export function albumCoverUrl(
serverUrl: string,
albumId: string,
size?: CoverImageSize
) {
return sizedCoverUrl(`${serverUrl}/api/album/${albumId}/cover`, size)
}
/** `version` is the summary's `cover`; it changes with the artwork. */
export function playlistCoverUrl(
serverUrl: string,
playlistId: string,
version: string | null,
size?: CoverImageSize
) {
const uri = `${serverUrl}/api/playlist/${playlistId}/cover`
return sizedCoverUrl(version ? `${uri}?v=${version}` : uri, size)
}
export function songAudioSource(
serverUrl: string,
songId: string,
quality: PlaybackQuality | undefined,
preferAtmos: boolean | undefined,
useHls: boolean,
options?: { cookieHeader?: string | null; songTitle?: string }
): AudioSource {
const path = useHls
? songHlsPlaylistPath(songId, quality, preferAtmos)
: songPlayPath(songId, quality, preferAtmos, {
losslessFormat: Platform.OS === "ios" ? "alac" : undefined,
})
const uri = `${serverUrl}${path}`
const headers = options?.cookieHeader
? { Cookie: options.cookieHeader }
: undefined
const name = options?.songTitle
if (headers || name || useHls) {
return {
uri,
...(useHls ? { mimeType: "application/x-mpegURL" } : {}),
...(name ? { name } : {}),
...(headers ? { headers } : {}),
}
}
return uri
}