feat(web): serve sized media session artwork

- Add typed cover image size parameters
- Request appropriately sized artwork variants for Media Session
This commit is contained in:
remhelper committed 2026-08-19 11:39:58 -04:00 Verified
1 parent 600b01d20e
commit 58b2d27e6d
2 files changed
+32 -16

No files matched your search

+20 -10
View File
@@ -3,7 +3,10 @@
import * as React from "react"
import type { BrowseSong } from "@otunes/convex/types"
import { albumCoverUrl } from "@/lib/cover-urls"
import {
albumCoverUrl,
type CoverImageSize,
} from "@/lib/cover-urls"
const MEDIA_SESSION_ACTIONS = [
"play",
@@ -13,6 +16,15 @@ const MEDIA_SESSION_ACTIONS = [
"seekto",
] as const
const MEDIA_SESSION_ARTWORK_SIZES = [
64,
128,
192,
256,
384,
512,
] as const satisfies readonly CoverImageSize[]
function mediaSessionSupported() {
return typeof navigator !== "undefined" && "mediaSession" in navigator
}
@@ -29,15 +41,13 @@ function setActionHandler(
}
function artworkForSong(song: BrowseSong): MediaImage[] {
const src = new URL(albumCoverUrl(song.album.id), window.location.origin).href
return [
{ src, sizes: "96x96" },
{ src, sizes: "128x128" },
{ src, sizes: "192x192" },
{ src, sizes: "256x256" },
{ src, sizes: "384x384" },
{ src, sizes: "512x512" },
]
return MEDIA_SESSION_ARTWORK_SIZES.map((size) => ({
src: new URL(
albumCoverUrl(song.album.id, size),
window.location.origin
).href,
sizes: `${size}x${size}`,
}))
}
function updatePositionState(duration: number, position: number) {
+12 -6
View File
@@ -1,11 +1,17 @@
export function artistCoverUrl(artistId: string) {
return `/api/artist/${artistId}/cover`
export type CoverImageSize = 64 | 128 | 192 | 256 | 384 | 512 | 768 | 1024
function withCoverImageSize(uri: string, size?: CoverImageSize) {
return size ? `${uri}?size=${size}` : uri
}
export function albumCoverUrl(albumId: string) {
return `/api/album/${albumId}/cover`
export function artistCoverUrl(artistId: string, size?: CoverImageSize) {
return withCoverImageSize(`/api/artist/${artistId}/cover`, size)
}
export function playlistCoverUrl(playlistId: string) {
return `/api/playlist/${playlistId}/cover`
export function albumCoverUrl(albumId: string, size?: CoverImageSize) {
return withCoverImageSize(`/api/album/${albumId}/cover`, size)
}
export function playlistCoverUrl(playlistId: string, size?: CoverImageSize) {
return withCoverImageSize(`/api/playlist/${playlistId}/cover`, size)
}