feat: playlist cover artwork (#117)
This commit was merged in pull request #117.
This commit is contained in:
1 parent
76409b9c3e
commit
4d50a49920
34 files changed
+1459
-92
No files matched your search
@@ -68,6 +68,7 @@ const OWNER_MENU_ACTIONS: MediaAction[] = [
|
|||||||
"play-last",
|
"play-last",
|
||||||
"shuffle",
|
"shuffle",
|
||||||
"favorite",
|
"favorite",
|
||||||
|
"change-cover",
|
||||||
"delete-playlist",
|
"delete-playlist",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -83,7 +84,11 @@ export default function PlaylistScreen() {
|
|||||||
const songs = playlist?.songs
|
const songs = playlist?.songs
|
||||||
const coverUri =
|
const coverUri =
|
||||||
config && playlistId
|
config && playlistId
|
||||||
? playlistCoverUrl(config.serverUrl, playlist?.id ?? playlistId)
|
? playlistCoverUrl(
|
||||||
|
config.serverUrl,
|
||||||
|
playlist?.id ?? playlistId,
|
||||||
|
playlist?.cover ?? null
|
||||||
|
)
|
||||||
: ""
|
: ""
|
||||||
const { palette: artworkPalette, settled } = useCoverPalette(
|
const { palette: artworkPalette, settled } = useCoverPalette(
|
||||||
coverUri,
|
coverUri,
|
||||||
@@ -261,6 +266,15 @@ function PlaylistContent({
|
|||||||
void handleDelete()
|
void handleDelete()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if (action === "change-cover") {
|
||||||
|
if (playlistId) {
|
||||||
|
router.push({
|
||||||
|
pathname: "/playlist-cover",
|
||||||
|
params: { id: playlistId },
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
void handlePlaylistDetailAction(songIds, action)
|
void handlePlaylistDetailAction(songIds, action)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -106,6 +106,22 @@ export default function AppLayout() {
|
|||||||
contentStyle: { backgroundColor: "#000" },
|
contentStyle: { backgroundColor: "#000" },
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
<Stack.Screen
|
||||||
|
name="playlist-cover"
|
||||||
|
options={{
|
||||||
|
title: "Cover",
|
||||||
|
headerTitleAlign: "center",
|
||||||
|
presentation:
|
||||||
|
Platform.OS === "ios" ? "modal" : "fullScreenModal",
|
||||||
|
animation: "slide_from_bottom",
|
||||||
|
headerLargeTitle: false,
|
||||||
|
headerTransparent: false,
|
||||||
|
headerShadowVisible: false,
|
||||||
|
headerStyle: { backgroundColor: "#000" },
|
||||||
|
headerTintColor: "#fff",
|
||||||
|
contentStyle: { backgroundColor: "#000" },
|
||||||
|
}}
|
||||||
|
/>
|
||||||
<Stack.Screen
|
<Stack.Screen
|
||||||
name="player"
|
name="player"
|
||||||
options={{
|
options={{
|
||||||
|
|||||||
@@ -0,0 +1,391 @@
|
|||||||
|
import { useMutation } from "convex/react"
|
||||||
|
import { Image } from "expo-image"
|
||||||
|
import * as ImagePicker from "expo-image-picker"
|
||||||
|
import { router, Stack, useLocalSearchParams } from "expo-router"
|
||||||
|
import { useMemo, useState, type ReactNode } from "react"
|
||||||
|
import {
|
||||||
|
Pressable,
|
||||||
|
ScrollView,
|
||||||
|
StyleSheet,
|
||||||
|
Text,
|
||||||
|
useWindowDimensions,
|
||||||
|
View,
|
||||||
|
} from "react-native"
|
||||||
|
|
||||||
|
import { CoverImage } from "@/components/cover-image"
|
||||||
|
import { Button, LoadingView } from "@/components/ui/primitives"
|
||||||
|
import { useServerConfig } from "@/contexts/server-config"
|
||||||
|
import { useCachedQuery } from "@/hooks/use-cached-query"
|
||||||
|
import { showError } from "@/lib/toast"
|
||||||
|
import { albumCoverUrl, playlistCoverUrl } from "@/lib/urls"
|
||||||
|
import { radius, spacing, useThemeColors } from "@/lib/theme"
|
||||||
|
import { api } from "@otunes/convex/_generated/api"
|
||||||
|
import type { Id } from "@otunes/convex/_generated/dataModel"
|
||||||
|
import {
|
||||||
|
COVER_GRID_ALBUMS,
|
||||||
|
playlistCoverAlbums,
|
||||||
|
randomPlaylistCovers,
|
||||||
|
} from "@otunes/convex/client/playlist-cover"
|
||||||
|
|
||||||
|
type Choice =
|
||||||
|
| { kind: "current" }
|
||||||
|
| { kind: "albums"; albumIds: Id<"albums">[] }
|
||||||
|
| { kind: "upload"; uri: string; image: string }
|
||||||
|
|
||||||
|
const STRIP_GAP = spacing.sm
|
||||||
|
/** Selection border and padding around each thumb, both sides. */
|
||||||
|
const THUMB_CHROME = 6
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Full screen cover editor for a playlist owner: a large preview of the
|
||||||
|
* selected cover, the current cover pinned apart from four random album
|
||||||
|
* grids, and Save in the header. Opened from the playlist's menu.
|
||||||
|
*/
|
||||||
|
export default function PlaylistCoverScreen() {
|
||||||
|
const { id } = useLocalSearchParams<{ id: string }>()
|
||||||
|
const playlistId = id as Id<"playlists">
|
||||||
|
const playlist = useCachedQuery(api.playlists.browsePlaylistDetail, {
|
||||||
|
id: playlistId,
|
||||||
|
})
|
||||||
|
const { config } = useServerConfig()
|
||||||
|
const colors = useThemeColors()
|
||||||
|
const { width } = useWindowDimensions()
|
||||||
|
const setCoverAlbums = useMutation(api.playlists.setPlaylistCoverAlbums)
|
||||||
|
const uploadCover = useMutation(api.playlists.uploadPlaylistCover)
|
||||||
|
|
||||||
|
const albums = useMemo(
|
||||||
|
() => (playlist ? playlistCoverAlbums(playlist.songs) : []),
|
||||||
|
[playlist]
|
||||||
|
)
|
||||||
|
const [options, setOptions] = useState<Id<"albums">[][] | null>(null)
|
||||||
|
const [selected, setSelected] = useState<Choice | null>(null)
|
||||||
|
const [saving, setSaving] = useState(false)
|
||||||
|
|
||||||
|
// Draw options once the songs arrive.
|
||||||
|
if (options === null && playlist) {
|
||||||
|
setOptions(randomPlaylistCovers(albums))
|
||||||
|
}
|
||||||
|
|
||||||
|
const hasCurrent = playlist?.cover != null
|
||||||
|
const choice: Choice | null =
|
||||||
|
selected ??
|
||||||
|
(hasCurrent
|
||||||
|
? { kind: "current" }
|
||||||
|
: options?.[0]
|
||||||
|
? { kind: "albums", albumIds: options[0] }
|
||||||
|
: null)
|
||||||
|
|
||||||
|
async function pickImage() {
|
||||||
|
const result = await ImagePicker.launchImageLibraryAsync({
|
||||||
|
mediaTypes: ["images"],
|
||||||
|
allowsEditing: true,
|
||||||
|
aspect: [1, 1],
|
||||||
|
quality: 0.7,
|
||||||
|
base64: true,
|
||||||
|
})
|
||||||
|
const asset = result.canceled ? undefined : result.assets[0]
|
||||||
|
if (!asset?.base64) return
|
||||||
|
setSelected({
|
||||||
|
kind: "upload",
|
||||||
|
uri: asset.uri,
|
||||||
|
image: `data:${asset.mimeType ?? "image/jpeg"};base64,${asset.base64}`,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async function save() {
|
||||||
|
if (!choice || choice.kind === "current") {
|
||||||
|
router.back()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
setSaving(true)
|
||||||
|
try {
|
||||||
|
if (choice.kind === "albums") {
|
||||||
|
await setCoverAlbums({ playlistId, albumIds: choice.albumIds })
|
||||||
|
} else {
|
||||||
|
await uploadCover({ playlistId, image: choice.image })
|
||||||
|
}
|
||||||
|
router.back()
|
||||||
|
} catch (error) {
|
||||||
|
showError(
|
||||||
|
error instanceof Error ? error.message : "Failed to change cover"
|
||||||
|
)
|
||||||
|
setSaving(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const header = (
|
||||||
|
<Stack.Screen
|
||||||
|
options={{
|
||||||
|
headerLeft: () => (
|
||||||
|
<HeaderButton label="Cancel" onPress={() => router.back()} />
|
||||||
|
),
|
||||||
|
headerRight: () => (
|
||||||
|
<HeaderButton
|
||||||
|
label="Save"
|
||||||
|
bold
|
||||||
|
disabled={saving || !choice}
|
||||||
|
onPress={() => void save()}
|
||||||
|
/>
|
||||||
|
),
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
|
||||||
|
if (!playlist || !config || !options) {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{header}
|
||||||
|
<LoadingView />
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const serverUrl = config.serverUrl
|
||||||
|
const currentUri = playlistCoverUrl(serverUrl, playlistId, playlist.cover)
|
||||||
|
const previewSize = Math.min(width - spacing.lg * 2, 360)
|
||||||
|
// One row: current | uploaded? | options, thumbs at most 72pt.
|
||||||
|
const thumbCount =
|
||||||
|
options.length + (hasCurrent ? 1 : 0) + (choice?.kind === "upload" ? 1 : 0)
|
||||||
|
const rowItems = thumbCount + (hasCurrent ? 1 : 0)
|
||||||
|
const thumbSize = Math.min(
|
||||||
|
72,
|
||||||
|
Math.floor(
|
||||||
|
(width - spacing.lg * 2 - STRIP_GAP * (rowItems - 1) - 1) /
|
||||||
|
Math.max(thumbCount, 1)
|
||||||
|
) - THUMB_CHROME
|
||||||
|
)
|
||||||
|
|
||||||
|
function renderChoice(item: Choice, size: number) {
|
||||||
|
if (item.kind === "current") {
|
||||||
|
return (
|
||||||
|
<CoverImage
|
||||||
|
uri={currentUri}
|
||||||
|
hasCover
|
||||||
|
style={{ width: size, height: size }}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if (item.kind === "upload") {
|
||||||
|
return (
|
||||||
|
<Image
|
||||||
|
source={{ uri: item.uri }}
|
||||||
|
style={{ width: size, height: size }}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<AlbumGrid serverUrl={serverUrl} albumIds={item.albumIds} size={size} />
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const isSelected = (item: Choice) =>
|
||||||
|
choice?.kind === item.kind &&
|
||||||
|
(item.kind !== "albums" ||
|
||||||
|
(choice.kind === "albums" && choice.albumIds === item.albumIds))
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ScrollView
|
||||||
|
style={{ backgroundColor: colors.background }}
|
||||||
|
contentContainerStyle={styles.content}
|
||||||
|
>
|
||||||
|
{header}
|
||||||
|
<View
|
||||||
|
style={[
|
||||||
|
styles.preview,
|
||||||
|
{
|
||||||
|
width: previewSize,
|
||||||
|
height: previewSize,
|
||||||
|
backgroundColor: colors.coverPlaceholder,
|
||||||
|
},
|
||||||
|
]}
|
||||||
|
>
|
||||||
|
{choice ? renderChoice(choice, previewSize) : null}
|
||||||
|
</View>
|
||||||
|
|
||||||
|
<View style={styles.strip}>
|
||||||
|
{hasCurrent ? (
|
||||||
|
<>
|
||||||
|
<Thumb
|
||||||
|
size={thumbSize}
|
||||||
|
selected={isSelected({ kind: "current" })}
|
||||||
|
label="Current cover"
|
||||||
|
onPress={() => setSelected({ kind: "current" })}
|
||||||
|
>
|
||||||
|
{renderChoice({ kind: "current" }, thumbSize)}
|
||||||
|
</Thumb>
|
||||||
|
<View
|
||||||
|
style={[styles.separator, { backgroundColor: colors.border }]}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
) : null}
|
||||||
|
{choice?.kind === "upload" ? (
|
||||||
|
<Thumb size={thumbSize} selected label="Uploaded image">
|
||||||
|
{renderChoice(choice, thumbSize)}
|
||||||
|
</Thumb>
|
||||||
|
) : null}
|
||||||
|
{options.map((albumIds) => {
|
||||||
|
const item: Choice = { kind: "albums", albumIds }
|
||||||
|
return (
|
||||||
|
<Thumb
|
||||||
|
key={albumIds.join(",")}
|
||||||
|
size={thumbSize}
|
||||||
|
selected={isSelected(item)}
|
||||||
|
label="Album cover option"
|
||||||
|
onPress={() => setSelected(item)}
|
||||||
|
>
|
||||||
|
{renderChoice(item, thumbSize)}
|
||||||
|
</Thumb>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
</View>
|
||||||
|
|
||||||
|
{albums.length === 0 ? (
|
||||||
|
<Text style={{ color: colors.mutedForeground, textAlign: "center" }}>
|
||||||
|
No album artwork in this playlist yet.
|
||||||
|
</Text>
|
||||||
|
) : null}
|
||||||
|
|
||||||
|
<View style={styles.actions}>
|
||||||
|
{albums.length >= COVER_GRID_ALBUMS ? (
|
||||||
|
<Button
|
||||||
|
title="Shuffle"
|
||||||
|
icon="shuffle"
|
||||||
|
variant="secondary"
|
||||||
|
onPress={() => {
|
||||||
|
setOptions(randomPlaylistCovers(albums))
|
||||||
|
if (selected?.kind === "albums") setSelected(null)
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
) : null}
|
||||||
|
<Button
|
||||||
|
title="Upload"
|
||||||
|
icon="arrow-up"
|
||||||
|
variant="secondary"
|
||||||
|
onPress={() => void pickImage()}
|
||||||
|
/>
|
||||||
|
</View>
|
||||||
|
</ScrollView>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function HeaderButton({
|
||||||
|
label,
|
||||||
|
bold = false,
|
||||||
|
disabled = false,
|
||||||
|
onPress,
|
||||||
|
}: {
|
||||||
|
label: string
|
||||||
|
bold?: boolean
|
||||||
|
disabled?: boolean
|
||||||
|
onPress: () => void
|
||||||
|
}) {
|
||||||
|
const colors = useThemeColors()
|
||||||
|
return (
|
||||||
|
<Pressable
|
||||||
|
accessibilityRole="button"
|
||||||
|
hitSlop={8}
|
||||||
|
disabled={disabled}
|
||||||
|
onPress={onPress}
|
||||||
|
style={{ opacity: disabled ? 0.4 : 1, paddingHorizontal: spacing.xs }}
|
||||||
|
>
|
||||||
|
<Text
|
||||||
|
style={{
|
||||||
|
color: colors.foreground,
|
||||||
|
fontSize: 17,
|
||||||
|
fontWeight: bold ? "600" : "400",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{label}
|
||||||
|
</Text>
|
||||||
|
</Pressable>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function Thumb({
|
||||||
|
size,
|
||||||
|
selected,
|
||||||
|
label,
|
||||||
|
onPress,
|
||||||
|
children,
|
||||||
|
}: {
|
||||||
|
size: number
|
||||||
|
selected: boolean
|
||||||
|
label: string
|
||||||
|
onPress?: () => void
|
||||||
|
children: ReactNode
|
||||||
|
}) {
|
||||||
|
const colors = useThemeColors()
|
||||||
|
return (
|
||||||
|
<Pressable
|
||||||
|
accessibilityRole="button"
|
||||||
|
accessibilityLabel={label}
|
||||||
|
accessibilityState={{ selected }}
|
||||||
|
onPress={onPress}
|
||||||
|
style={[
|
||||||
|
styles.thumb,
|
||||||
|
{
|
||||||
|
width: size + THUMB_CHROME,
|
||||||
|
height: size + THUMB_CHROME,
|
||||||
|
borderColor: selected ? colors.foreground : "transparent",
|
||||||
|
},
|
||||||
|
]}
|
||||||
|
>
|
||||||
|
<View style={styles.thumbInner}>{children}</View>
|
||||||
|
</Pressable>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** A generated cover option: one album alone, or four tiled 2x2. */
|
||||||
|
function AlbumGrid({
|
||||||
|
serverUrl,
|
||||||
|
albumIds,
|
||||||
|
size,
|
||||||
|
}: {
|
||||||
|
serverUrl: string
|
||||||
|
albumIds: Id<"albums">[]
|
||||||
|
size: number
|
||||||
|
}) {
|
||||||
|
const tile = albumIds.length > 1 ? size / 2 : size
|
||||||
|
return (
|
||||||
|
<View
|
||||||
|
style={{
|
||||||
|
width: size,
|
||||||
|
height: size,
|
||||||
|
flexDirection: "row",
|
||||||
|
flexWrap: "wrap",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{albumIds.map((albumId) => (
|
||||||
|
<CoverImage
|
||||||
|
key={albumId}
|
||||||
|
uri={albumCoverUrl(serverUrl, albumId)}
|
||||||
|
hasCover
|
||||||
|
style={{ width: tile, height: tile }}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</View>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const styles = StyleSheet.create({
|
||||||
|
content: {
|
||||||
|
alignItems: "center",
|
||||||
|
gap: spacing.xl,
|
||||||
|
padding: spacing.lg,
|
||||||
|
},
|
||||||
|
preview: { borderRadius: radius.lg, overflow: "hidden" },
|
||||||
|
strip: {
|
||||||
|
flexDirection: "row",
|
||||||
|
alignItems: "center",
|
||||||
|
justifyContent: "center",
|
||||||
|
gap: STRIP_GAP,
|
||||||
|
},
|
||||||
|
separator: { width: StyleSheet.hairlineWidth, alignSelf: "stretch" },
|
||||||
|
thumb: {
|
||||||
|
borderWidth: 2,
|
||||||
|
borderRadius: radius.sm + 2,
|
||||||
|
padding: 1,
|
||||||
|
},
|
||||||
|
thumbInner: { flex: 1, borderRadius: radius.sm, overflow: "hidden" },
|
||||||
|
actions: { flexDirection: "row", gap: spacing.sm },
|
||||||
|
})
|
||||||
@@ -200,7 +200,11 @@ export const PlaylistCard = memo(function PlaylistCard({
|
|||||||
<Link.AppleZoom>
|
<Link.AppleZoom>
|
||||||
<View collapsable={false}>
|
<View collapsable={false}>
|
||||||
<CoverImage
|
<CoverImage
|
||||||
uri={playlistCoverUrl(config.serverUrl, playlist.id)}
|
uri={playlistCoverUrl(
|
||||||
|
config.serverUrl,
|
||||||
|
playlist.id,
|
||||||
|
playlist.cover
|
||||||
|
)}
|
||||||
hasCover={playlist.cover != null}
|
hasCover={playlist.cover != null}
|
||||||
style={
|
style={
|
||||||
fluid
|
fluid
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ export type MediaAction =
|
|||||||
| "go-artist"
|
| "go-artist"
|
||||||
| "go-album"
|
| "go-album"
|
||||||
| "remove-from-playlist"
|
| "remove-from-playlist"
|
||||||
|
| "change-cover"
|
||||||
| "delete-playlist"
|
| "delete-playlist"
|
||||||
|
|
||||||
export type MediaNavigation = {
|
export type MediaNavigation = {
|
||||||
@@ -128,6 +129,11 @@ const MEDIA_ACTION_ITEMS: {
|
|||||||
image: "minus.circle",
|
image: "minus.circle",
|
||||||
destructive: true,
|
destructive: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
action: "change-cover",
|
||||||
|
label: () => "Change Cover",
|
||||||
|
image: "photo",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
action: "delete-playlist",
|
action: "delete-playlist",
|
||||||
label: () => "Delete Playlist",
|
label: () => "Delete Playlist",
|
||||||
@@ -151,7 +157,7 @@ function isActionIncluded(
|
|||||||
return config.includeActions.includes(action)
|
return config.includeActions.includes(action)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (action === "delete-playlist") {
|
if (action === "delete-playlist" || action === "change-cover") {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ const actionIcons = {
|
|||||||
"go-artist": "person-outline",
|
"go-artist": "person-outline",
|
||||||
"go-album": "disc-outline",
|
"go-album": "disc-outline",
|
||||||
"remove-from-playlist": "remove-circle-outline",
|
"remove-from-playlist": "remove-circle-outline",
|
||||||
|
"change-cover": "image-outline",
|
||||||
"delete-playlist": "trash-outline",
|
"delete-playlist": "trash-outline",
|
||||||
} satisfies Record<MediaAction, ComponentProps<typeof Ionicons>["name"]>
|
} satisfies Record<MediaAction, ComponentProps<typeof Ionicons>["name"]>
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,19 @@
|
|||||||
import { useOpenSettings } from "@/hooks/use-open-settings"
|
import { useOpenSettings } from "@/hooks/use-open-settings"
|
||||||
import { requireNativeView } from "expo"
|
import { requireNativeView } from "expo"
|
||||||
import { useIsFocused } from "expo-router"
|
import { useIsFocused, useRouter } from "expo-router"
|
||||||
|
import { useMemo } from "react"
|
||||||
import { Platform, type ViewProps } from "react-native"
|
import { Platform, type ViewProps } from "react-native"
|
||||||
|
|
||||||
|
import { useServerConfig } from "@/contexts/server-config"
|
||||||
import { useCachedQuery } from "@/hooks/use-cached-query"
|
import { useCachedQuery } from "@/hooks/use-cached-query"
|
||||||
import { useProfileImageUrl } from "@/hooks/use-profile-image-url"
|
import { useProfileImageUrl } from "@/hooks/use-profile-image-url"
|
||||||
|
import { tabPath } from "@/lib/app-navigation"
|
||||||
import { initialsFromName } from "@/lib/format"
|
import { initialsFromName } from "@/lib/format"
|
||||||
|
import { playlistCoverUrl } from "@/lib/urls"
|
||||||
import { api } from "@otunes/convex/_generated/api"
|
import { api } from "@otunes/convex/_generated/api"
|
||||||
|
|
||||||
|
type SidebarPlaylist = { id: string; title: string; imageURL?: string }
|
||||||
|
|
||||||
const NativeSidebar =
|
const NativeSidebar =
|
||||||
Platform.OS === "ios"
|
Platform.OS === "ios"
|
||||||
? requireNativeView<
|
? requireNativeView<
|
||||||
@@ -16,16 +22,39 @@ const NativeSidebar =
|
|||||||
initials: string
|
initials: string
|
||||||
imageURL?: string
|
imageURL?: string
|
||||||
active: boolean
|
active: boolean
|
||||||
|
playlists: SidebarPlaylist[]
|
||||||
onProfilePress: () => void
|
onProfilePress: () => void
|
||||||
|
onPlaylistPress: (event: { nativeEvent: { id: string } }) => void
|
||||||
}
|
}
|
||||||
>("TabletSidebar")
|
>("TabletSidebar")
|
||||||
: null
|
: null
|
||||||
|
|
||||||
|
/** iPad sidebar extras: the account footer and a row per playlist with its cover. */
|
||||||
export function TabletSidebar() {
|
export function TabletSidebar() {
|
||||||
const user = useCachedQuery(api.auth.currentUser)
|
const user = useCachedQuery(api.auth.currentUser)
|
||||||
|
const sidebarPlaylists = useCachedQuery(api.playlists.listSidebarPlaylists)
|
||||||
|
const { config } = useServerConfig()
|
||||||
const imageUrl = useProfileImageUrl(user?.image)
|
const imageUrl = useProfileImageUrl(user?.image)
|
||||||
const openSettings = useOpenSettings()
|
const openSettings = useOpenSettings()
|
||||||
|
const router = useRouter()
|
||||||
const active = useIsFocused()
|
const active = useIsFocused()
|
||||||
|
const serverUrl = config?.serverUrl
|
||||||
|
|
||||||
|
const playlists = useMemo(
|
||||||
|
() =>
|
||||||
|
[
|
||||||
|
...(sidebarPlaylists?.owned ?? []),
|
||||||
|
...(sidebarPlaylists?.subscribed ?? []),
|
||||||
|
].map((playlist): SidebarPlaylist => ({
|
||||||
|
id: playlist.id,
|
||||||
|
title: playlist.title,
|
||||||
|
imageURL:
|
||||||
|
serverUrl && playlist.cover
|
||||||
|
? playlistCoverUrl(serverUrl, playlist.id, playlist.cover, 64)
|
||||||
|
: undefined,
|
||||||
|
})),
|
||||||
|
[serverUrl, sidebarPlaylists]
|
||||||
|
)
|
||||||
|
|
||||||
if (!NativeSidebar) return null
|
if (!NativeSidebar) return null
|
||||||
return (
|
return (
|
||||||
@@ -35,7 +64,11 @@ export function TabletSidebar() {
|
|||||||
initials={initialsFromName(user?.name ?? "Me")}
|
initials={initialsFromName(user?.name ?? "Me")}
|
||||||
imageURL={imageUrl}
|
imageURL={imageUrl}
|
||||||
active={active}
|
active={active}
|
||||||
|
playlists={playlists}
|
||||||
onProfilePress={openSettings}
|
onProfilePress={openSettings}
|
||||||
|
onPlaylistPress={({ nativeEvent }) =>
|
||||||
|
router.navigate(tabPath("(playlists)", `/playlist/${nativeEvent.id}`))
|
||||||
|
}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -39,12 +39,15 @@ export function albumCoverUrl(
|
|||||||
return sizedCoverUrl(`${serverUrl}/api/album/${albumId}/cover`, size)
|
return sizedCoverUrl(`${serverUrl}/api/album/${albumId}/cover`, size)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** `version` is the summary's `cover`; it changes with the artwork. */
|
||||||
export function playlistCoverUrl(
|
export function playlistCoverUrl(
|
||||||
serverUrl: string,
|
serverUrl: string,
|
||||||
playlistId: string,
|
playlistId: string,
|
||||||
|
version: string | null,
|
||||||
size?: CoverImageSize
|
size?: CoverImageSize
|
||||||
) {
|
) {
|
||||||
return sizedCoverUrl(`${serverUrl}/api/playlist/${playlistId}/cover`, size)
|
const uri = `${serverUrl}/api/playlist/${playlistId}/cover`
|
||||||
|
return sizedCoverUrl(version ? `${uri}?v=${version}` : uri, size)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function songAudioSource(
|
export function songAudioSource(
|
||||||
|
|||||||
@@ -5,7 +5,11 @@ final class TabletSidebar: Module {
|
|||||||
public func definition() -> ExpoModulesCore.ModuleDefinition {
|
public func definition() -> ExpoModulesCore.ModuleDefinition {
|
||||||
Name("TabletSidebar")
|
Name("TabletSidebar")
|
||||||
View(TabletSidebarView.self) {
|
View(TabletSidebarView.self) {
|
||||||
Events("onProfilePress")
|
Events("onProfilePress", "onPlaylistPress")
|
||||||
|
Prop("playlists") { (view: TabletSidebarView, value: [SidebarPlaylist]) in
|
||||||
|
view.playlists = value
|
||||||
|
view.configure()
|
||||||
|
}
|
||||||
Prop("accountName") { (view: TabletSidebarView, value: String) in
|
Prop("accountName") { (view: TabletSidebarView, value: String) in
|
||||||
view.accountName = value
|
view.accountName = value
|
||||||
view.configure()
|
view.configure()
|
||||||
@@ -26,9 +30,23 @@ final class TabletSidebar: Module {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A playlist row under the sidebar's Playlists section.
|
||||||
|
struct SidebarPlaylist: Record {
|
||||||
|
@Field var id: String = ""
|
||||||
|
@Field var title: String = ""
|
||||||
|
/// Cover URL; changes whenever the artwork does. `nil` shows a list icon.
|
||||||
|
@Field var imageURL: String?
|
||||||
|
}
|
||||||
|
|
||||||
/// Adds UIKit sidebar features not yet exposed by Expo Router's native tabs.
|
/// Adds UIKit sidebar features not yet exposed by Expo Router's native tabs.
|
||||||
final class TabletSidebarView: ExpoView {
|
final class TabletSidebarView: ExpoView {
|
||||||
let onProfilePress = EventDispatcher()
|
let onProfilePress = EventDispatcher()
|
||||||
|
let onPlaylistPress = EventDispatcher()
|
||||||
|
var playlists: [SidebarPlaylist] = []
|
||||||
|
private let selectionProxy = SidebarSelectionProxy()
|
||||||
|
private var playlistTabs: [String: UITab] = [:]
|
||||||
|
private var coverImages: [String: UIImage] = [:]
|
||||||
|
private var coverTasks: [String: URLSessionDataTask] = [:]
|
||||||
var accountName = ""
|
var accountName = ""
|
||||||
var initials = ""
|
var initials = ""
|
||||||
var imageURL: String?
|
var imageURL: String?
|
||||||
@@ -82,11 +100,13 @@ final class TabletSidebarView: ExpoView {
|
|||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
if let tabsObserver { NotificationCenter.default.removeObserver(tabsObserver) }
|
if let tabsObserver { NotificationCenter.default.removeObserver(tabsObserver) }
|
||||||
|
coverTasks.values.forEach { $0.cancel() }
|
||||||
}
|
}
|
||||||
|
|
||||||
@available(iOS 18.0, *)
|
@available(iOS 18.0, *)
|
||||||
private func configure(_ controller: UITabBarController) {
|
private func configure(_ controller: UITabBarController) {
|
||||||
configureGroups(controller)
|
configureGroups(controller)
|
||||||
|
configurePlaylists(controller)
|
||||||
|
|
||||||
let footer: SidebarAccountButton
|
let footer: SidebarAccountButton
|
||||||
if let existing = controller.sidebar.bottomBarView as? SidebarAccountButton {
|
if let existing = controller.sidebar.bottomBarView as? SidebarAccountButton {
|
||||||
@@ -103,6 +123,7 @@ final class TabletSidebarView: ExpoView {
|
|||||||
@available(iOS 18.0, *)
|
@available(iOS 18.0, *)
|
||||||
private func configureTabBar(_ controller: UITabBarController, footer: SidebarAccountButton) {
|
private func configureTabBar(_ controller: UITabBarController, footer: SidebarAccountButton) {
|
||||||
let destinations = controller.tabs.compactMap { $0 as? UITabGroup }.flatMap { $0.children }
|
let destinations = controller.tabs.compactMap { $0 as? UITabGroup }.flatMap { $0.children }
|
||||||
|
.filter { !$0.identifier.hasPrefix(playlistTabPrefix) }
|
||||||
let selected = destinations.first { $0 === controller.selectedTab }
|
let selected = destinations.first { $0 === controller.selectedTab }
|
||||||
let contextualTab = selected ?? footer.contextualTab ?? destinations.first
|
let contextualTab = selected ?? footer.contextualTab ?? destinations.first
|
||||||
footer.contextualTab = contextualTab
|
footer.contextualTab = contextualTab
|
||||||
@@ -160,6 +181,101 @@ final class TabletSidebarView: ExpoView {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private let playlistTabPrefix = "otunes.playlist."
|
||||||
|
private let playlistIconSize: CGFloat = 24
|
||||||
|
|
||||||
|
extension TabletSidebarView {
|
||||||
|
/// Lists each playlist under Playlists with its cover. Rows are never
|
||||||
|
/// selected natively: a tap opens the playlist in the All Playlists stack.
|
||||||
|
@available(iOS 18.0, *)
|
||||||
|
fileprivate func configurePlaylists(_ controller: UITabBarController) {
|
||||||
|
guard let group = controller.tabs.first(where: { $0.identifier == "otunes.playlists" })
|
||||||
|
as? UITabGroup else { return }
|
||||||
|
|
||||||
|
if !(controller.delegate is SidebarSelectionProxy) {
|
||||||
|
selectionProxy.original = controller.delegate
|
||||||
|
controller.delegate = selectionProxy
|
||||||
|
}
|
||||||
|
selectionProxy.onPlaylist = { [weak self] id in self?.onPlaylistPress(["id": id]) }
|
||||||
|
|
||||||
|
var rows: [UITab] = []
|
||||||
|
for playlist in playlists {
|
||||||
|
let identifier = playlistTabPrefix + playlist.id
|
||||||
|
let tab = playlistTabs[identifier] ?? UITab(
|
||||||
|
title: playlist.title, image: nil, identifier: identifier,
|
||||||
|
viewControllerProvider: { _ in UIViewController() })
|
||||||
|
playlistTabs[identifier] = tab
|
||||||
|
if tab.title != playlist.title { tab.title = playlist.title }
|
||||||
|
tab.preferredPlacement = .sidebarOnly
|
||||||
|
tab.image = playlistImage(identifier: identifier, url: playlist.imageURL)
|
||||||
|
rows.append(tab)
|
||||||
|
}
|
||||||
|
let ids = Set(rows.map(\.identifier))
|
||||||
|
playlistTabs = playlistTabs.filter { ids.contains($0.key) }
|
||||||
|
|
||||||
|
let fixed = group.children.filter { !$0.identifier.hasPrefix(playlistTabPrefix) }
|
||||||
|
let children = fixed + rows
|
||||||
|
if group.children.map(\.identifier) != children.map(\.identifier) {
|
||||||
|
group.children = children
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The cached cover for `url`, starting a download on a miss.
|
||||||
|
@available(iOS 18.0, *)
|
||||||
|
private func playlistImage(identifier: String, url: String?) -> UIImage? {
|
||||||
|
let placeholder = UIImage(systemName: "music.note.list")
|
||||||
|
guard let url, let remote = URL(string: url) else { return placeholder }
|
||||||
|
if let image = coverImages[url] { return image }
|
||||||
|
if coverTasks[url] == nil {
|
||||||
|
let task = URLSession.shared.dataTask(with: remote) { [weak self] data, _, _ in
|
||||||
|
guard let data, let image = UIImage(data: data) else { return }
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
guard let self else { return }
|
||||||
|
self.coverTasks[url] = nil
|
||||||
|
self.coverImages[url] = roundedIcon(image)
|
||||||
|
if let tab = self.playlistTabs[identifier] { tab.image = self.coverImages[url] }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
coverTasks[url] = task
|
||||||
|
task.resume()
|
||||||
|
}
|
||||||
|
return placeholder
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Square cover shrunk to sidebar icon size with rounded corners, drawn as is
|
||||||
|
/// rather than tinted like a symbol.
|
||||||
|
private func roundedIcon(_ image: UIImage) -> UIImage {
|
||||||
|
let size = CGSize(width: playlistIconSize, height: playlistIconSize)
|
||||||
|
let rendered = UIGraphicsImageRenderer(size: size).image { _ in
|
||||||
|
UIBezierPath(roundedRect: CGRect(origin: .zero, size: size), cornerRadius: 4).addClip()
|
||||||
|
image.draw(in: CGRect(origin: .zero, size: size))
|
||||||
|
}
|
||||||
|
return rendered.withRenderingMode(.alwaysOriginal)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sits in front of the router's tab controller delegate. Taps on playlist
|
||||||
|
/// rows go to JS; everything else is forwarded untouched.
|
||||||
|
private final class SidebarSelectionProxy: NSObject, UITabBarControllerDelegate {
|
||||||
|
weak var original: UITabBarControllerDelegate?
|
||||||
|
var onPlaylist: ((String) -> Void)?
|
||||||
|
|
||||||
|
override func responds(to aSelector: Selector!) -> Bool {
|
||||||
|
super.responds(to: aSelector) || (original?.responds(to: aSelector) ?? false)
|
||||||
|
}
|
||||||
|
|
||||||
|
override func forwardingTarget(for aSelector: Selector!) -> Any? { original }
|
||||||
|
|
||||||
|
@available(iOS 18.0, *)
|
||||||
|
func tabBarController(_ tabBarController: UITabBarController, shouldSelectTab tab: UITab) -> Bool {
|
||||||
|
if tab.identifier.hasPrefix(playlistTabPrefix) {
|
||||||
|
onPlaylist?(String(tab.identifier.dropFirst(playlistTabPrefix.count)))
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return original?.tabBarController?(tabBarController, shouldSelectTab: tab) ?? true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@available(iOS 18.0, *)
|
@available(iOS 18.0, *)
|
||||||
private final class SidebarAccountButton: UIView {
|
private final class SidebarAccountButton: UIView {
|
||||||
var onPress: (() -> Void)?
|
var onPress: (() -> Void)?
|
||||||
|
|||||||
@@ -1,18 +1,23 @@
|
|||||||
import { api } from "@otunes/convex/_generated/api"
|
import { api } from "@otunes/convex/_generated/api"
|
||||||
import type { Id } from "@otunes/convex/_generated/dataModel"
|
import type { Id } from "@otunes/convex/_generated/dataModel"
|
||||||
import { getConvexClient } from "@/lib/convex-server"
|
import { fetchConvexStorageFile, getConvexClient } from "@/lib/convex-server"
|
||||||
import { serveCoverFile } from "@/lib/serve-cover"
|
import { serveCoverFile, serveStoredCover } from "@/lib/serve-cover"
|
||||||
|
|
||||||
|
/** Playlist artwork, drawn or uploaded ahead of time into Convex storage. */
|
||||||
export async function GET(
|
export async function GET(
|
||||||
request: Request,
|
request: Request,
|
||||||
{ params }: { params: Promise<{ id: string }> }
|
{ params }: { params: Promise<{ id: string }> }
|
||||||
) {
|
) {
|
||||||
const { id } = await params
|
const { id } = await params
|
||||||
try {
|
try {
|
||||||
const cover = await getConvexClient().query(api.library.getPlaylistCover, {
|
const storageUrl = await getConvexClient().query(
|
||||||
id: id as Id<"playlists">,
|
api.library.getPlaylistCover,
|
||||||
})
|
{ id: id as Id<"playlists"> }
|
||||||
return serveCoverFile(cover, request)
|
)
|
||||||
|
const image = storageUrl ? await fetchConvexStorageFile(storageUrl) : null
|
||||||
|
return image
|
||||||
|
? await serveStoredCover(image, request)
|
||||||
|
: await serveCoverFile(null, request)
|
||||||
} catch {
|
} catch {
|
||||||
return new Response("Not found", { status: 404 })
|
return new Response("Not found", { status: 404 })
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { api } from "@otunes/convex/_generated/api"
|
import { api } from "@otunes/convex/_generated/api"
|
||||||
import type { Id } from "@otunes/convex/_generated/dataModel"
|
import type { Id } from "@otunes/convex/_generated/dataModel"
|
||||||
import sharp from "sharp"
|
import sharp from "sharp"
|
||||||
import { getConvexClient, getConvexServerUrl } from "@/lib/convex-server"
|
import { fetchConvexStorageFile, getConvexClient } from "@/lib/convex-server"
|
||||||
|
|
||||||
const AVATAR_SIZE = 256
|
const AVATAR_SIZE = 256
|
||||||
|
|
||||||
@@ -24,17 +24,12 @@ export async function GET(
|
|||||||
return new Response("Not found", { status: 404 })
|
return new Response("Not found", { status: 404 })
|
||||||
}
|
}
|
||||||
|
|
||||||
// The backend builds storage URLs from its public origin, which may not
|
const image = await fetchConvexStorageFile(storageUrl)
|
||||||
// be reachable from here. Fetch the same path from the internal URL.
|
if (!image) {
|
||||||
const { pathname, search } = new URL(storageUrl)
|
|
||||||
const upstream = await fetch(
|
|
||||||
new URL(pathname + search, getConvexServerUrl())
|
|
||||||
)
|
|
||||||
if (!upstream.ok) {
|
|
||||||
return new Response("Not found", { status: 404 })
|
return new Response("Not found", { status: 404 })
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await sharp(Buffer.from(await upstream.arrayBuffer()))
|
const data = await sharp(image)
|
||||||
.rotate()
|
.rotate()
|
||||||
.resize(AVATAR_SIZE, AVATAR_SIZE, {
|
.resize(AVATAR_SIZE, AVATAR_SIZE, {
|
||||||
fit: "cover",
|
fit: "cover",
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ import Link from "next/link"
|
|||||||
import { useTheme } from "next-themes"
|
import { useTheme } from "next-themes"
|
||||||
import * as React from "react"
|
import * as React from "react"
|
||||||
import { api } from "@otunes/convex/_generated/api"
|
import { api } from "@otunes/convex/_generated/api"
|
||||||
|
import { playlistCoverUrl } from "@/lib/cover-urls"
|
||||||
import { useCachedQuery } from "@/hooks/use-cached-query"
|
import { useCachedQuery } from "@/hooks/use-cached-query"
|
||||||
import { useMounted } from "@/hooks/use-mounted"
|
import { useMounted } from "@/hooks/use-mounted"
|
||||||
import { clearPersistentCache } from "@/lib/persistent-cache"
|
import { clearPersistentCache } from "@/lib/persistent-cache"
|
||||||
@@ -86,17 +87,28 @@ export function AppSidebar() {
|
|||||||
const isDark = mounted && resolvedTheme === "dark"
|
const isDark = mounted && resolvedTheme === "dark"
|
||||||
|
|
||||||
const playlistNav = [
|
const playlistNav = [
|
||||||
{ title: "All Playlists", path: "/playlists", icon: LayoutGrid },
|
{
|
||||||
{ title: "Favorites", path: "/playlist/favorites", icon: Star },
|
title: "All Playlists",
|
||||||
...(sidebarPlaylists?.owned ?? []).map((playlist) => ({
|
path: "/playlists",
|
||||||
|
icon: <LayoutGrid />,
|
||||||
|
},
|
||||||
|
{ title: "Favorites", path: "/playlist/favorites", icon: <Star /> },
|
||||||
|
...[
|
||||||
|
...(sidebarPlaylists?.owned ?? []),
|
||||||
|
...(sidebarPlaylists?.subscribed ?? []),
|
||||||
|
].map((playlist) => ({
|
||||||
title: playlist.title,
|
title: playlist.title,
|
||||||
path: `/playlist/${playlist.id}`,
|
path: `/playlist/${playlist.id}`,
|
||||||
icon: ListMusic,
|
icon: playlist.cover ? (
|
||||||
})),
|
// eslint-disable-next-line @next/next/no-img-element -- versioned cover, already sized by the route
|
||||||
...(sidebarPlaylists?.subscribed ?? []).map((playlist) => ({
|
<img
|
||||||
title: playlist.title,
|
src={playlistCoverUrl(playlist.id, playlist.cover, 64)}
|
||||||
path: `/playlist/${playlist.id}`,
|
alt=""
|
||||||
icon: ListMusic,
|
className="size-4 shrink-0 rounded-[3px] object-cover"
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<ListMusic />
|
||||||
|
),
|
||||||
})),
|
})),
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -165,7 +177,7 @@ export function AppSidebar() {
|
|||||||
<SidebarMenuItem key={item.path}>
|
<SidebarMenuItem key={item.path}>
|
||||||
<SidebarMenuButton isActive={pathname === item.path} asChild>
|
<SidebarMenuButton isActive={pathname === item.path} asChild>
|
||||||
<Link href={item.path} onClick={() => setOpenMobile(false)}>
|
<Link href={item.path} onClick={() => setOpenMobile(false)}>
|
||||||
<item.icon />
|
{item.icon}
|
||||||
<span>{item.title}</span>
|
<span>{item.title}</span>
|
||||||
</Link>
|
</Link>
|
||||||
</SidebarMenuButton>
|
</SidebarMenuButton>
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ import { usePlayback } from "@/components/playback-provider"
|
|||||||
import { api } from "@otunes/convex/_generated/api"
|
import { api } from "@otunes/convex/_generated/api"
|
||||||
import type { Id } from "@otunes/convex/_generated/dataModel"
|
import type { Id } from "@otunes/convex/_generated/dataModel"
|
||||||
import type { PlaybackQuality } from "@otunes/convex/playback-stream"
|
import type { PlaybackQuality } from "@otunes/convex/playback-stream"
|
||||||
import { resizeProfileImageToDataUrl } from "@/lib/resize-profile-image"
|
import { resizeProfileImageToDataUrl } from "@/lib/resize-image"
|
||||||
import { cn } from "@/lib/utils"
|
import { cn } from "@/lib/utils"
|
||||||
|
|
||||||
type Section = "profile" | "quality" | "users" | "library"
|
type Section = "profile" | "quality" | "users" | "library"
|
||||||
|
|||||||
@@ -62,10 +62,10 @@ export function PlaylistCard({ playlist }: { playlist: PlaylistSummary }) {
|
|||||||
<div className="group relative aspect-square overflow-hidden rounded-lg ring-1 ring-foreground/10">
|
<div className="group relative aspect-square overflow-hidden rounded-lg ring-1 ring-foreground/10">
|
||||||
<Link className="absolute inset-0" href={"/playlist/" + playlist.id}>
|
<Link className="absolute inset-0" href={"/playlist/" + playlist.id}>
|
||||||
<Image
|
<Image
|
||||||
src={playlistCoverUrl(playlist.id)}
|
src={playlistCoverUrl(playlist.id, playlist.cover, 384)}
|
||||||
alt={playlist.title}
|
alt={playlist.title}
|
||||||
fill
|
fill
|
||||||
sizes="(max-width: 640px) 50vw, (max-width: 1024px) 25vw, 16vw"
|
unoptimized
|
||||||
className="object-cover"
|
className="object-cover"
|
||||||
/>
|
/>
|
||||||
</Link>
|
</Link>
|
||||||
|
|||||||
@@ -0,0 +1,215 @@
|
|||||||
|
"use client"
|
||||||
|
|
||||||
|
import * as React from "react"
|
||||||
|
import Image from "next/image"
|
||||||
|
import { useMutation } from "convex/react"
|
||||||
|
import { ArrowUp, Shuffle } from "lucide-react"
|
||||||
|
import { toast } from "sonner"
|
||||||
|
|
||||||
|
import { Button } from "@/components/ui/button"
|
||||||
|
import {
|
||||||
|
Dialog,
|
||||||
|
DialogContent,
|
||||||
|
DialogHeader,
|
||||||
|
DialogTitle,
|
||||||
|
} from "@/components/ui/dialog"
|
||||||
|
import { albumCoverUrl, playlistCoverUrl } from "@/lib/cover-urls"
|
||||||
|
import { resizeImageToDataUrl } from "@/lib/resize-image"
|
||||||
|
import { cn } from "@/lib/utils"
|
||||||
|
import { api } from "@otunes/convex/_generated/api"
|
||||||
|
import type { Id } from "@otunes/convex/_generated/dataModel"
|
||||||
|
import {
|
||||||
|
COVER_GRID_ALBUMS,
|
||||||
|
playlistCoverAlbums,
|
||||||
|
randomPlaylistCovers,
|
||||||
|
} from "@otunes/convex/client/playlist-cover"
|
||||||
|
import type { BrowseSong } from "@otunes/convex/types"
|
||||||
|
|
||||||
|
const UPLOAD_SIZE = 1024
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lets a playlist owner pick generated artwork (random 2x2 grids of the
|
||||||
|
* playlist's albums, reshuffled on demand) or upload an image. The current
|
||||||
|
* cover sits apart from the options so the owner can always keep it.
|
||||||
|
*/
|
||||||
|
export function PlaylistCoverDialog({
|
||||||
|
open,
|
||||||
|
onOpenChange,
|
||||||
|
playlistId,
|
||||||
|
title,
|
||||||
|
coverVersion,
|
||||||
|
songs,
|
||||||
|
}: {
|
||||||
|
open: boolean
|
||||||
|
onOpenChange: (open: boolean) => void
|
||||||
|
playlistId: Id<"playlists">
|
||||||
|
title: string
|
||||||
|
coverVersion: string | null
|
||||||
|
songs: BrowseSong[]
|
||||||
|
}) {
|
||||||
|
const setCoverAlbums = useMutation(api.playlists.setPlaylistCoverAlbums)
|
||||||
|
const uploadCover = useMutation(api.playlists.uploadPlaylistCover)
|
||||||
|
const fileInputRef = React.useRef<HTMLInputElement>(null)
|
||||||
|
const albums = React.useMemo(() => playlistCoverAlbums(songs), [songs])
|
||||||
|
const [options, setOptions] = React.useState<Id<"albums">[][]>([])
|
||||||
|
const canShuffle = albums.length >= COVER_GRID_ALBUMS
|
||||||
|
|
||||||
|
function shuffle() {
|
||||||
|
setOptions(randomPlaylistCovers(albums))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fresh options each time the dialog opens.
|
||||||
|
const [wasOpen, setWasOpen] = React.useState(false)
|
||||||
|
if (open !== wasOpen) {
|
||||||
|
setWasOpen(open)
|
||||||
|
if (open) shuffle()
|
||||||
|
}
|
||||||
|
|
||||||
|
async function pick(albumIds: Id<"albums">[]) {
|
||||||
|
onOpenChange(false)
|
||||||
|
try {
|
||||||
|
await setCoverAlbums({ playlistId, albumIds })
|
||||||
|
} catch (error) {
|
||||||
|
toast.error(
|
||||||
|
error instanceof Error ? error.message : "Failed to change cover"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function upload(file: File) {
|
||||||
|
if (!file.type.startsWith("image/")) {
|
||||||
|
toast.error("Choose an image file")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
onOpenChange(false)
|
||||||
|
try {
|
||||||
|
const image = await resizeImageToDataUrl(file, UPLOAD_SIZE)
|
||||||
|
await uploadCover({ playlistId, image })
|
||||||
|
} catch (error) {
|
||||||
|
toast.error(
|
||||||
|
error instanceof Error ? error.message : "Failed to upload cover"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||||
|
<DialogContent className="sm:max-w-xl">
|
||||||
|
<DialogHeader>
|
||||||
|
<DialogTitle>Playlist Cover</DialogTitle>
|
||||||
|
</DialogHeader>
|
||||||
|
|
||||||
|
<div className="flex items-stretch gap-3">
|
||||||
|
{coverVersion ? (
|
||||||
|
<>
|
||||||
|
<CoverButton
|
||||||
|
label="Keep current cover"
|
||||||
|
selected
|
||||||
|
onClick={() => onOpenChange(false)}
|
||||||
|
>
|
||||||
|
<Image
|
||||||
|
src={playlistCoverUrl(playlistId, coverVersion, 256)}
|
||||||
|
alt={title}
|
||||||
|
fill
|
||||||
|
unoptimized
|
||||||
|
className="object-cover"
|
||||||
|
/>
|
||||||
|
</CoverButton>
|
||||||
|
<div className="w-px shrink-0 bg-border" />
|
||||||
|
</>
|
||||||
|
) : null}
|
||||||
|
<div className="grid flex-[4] grid-cols-4 gap-3">
|
||||||
|
{options.map((albumIds) => (
|
||||||
|
<CoverButton
|
||||||
|
key={albumIds.join(",")}
|
||||||
|
label="Use this cover"
|
||||||
|
onClick={() => void pick(albumIds)}
|
||||||
|
>
|
||||||
|
<AlbumGrid albumIds={albumIds} />
|
||||||
|
</CoverButton>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{albums.length === 0 ? (
|
||||||
|
<p className="text-sm text-muted-foreground">
|
||||||
|
No album artwork in this playlist yet.
|
||||||
|
</p>
|
||||||
|
) : null}
|
||||||
|
|
||||||
|
<div className="flex gap-2">
|
||||||
|
{canShuffle ? (
|
||||||
|
<Button variant="secondary" onClick={shuffle}>
|
||||||
|
<Shuffle />
|
||||||
|
Shuffle
|
||||||
|
</Button>
|
||||||
|
) : null}
|
||||||
|
<Button
|
||||||
|
variant="secondary"
|
||||||
|
onClick={() => fileInputRef.current?.click()}
|
||||||
|
>
|
||||||
|
<ArrowUp />
|
||||||
|
Upload Image
|
||||||
|
</Button>
|
||||||
|
<input
|
||||||
|
ref={fileInputRef}
|
||||||
|
type="file"
|
||||||
|
accept="image/*"
|
||||||
|
className="hidden"
|
||||||
|
onChange={(event) => {
|
||||||
|
const file = event.target.files?.[0]
|
||||||
|
event.target.value = ""
|
||||||
|
if (file) void upload(file)
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function CoverButton({
|
||||||
|
label,
|
||||||
|
selected = false,
|
||||||
|
onClick,
|
||||||
|
children,
|
||||||
|
}: {
|
||||||
|
label: string
|
||||||
|
selected?: boolean
|
||||||
|
onClick: () => void
|
||||||
|
children: React.ReactNode
|
||||||
|
}) {
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
aria-label={label}
|
||||||
|
onClick={onClick}
|
||||||
|
className={cn(
|
||||||
|
"relative aspect-square min-w-0 flex-1 overflow-hidden rounded-md ring-offset-2 ring-offset-background transition-shadow outline-none hover:ring-2 hover:ring-foreground/60 focus-visible:ring-2 focus-visible:ring-ring",
|
||||||
|
selected && "ring-2 ring-foreground hover:ring-foreground"
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</button>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Preview of a generated cover: one album alone, or four tiled 2x2. */
|
||||||
|
function AlbumGrid({ albumIds }: { albumIds: Id<"albums">[] }) {
|
||||||
|
const grid = albumIds.length > 1
|
||||||
|
return (
|
||||||
|
<div className={cn("grid size-full", grid && "grid-cols-2")}>
|
||||||
|
{albumIds.map((albumId) => (
|
||||||
|
<div key={albumId} className="relative">
|
||||||
|
<Image
|
||||||
|
src={albumCoverUrl(albumId)}
|
||||||
|
alt=""
|
||||||
|
fill
|
||||||
|
sizes={grid ? "64px" : "128px"}
|
||||||
|
className="object-cover"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -6,7 +6,16 @@ import * as React from "react"
|
|||||||
import Image from "next/image"
|
import Image from "next/image"
|
||||||
import { useRouter } from "next/navigation"
|
import { useRouter } from "next/navigation"
|
||||||
import { useMutation } from "convex/react"
|
import { useMutation } from "convex/react"
|
||||||
import { Play, SearchIcon, Share2, Shuffle, Star, Trash2 } from "lucide-react"
|
import {
|
||||||
|
ImageIcon,
|
||||||
|
Pencil,
|
||||||
|
Play,
|
||||||
|
SearchIcon,
|
||||||
|
Share2,
|
||||||
|
Shuffle,
|
||||||
|
Star,
|
||||||
|
Trash2,
|
||||||
|
} from "lucide-react"
|
||||||
import { toast } from "sonner"
|
import { toast } from "sonner"
|
||||||
|
|
||||||
import { useFavorites } from "@/components/favorites-provider"
|
import { useFavorites } from "@/components/favorites-provider"
|
||||||
@@ -15,6 +24,7 @@ import {
|
|||||||
usePlaybackActions,
|
usePlaybackActions,
|
||||||
} from "@/components/playback-provider"
|
} from "@/components/playback-provider"
|
||||||
import { MediaCollectionFooter } from "@/components/media-collection-footer"
|
import { MediaCollectionFooter } from "@/components/media-collection-footer"
|
||||||
|
import { PlaylistCoverDialog } from "@/components/playlist-cover-dialog"
|
||||||
import {
|
import {
|
||||||
MediaContextMenu,
|
MediaContextMenu,
|
||||||
type MediaContextAction,
|
type MediaContextAction,
|
||||||
@@ -97,6 +107,8 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) {
|
|||||||
const [dragIndex, setDragIndex] = React.useState<number | null>(null)
|
const [dragIndex, setDragIndex] = React.useState<number | null>(null)
|
||||||
const [isSubscribed, setIsSubscribed] = React.useState(playlist.isSubscribed)
|
const [isSubscribed, setIsSubscribed] = React.useState(playlist.isSubscribed)
|
||||||
const [subscriptionPending, setSubscriptionPending] = React.useState(false)
|
const [subscriptionPending, setSubscriptionPending] = React.useState(false)
|
||||||
|
const [coverDialogOpen, setCoverDialogOpen] = React.useState(false)
|
||||||
|
const canEditCover = isOwner && !isFavorites && playlist.id !== null
|
||||||
|
|
||||||
if (syncedPlaylist !== playlist) {
|
if (syncedPlaylist !== playlist) {
|
||||||
setSyncedPlaylist(playlist)
|
setSyncedPlaylist(playlist)
|
||||||
@@ -291,21 +303,32 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) {
|
|||||||
<div className="pb-28">
|
<div className="pb-28">
|
||||||
<div className="mx-auto w-full max-w-9/10 min-w-0">
|
<div className="mx-auto w-full max-w-9/10 min-w-0">
|
||||||
<div className="flex flex-col gap-8 px-6 pt-6 pb-8 sm:flex-row sm:items-end">
|
<div className="flex flex-col gap-8 px-6 pt-6 pb-8 sm:flex-row sm:items-end">
|
||||||
<div className="relative mx-auto aspect-square w-full max-w-60 shrink-0 overflow-hidden rounded-lg ring-1 ring-foreground/10 sm:mx-0">
|
<div className="group relative mx-auto aspect-square w-full max-w-60 shrink-0 overflow-hidden rounded-lg ring-1 ring-foreground/10 sm:mx-0">
|
||||||
{isFavorites ? (
|
{isFavorites ? (
|
||||||
<div className="flex size-full items-center justify-center bg-muted">
|
<div className="flex size-full items-center justify-center bg-muted">
|
||||||
<Star className="size-16 fill-primary text-primary" />
|
<Star className="size-16 fill-primary text-primary" />
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<Image
|
<Image
|
||||||
src={playlistCoverUrl(playlist.id!)}
|
src={playlistCoverUrl(playlist.id!, playlist.cover, 512)}
|
||||||
alt={playlist.title}
|
alt={playlist.title}
|
||||||
fill
|
fill
|
||||||
sizes="240px"
|
unoptimized
|
||||||
className="object-cover"
|
className="object-cover"
|
||||||
preload
|
preload
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
{canEditCover ? (
|
||||||
|
<Button
|
||||||
|
variant="secondary"
|
||||||
|
size="icon"
|
||||||
|
className="absolute right-2 bottom-2 rounded-full opacity-0 shadow-md transition-opacity group-hover:opacity-100 focus-visible:opacity-100"
|
||||||
|
aria-label="Change cover"
|
||||||
|
onClick={() => setCoverDialogOpen(true)}
|
||||||
|
>
|
||||||
|
<Pencil />
|
||||||
|
</Button>
|
||||||
|
) : null}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex min-w-0 flex-1 flex-col gap-4">
|
<div className="flex min-w-0 flex-1 flex-col gap-4">
|
||||||
@@ -385,6 +408,13 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) {
|
|||||||
{isOwner && !isFavorites ? (
|
{isOwner && !isFavorites ? (
|
||||||
<>
|
<>
|
||||||
<DropdownMenuSeparator />
|
<DropdownMenuSeparator />
|
||||||
|
<DropdownMenuItem
|
||||||
|
className="justify-between"
|
||||||
|
onClick={() => setCoverDialogOpen(true)}
|
||||||
|
>
|
||||||
|
Change Cover
|
||||||
|
<ImageIcon />
|
||||||
|
</DropdownMenuItem>
|
||||||
<DropdownMenuItem
|
<DropdownMenuItem
|
||||||
variant="destructive"
|
variant="destructive"
|
||||||
className="justify-between"
|
className="justify-between"
|
||||||
@@ -476,6 +506,16 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</ScrollArea>
|
</ScrollArea>
|
||||||
|
{canEditCover ? (
|
||||||
|
<PlaylistCoverDialog
|
||||||
|
open={coverDialogOpen}
|
||||||
|
onOpenChange={setCoverDialogOpen}
|
||||||
|
playlistId={playlist.id!}
|
||||||
|
title={playlist.title}
|
||||||
|
coverVersion={playlist.cover}
|
||||||
|
songs={songs}
|
||||||
|
/>
|
||||||
|
) : null}
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -24,3 +24,14 @@ export function getConvexClient() {
|
|||||||
}
|
}
|
||||||
return client
|
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
|
||||||
|
}
|
||||||
@@ -12,6 +12,15 @@ export function albumCoverUrl(albumId: string, size?: CoverImageSize) {
|
|||||||
return withCoverImageSize(`/api/album/${albumId}/cover`, size)
|
return withCoverImageSize(`/api/album/${albumId}/cover`, size)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function playlistCoverUrl(playlistId: string, size?: CoverImageSize) {
|
/** `version` is the summary's `cover`; it changes with the artwork. */
|
||||||
return withCoverImageSize(`/api/playlist/${playlistId}/cover`, size)
|
export function playlistCoverUrl(
|
||||||
|
playlistId: string,
|
||||||
|
version: string | null,
|
||||||
|
size?: CoverImageSize
|
||||||
|
) {
|
||||||
|
const params = new URLSearchParams()
|
||||||
|
if (version) params.set("v", version)
|
||||||
|
if (size) params.set("size", String(size))
|
||||||
|
const query = params.toString()
|
||||||
|
return `/api/playlist/${playlistId}/cover${query ? `?${query}` : ""}`
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,12 @@
|
|||||||
const PROFILE_IMAGE_SIZE = 256
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Downscales an image to 256×256 (center-cropped cover) and returns a base64 data URL.
|
* Downscales an image to `size`×`size` (center-cropped cover) and returns a
|
||||||
|
* base64 data URL. JPEG, unless `keepLossless` and the file is PNG or WebP.
|
||||||
*/
|
*/
|
||||||
export function resizeProfileImageToDataUrl(file: File): Promise<string> {
|
export function resizeImageToDataUrl(
|
||||||
|
file: File,
|
||||||
|
size: number,
|
||||||
|
{ keepLossless = false } = {}
|
||||||
|
): Promise<string> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const url = URL.createObjectURL(file)
|
const url = URL.createObjectURL(file)
|
||||||
const image = new Image()
|
const image = new Image()
|
||||||
@@ -12,8 +15,8 @@ export function resizeProfileImageToDataUrl(file: File): Promise<string> {
|
|||||||
URL.revokeObjectURL(url)
|
URL.revokeObjectURL(url)
|
||||||
|
|
||||||
const canvas = document.createElement("canvas")
|
const canvas = document.createElement("canvas")
|
||||||
canvas.width = PROFILE_IMAGE_SIZE
|
canvas.width = size
|
||||||
canvas.height = PROFILE_IMAGE_SIZE
|
canvas.height = size
|
||||||
|
|
||||||
const ctx = canvas.getContext("2d")
|
const ctx = canvas.getContext("2d")
|
||||||
if (!ctx) {
|
if (!ctx) {
|
||||||
@@ -21,19 +24,20 @@ export function resizeProfileImageToDataUrl(file: File): Promise<string> {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const scale = Math.max(
|
const scale = Math.max(size / image.width, size / image.height)
|
||||||
PROFILE_IMAGE_SIZE / image.width,
|
|
||||||
PROFILE_IMAGE_SIZE / image.height
|
|
||||||
)
|
|
||||||
const width = image.width * scale
|
const width = image.width * scale
|
||||||
const height = image.height * scale
|
const height = image.height * scale
|
||||||
const x = (PROFILE_IMAGE_SIZE - width) / 2
|
ctx.drawImage(
|
||||||
const y = (PROFILE_IMAGE_SIZE - height) / 2
|
image,
|
||||||
|
(size - width) / 2,
|
||||||
ctx.drawImage(image, x, y, width, height)
|
(size - height) / 2,
|
||||||
|
width,
|
||||||
|
height
|
||||||
|
)
|
||||||
|
|
||||||
const outputType =
|
const outputType =
|
||||||
file.type === "image/png" || file.type === "image/webp"
|
keepLossless &&
|
||||||
|
(file.type === "image/png" || file.type === "image/webp")
|
||||||
? "image/png"
|
? "image/png"
|
||||||
: "image/jpeg"
|
: "image/jpeg"
|
||||||
resolve(
|
resolve(
|
||||||
@@ -52,3 +56,7 @@ export function resizeProfileImageToDataUrl(file: File): Promise<string> {
|
|||||||
image.src = url
|
image.src = url
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function resizeProfileImageToDataUrl(file: File) {
|
||||||
|
return resizeImageToDataUrl(file, 256, { keepLossless: true })
|
||||||
|
}
|
||||||
@@ -34,6 +34,29 @@ function requestedCoverSize(request: Request): number | undefined {
|
|||||||
return COVER_IMAGE_SIZES.has(size) ? size : undefined
|
return COVER_IMAGE_SIZES.has(size) ? size : undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Serves stored playlist artwork as a square WebP, `?size=` pixels a side
|
||||||
|
* (1024 by default). URLs
|
||||||
|
* carry the artwork's version (`?v=`), so new artwork gets a new URL and
|
||||||
|
* versioned responses can be cached for good.
|
||||||
|
*/
|
||||||
|
export async function serveStoredCover(image: Buffer, request: Request) {
|
||||||
|
const size = requestedCoverSize(request) ?? 1024
|
||||||
|
const data = await sharp(image)
|
||||||
|
.rotate()
|
||||||
|
.resize(size, size, { fit: "cover", withoutEnlargement: true })
|
||||||
|
.webp({ quality: 80 })
|
||||||
|
.toBuffer()
|
||||||
|
return new Response(new Uint8Array(data), {
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "image/webp",
|
||||||
|
"Cache-Control": new URL(request.url).searchParams.has("v")
|
||||||
|
? "public, max-age=31536000, immutable"
|
||||||
|
: COVER_CACHE_CONTROL,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
export async function serveCoverFile(
|
export async function serveCoverFile(
|
||||||
coverPath: string | null | undefined,
|
coverPath: string | null | undefined,
|
||||||
request: Request
|
request: Request
|
||||||
|
|||||||
@@ -99,6 +99,7 @@ convex_cli deploy --typecheck disable --codegen disable
|
|||||||
|
|
||||||
log "Running data migrations"
|
log "Running data migrations"
|
||||||
convex_cli run migrations:migrateUsers
|
convex_cli run migrations:migrateUsers
|
||||||
|
convex_cli run migrations:migratePlaylists
|
||||||
|
|
||||||
# `env get` exits 0 with no output when the variable is unset. Any other failure
|
# `env get` exits 0 with no output when the variable is unset. Any other failure
|
||||||
# must not be read as "no keys": regenerating them signs every user out.
|
# must not be read as "no keys": regenerating them signs every user out.
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
import type { Id } from "../convex/_generated/dataModel"
|
||||||
|
import { COVER_GRID_ALBUMS } from "../convex/limits"
|
||||||
|
import type { BrowseSong } from "../convex/types"
|
||||||
|
|
||||||
|
export { COVER_GRID_ALBUMS }
|
||||||
|
|
||||||
|
export type CoverAlbum = BrowseSong["album"]
|
||||||
|
|
||||||
|
/** Distinct albums that have artwork, in playlist order. */
|
||||||
|
export function playlistCoverAlbums(songs: readonly BrowseSong[]) {
|
||||||
|
const albums = new Map<string, CoverAlbum>()
|
||||||
|
for (const song of songs) {
|
||||||
|
if (song.album.cover !== null && !albums.has(song.album.id)) {
|
||||||
|
albums.set(song.album.id, song.album)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return [...albums.values()]
|
||||||
|
}
|
||||||
|
|
||||||
|
/** `count` distinct items in random order (Fisher-Yates prefix). */
|
||||||
|
function sample<T>(items: readonly T[], count: number) {
|
||||||
|
const pool = [...items]
|
||||||
|
for (let index = 0; index < count; index++) {
|
||||||
|
const swap = index + Math.floor(Math.random() * (pool.length - index))
|
||||||
|
;[pool[index], pool[swap]] = [pool[swap], pool[index]]
|
||||||
|
}
|
||||||
|
return pool.slice(0, count)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cover options to pick from, as album ids for `setPlaylistCoverAlbums`.
|
||||||
|
* With four or more albums: `count` random 2x2 grids, avoiding repeats.
|
||||||
|
* With fewer: each album's cover alone.
|
||||||
|
*/
|
||||||
|
export function randomPlaylistCovers(
|
||||||
|
albums: readonly CoverAlbum[],
|
||||||
|
count = 4
|
||||||
|
): Id<"albums">[][] {
|
||||||
|
const ids = albums.map((album) => album.id)
|
||||||
|
if (ids.length < COVER_GRID_ALBUMS) return ids.map((id) => [id])
|
||||||
|
|
||||||
|
const options = new Map<string, Id<"albums">[]>()
|
||||||
|
for (
|
||||||
|
let attempt = 0;
|
||||||
|
options.size < count && attempt < count * 10;
|
||||||
|
attempt++
|
||||||
|
) {
|
||||||
|
const option = sample(ids, COVER_GRID_ALBUMS)
|
||||||
|
options.set(option.join(","), option)
|
||||||
|
}
|
||||||
|
return [...options.values()]
|
||||||
|
}
|
||||||
+6
@@ -14,6 +14,7 @@ import type * as crons from "../crons.js";
|
|||||||
import type * as favoriteStore from "../favoriteStore.js";
|
import type * as favoriteStore from "../favoriteStore.js";
|
||||||
import type * as favorites from "../favorites.js";
|
import type * as favorites from "../favorites.js";
|
||||||
import type * as http from "../http.js";
|
import type * as http from "../http.js";
|
||||||
|
import type * as images from "../images.js";
|
||||||
import type * as intentCatalog from "../intentCatalog.js";
|
import type * as intentCatalog from "../intentCatalog.js";
|
||||||
import type * as intentCatalogChanges from "../intentCatalogChanges.js";
|
import type * as intentCatalogChanges from "../intentCatalogChanges.js";
|
||||||
import type * as library from "../library.js";
|
import type * as library from "../library.js";
|
||||||
@@ -21,6 +22,8 @@ import type * as limits from "../limits.js";
|
|||||||
import type * as migrations from "../migrations.js";
|
import type * as migrations from "../migrations.js";
|
||||||
import type * as playback from "../playback.js";
|
import type * as playback from "../playback.js";
|
||||||
import type * as playbackState from "../playbackState.js";
|
import type * as playbackState from "../playbackState.js";
|
||||||
|
import type * as playlistCover from "../playlistCover.js";
|
||||||
|
import type * as playlistCoverImages from "../playlistCoverImages.js";
|
||||||
import type * as playlists from "../playlists.js";
|
import type * as playlists from "../playlists.js";
|
||||||
import type * as types from "../types.js";
|
import type * as types from "../types.js";
|
||||||
import type * as users from "../users.js";
|
import type * as users from "../users.js";
|
||||||
@@ -38,6 +41,7 @@ declare const fullApi: ApiFromModules<{
|
|||||||
favoriteStore: typeof favoriteStore;
|
favoriteStore: typeof favoriteStore;
|
||||||
favorites: typeof favorites;
|
favorites: typeof favorites;
|
||||||
http: typeof http;
|
http: typeof http;
|
||||||
|
images: typeof images;
|
||||||
intentCatalog: typeof intentCatalog;
|
intentCatalog: typeof intentCatalog;
|
||||||
intentCatalogChanges: typeof intentCatalogChanges;
|
intentCatalogChanges: typeof intentCatalogChanges;
|
||||||
library: typeof library;
|
library: typeof library;
|
||||||
@@ -45,6 +49,8 @@ declare const fullApi: ApiFromModules<{
|
|||||||
migrations: typeof migrations;
|
migrations: typeof migrations;
|
||||||
playback: typeof playback;
|
playback: typeof playback;
|
||||||
playbackState: typeof playbackState;
|
playbackState: typeof playbackState;
|
||||||
|
playlistCover: typeof playlistCover;
|
||||||
|
playlistCoverImages: typeof playlistCoverImages;
|
||||||
playlists: typeof playlists;
|
playlists: typeof playlists;
|
||||||
types: typeof types;
|
types: typeof types;
|
||||||
users: typeof users;
|
users: typeof users;
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
import { ConvexError } from "convex/values"
|
||||||
|
import { MAX_IMAGE_UPLOAD_BYTES } from "./limits"
|
||||||
|
|
||||||
|
const DATA_URL_PATTERN = /^data:(image\/[\w.+-]+);base64,([A-Za-z0-9+/=]+)$/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Splits an image data URL, rejecting anything over the size cap. `label`
|
||||||
|
* names the image in error messages ("Profile photo").
|
||||||
|
*/
|
||||||
|
export function parseImageDataUrl(image: string, label: string) {
|
||||||
|
const match = DATA_URL_PATTERN.exec(image)
|
||||||
|
if (!match) {
|
||||||
|
throw new ConvexError(`${label} must be an image`)
|
||||||
|
}
|
||||||
|
const [, contentType, base64] = match
|
||||||
|
if (Math.floor((base64.length * 3) / 4) > MAX_IMAGE_UPLOAD_BYTES) {
|
||||||
|
throw new ConvexError(
|
||||||
|
`${label} must be ${MAX_IMAGE_UPLOAD_BYTES / 1024 / 1024} MB or smaller`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return { contentType, base64 }
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Decodes an image data URL for `ctx.storage.store`. */
|
||||||
|
export function imageDataUrlToBlob(image: string, label: string) {
|
||||||
|
const { contentType, base64 } = parseImageDataUrl(image, label)
|
||||||
|
const bytes = Uint8Array.from(atob(base64), (char) => char.charCodeAt(0))
|
||||||
|
return new Blob([bytes], { type: contentType })
|
||||||
|
}
|
||||||
@@ -24,6 +24,7 @@ import type {
|
|||||||
import { purgeFavoriteEntities } from "./favoriteStore"
|
import { purgeFavoriteEntities } from "./favoriteStore"
|
||||||
import { chunk, type Chunked } from "./limits"
|
import { chunk, type Chunked } from "./limits"
|
||||||
import { recordIntentCatalogChanges } from "./intentCatalogChanges"
|
import { recordIntentCatalogChanges } from "./intentCatalogChanges"
|
||||||
|
import { playlistCoverVersion } from "./playlistCover"
|
||||||
|
|
||||||
export type {
|
export type {
|
||||||
BrowseAlbum,
|
BrowseAlbum,
|
||||||
@@ -875,7 +876,7 @@ async function browseRecentlyPlayedItems(ctx: QueryCtx, limit?: number) {
|
|||||||
id: entry.playlist._id,
|
id: entry.playlist._id,
|
||||||
title: entry.playlist.name,
|
title: entry.playlist.name,
|
||||||
owner: ownerMap.get(entry.playlist.owner)?.name ?? "Unknown",
|
owner: ownerMap.get(entry.playlist.owner)?.name ?? "Unknown",
|
||||||
cover: entry.playlist.cover,
|
cover: playlistCoverVersion(entry.playlist),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -959,11 +960,15 @@ export const getAlbumCover = query({
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
/** Public: share-link previews load covers without a session. */
|
/**
|
||||||
|
* Public: share-link previews load covers without a session. A storage URL
|
||||||
|
* the web server proxies, or `null` when the playlist has no artwork.
|
||||||
|
*/
|
||||||
export const getPlaylistCover = query({
|
export const getPlaylistCover = query({
|
||||||
args: { id: v.id("playlists") },
|
args: { id: v.id("playlists") },
|
||||||
handler: async (ctx, args) => {
|
handler: async (ctx, args) => {
|
||||||
return (await ctx.db.get("playlists", args.id))?.cover ?? null
|
const storageId = (await ctx.db.get("playlists", args.id))?.coverStorageId
|
||||||
|
return storageId ? await ctx.storage.getUrl(storageId) : null
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -6,8 +6,11 @@
|
|||||||
/** Songs kept in the playback queue, including the current song. */
|
/** Songs kept in the playback queue, including the current song. */
|
||||||
export const MAX_QUEUE_LENGTH = 5_000
|
export const MAX_QUEUE_LENGTH = 5_000
|
||||||
|
|
||||||
/** Largest profile photo accepted, in bytes after base64 decoding. */
|
/** Largest uploaded image (profile photo, playlist cover), in bytes after base64 decoding. */
|
||||||
export const MAX_PROFILE_IMAGE_BYTES = 2 * 1024 * 1024
|
export const MAX_IMAGE_UPLOAD_BYTES = 2 * 1024 * 1024
|
||||||
|
|
||||||
|
/** Album covers a generated playlist cover tiles 2x2. */
|
||||||
|
export const COVER_GRID_ALBUMS = 4
|
||||||
|
|
||||||
/** Most values Convex accepts in a single array. */
|
/** Most values Convex accepts in a single array. */
|
||||||
export const MAX_ARRAY_LENGTH = 8_192
|
export const MAX_ARRAY_LENGTH = 8_192
|
||||||
|
|||||||
@@ -56,3 +56,32 @@ export const migrateFavorites = internalMutation({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Draws automatic artwork for playlists that have none yet. Idempotent; the
|
||||||
|
* Docker entrypoint runs it on every boot.
|
||||||
|
*/
|
||||||
|
export const migratePlaylists = internalMutation({
|
||||||
|
args: { cursor: v.optional(v.union(v.string(), v.null())) },
|
||||||
|
handler: async (ctx, { cursor }) => {
|
||||||
|
const page = await ctx.db
|
||||||
|
.query("playlists")
|
||||||
|
.paginate({ cursor: cursor ?? null, numItems: BATCH_SIZE })
|
||||||
|
|
||||||
|
for (const playlist of page.page) {
|
||||||
|
if (!playlist.coverSource && !playlist.coverAlbums) {
|
||||||
|
await ctx.scheduler.runAfter(
|
||||||
|
0,
|
||||||
|
internal.playlistCover.refreshPlaylistCover,
|
||||||
|
{ playlistId: playlist._id }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!page.isDone) {
|
||||||
|
await ctx.scheduler.runAfter(0, internal.migrations.migratePlaylists, {
|
||||||
|
cursor: page.continueCursor,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
},
|
||||||
|
})
|
||||||
@@ -0,0 +1,161 @@
|
|||||||
|
import { v } from "convex/values"
|
||||||
|
import { internal } from "./_generated/api"
|
||||||
|
import type { Doc, Id } from "./_generated/dataModel"
|
||||||
|
import type { MutationCtx } from "./_generated/server"
|
||||||
|
import { internalMutation, internalQuery } from "./_generated/server"
|
||||||
|
import { COVER_GRID_ALBUMS } from "./limits"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Playlist artwork is a stored image (`coverStorageId`), drawn once and
|
||||||
|
* redrawn only when its inputs change:
|
||||||
|
* - automatic (no `coverSource`): the first albums in the playlist, kept in
|
||||||
|
* sync by `refreshAutomaticCover` whenever the songs change
|
||||||
|
* - "albums": albums the owner picked
|
||||||
|
* - "upload": an image the owner uploaded
|
||||||
|
* `coverAlbums` holds the albums behind the current or pending drawing: four
|
||||||
|
* tile 2x2, one shows alone. Drawing happens in `playlistCoverImages`.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* What clients put in the cover URL (`?v=`) so new artwork gets a new URL.
|
||||||
|
* `null` when the playlist has no artwork.
|
||||||
|
*/
|
||||||
|
export function playlistCoverVersion(playlist: Doc<"playlists">) {
|
||||||
|
return playlist.coverStorageId ?? null
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Songs read when picking automatic artwork, so long single-album runs stay cheap. */
|
||||||
|
const AUTO_COVER_SONG_SCAN = 50
|
||||||
|
|
||||||
|
/** First distinct album covers in playlist order: four for a grid, else one. */
|
||||||
|
async function automaticCoverAlbums(ctx: MutationCtx, songIds: Id<"songs">[]) {
|
||||||
|
const seen = new Set<Id<"albums">>()
|
||||||
|
const albums: Id<"albums">[] = []
|
||||||
|
for (const songId of songIds.slice(0, AUTO_COVER_SONG_SCAN)) {
|
||||||
|
const song = await ctx.db.get("songs", songId)
|
||||||
|
if (!song || seen.has(song.album)) continue
|
||||||
|
seen.add(song.album)
|
||||||
|
const album = await ctx.db.get("albums", song.album)
|
||||||
|
if (album?.cover) albums.push(album._id)
|
||||||
|
if (albums.length === COVER_GRID_ALBUMS) break
|
||||||
|
}
|
||||||
|
return albums.length === COVER_GRID_ALBUMS ? albums : albums.slice(0, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
function sameAlbums(a: Id<"albums">[] | undefined, b: Id<"albums">[]) {
|
||||||
|
return a?.length === b.length && a.every((id, index) => id === b[index])
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Points `coverAlbums` at new albums and queues a drawing of them. The old
|
||||||
|
* image stays up until the new one lands.
|
||||||
|
*/
|
||||||
|
export async function drawCoverAlbums(
|
||||||
|
ctx: MutationCtx,
|
||||||
|
playlist: Doc<"playlists">,
|
||||||
|
albumIds: Id<"albums">[],
|
||||||
|
coverSource: "albums" | undefined
|
||||||
|
) {
|
||||||
|
await ctx.db.patch(playlist._id, { coverAlbums: albumIds, coverSource })
|
||||||
|
await ctx.scheduler.runAfter(
|
||||||
|
0,
|
||||||
|
internal.playlistCoverImages.renderPlaylistCover,
|
||||||
|
{ playlistId: playlist._id, albumIds }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Keeps automatic artwork in step with the playlist's first albums. Call
|
||||||
|
* after the songs change, with the new order. Draws only when the albums
|
||||||
|
* differ from the last drawing.
|
||||||
|
*/
|
||||||
|
export async function refreshAutomaticCover(
|
||||||
|
ctx: MutationCtx,
|
||||||
|
playlist: Doc<"playlists">,
|
||||||
|
songIds: Id<"songs">[]
|
||||||
|
) {
|
||||||
|
if (playlist.coverSource) return
|
||||||
|
const albums = await automaticCoverAlbums(ctx, songIds)
|
||||||
|
if (sameAlbums(playlist.coverAlbums, albums)) return
|
||||||
|
if (albums.length > 0) {
|
||||||
|
await drawCoverAlbums(ctx, playlist, albums, undefined)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (playlist.coverStorageId) await ctx.storage.delete(playlist.coverStorageId)
|
||||||
|
await ctx.db.patch(playlist._id, {
|
||||||
|
coverAlbums: undefined,
|
||||||
|
coverStorageId: undefined,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Album cover file paths for a drawing, or `null` if any lost its artwork. */
|
||||||
|
export const coverFilePaths = internalQuery({
|
||||||
|
args: { albumIds: v.array(v.id("albums")) },
|
||||||
|
handler: async (ctx, { albumIds }) => {
|
||||||
|
const albums = await Promise.all(
|
||||||
|
albumIds.map((id) => ctx.db.get("albums", id))
|
||||||
|
)
|
||||||
|
const paths = albums.flatMap((album) => (album?.cover ? [album.cover] : []))
|
||||||
|
return paths.length === albumIds.length ? paths : null
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
/** Deletes the image `storageId` replaces. */
|
||||||
|
async function deleteReplacedImage(
|
||||||
|
ctx: MutationCtx,
|
||||||
|
playlist: Doc<"playlists">,
|
||||||
|
storageId: Id<"_storage">
|
||||||
|
) {
|
||||||
|
if (playlist.coverStorageId && playlist.coverStorageId !== storageId) {
|
||||||
|
await ctx.storage.delete(playlist.coverStorageId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Saves a finished drawing unless the playlist moved on while it rendered. */
|
||||||
|
export const attachRenderedCover = internalMutation({
|
||||||
|
args: {
|
||||||
|
playlistId: v.id("playlists"),
|
||||||
|
albumIds: v.array(v.id("albums")),
|
||||||
|
storageId: v.id("_storage"),
|
||||||
|
},
|
||||||
|
handler: async (ctx, { playlistId, albumIds, storageId }) => {
|
||||||
|
const playlist = await ctx.db.get("playlists", playlistId)
|
||||||
|
if (
|
||||||
|
!playlist ||
|
||||||
|
playlist.coverSource === "upload" ||
|
||||||
|
!sameAlbums(playlist.coverAlbums, albumIds)
|
||||||
|
) {
|
||||||
|
await ctx.storage.delete(storageId)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
await deleteReplacedImage(ctx, playlist, storageId)
|
||||||
|
await ctx.db.patch(playlist._id, { coverStorageId: storageId })
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
/** Saves an uploaded image as the playlist's artwork. */
|
||||||
|
export const attachUploadedCover = internalMutation({
|
||||||
|
args: { playlistId: v.id("playlists"), storageId: v.id("_storage") },
|
||||||
|
handler: async (ctx, { playlistId, storageId }) => {
|
||||||
|
const playlist = await ctx.db.get("playlists", playlistId)
|
||||||
|
if (!playlist) {
|
||||||
|
await ctx.storage.delete(storageId)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
await deleteReplacedImage(ctx, playlist, storageId)
|
||||||
|
await ctx.db.patch(playlist._id, {
|
||||||
|
coverSource: "upload",
|
||||||
|
coverAlbums: undefined,
|
||||||
|
coverStorageId: storageId,
|
||||||
|
})
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
/** Draws automatic artwork for one playlist. Scheduled by `migratePlaylists`. */
|
||||||
|
export const refreshPlaylistCover = internalMutation({
|
||||||
|
args: { playlistId: v.id("playlists") },
|
||||||
|
handler: async (ctx, { playlistId }) => {
|
||||||
|
const playlist = await ctx.db.get("playlists", playlistId)
|
||||||
|
if (playlist) await refreshAutomaticCover(ctx, playlist, playlist.songs)
|
||||||
|
},
|
||||||
|
})
|
||||||
@@ -0,0 +1,123 @@
|
|||||||
|
"use node"
|
||||||
|
|
||||||
|
import * as fs from "node:fs/promises"
|
||||||
|
import { v } from "convex/values"
|
||||||
|
import * as jpeg from "jpeg-js"
|
||||||
|
import { internal } from "./_generated/api"
|
||||||
|
import { internalAction } from "./_generated/server"
|
||||||
|
import { imageDataUrlToBlob } from "./images"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Draws playlist artwork into storage. Pure JS on purpose: self-hosted Convex
|
||||||
|
* cannot install native packages like sharp for node actions. Album covers
|
||||||
|
* are always `cover.jpg` / `folder.jpg`, so JPEG in and out is enough.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** Stored playlist artwork is square, this many pixels a side. */
|
||||||
|
const COVER_SIZE = 1024
|
||||||
|
const JPEG_QUALITY = 88
|
||||||
|
|
||||||
|
type Rgba = { width: number; height: number; data: Uint8Array }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Center-crops `source` to a square and area-averages it into `target` at
|
||||||
|
* (`left`, `top`), `size` pixels a side. Averaging keeps downscaled covers
|
||||||
|
* smooth; tiny covers upscale blocky but intact.
|
||||||
|
*/
|
||||||
|
function drawSquare(
|
||||||
|
source: Rgba,
|
||||||
|
target: Rgba,
|
||||||
|
left: number,
|
||||||
|
top: number,
|
||||||
|
size: number
|
||||||
|
) {
|
||||||
|
const side = Math.min(source.width, source.height)
|
||||||
|
const offsetX = Math.floor((source.width - side) / 2)
|
||||||
|
const offsetY = Math.floor((source.height - side) / 2)
|
||||||
|
const scale = side / size
|
||||||
|
|
||||||
|
for (let y = 0; y < size; y++) {
|
||||||
|
const y0 = offsetY + Math.floor(y * scale)
|
||||||
|
const y1 = Math.max(y0 + 1, offsetY + Math.floor((y + 1) * scale))
|
||||||
|
for (let x = 0; x < size; x++) {
|
||||||
|
const x0 = offsetX + Math.floor(x * scale)
|
||||||
|
const x1 = Math.max(x0 + 1, offsetX + Math.floor((x + 1) * scale))
|
||||||
|
let r = 0
|
||||||
|
let g = 0
|
||||||
|
let b = 0
|
||||||
|
for (let sy = y0; sy < y1; sy++) {
|
||||||
|
let index = (sy * source.width + x0) * 4
|
||||||
|
for (let sx = x0; sx < x1; sx++, index += 4) {
|
||||||
|
r += source.data[index]
|
||||||
|
g += source.data[index + 1]
|
||||||
|
b += source.data[index + 2]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const count = (x1 - x0) * (y1 - y0)
|
||||||
|
const out = ((top + y) * target.width + left + x) * 4
|
||||||
|
target.data[out] = r / count
|
||||||
|
target.data[out + 1] = g / count
|
||||||
|
target.data[out + 2] = b / count
|
||||||
|
target.data[out + 3] = 255
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** One cover alone, or four tiled 2x2 left to right, top to bottom. */
|
||||||
|
async function drawCover(paths: string[]) {
|
||||||
|
const canvas: Rgba = {
|
||||||
|
width: COVER_SIZE,
|
||||||
|
height: COVER_SIZE,
|
||||||
|
data: new Uint8Array(COVER_SIZE * COVER_SIZE * 4),
|
||||||
|
}
|
||||||
|
const tile = paths.length === 1 ? COVER_SIZE : COVER_SIZE / 2
|
||||||
|
// One at a time: a decoded 3000px cover is ~36 MB.
|
||||||
|
for (const [index, path] of paths.entries()) {
|
||||||
|
const source = jpeg.decode(await fs.readFile(path), { useTArray: true })
|
||||||
|
drawSquare(
|
||||||
|
source,
|
||||||
|
canvas,
|
||||||
|
(index % 2) * tile,
|
||||||
|
Math.floor(index / 2) * tile,
|
||||||
|
tile
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return jpeg.encode(canvas, JPEG_QUALITY).data
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Draws album artwork into storage. Scheduled by `drawCoverAlbums`. */
|
||||||
|
export const renderPlaylistCover = internalAction({
|
||||||
|
args: { playlistId: v.id("playlists"), albumIds: v.array(v.id("albums")) },
|
||||||
|
handler: async (ctx, { playlistId, albumIds }) => {
|
||||||
|
const paths = await ctx.runQuery(internal.playlistCover.coverFilePaths, {
|
||||||
|
albumIds,
|
||||||
|
})
|
||||||
|
if (!paths) return
|
||||||
|
const image = await drawCover(paths)
|
||||||
|
const storageId = await ctx.storage.store(
|
||||||
|
new Blob([new Uint8Array(image)], { type: "image/jpeg" })
|
||||||
|
)
|
||||||
|
await ctx.runMutation(internal.playlistCover.attachRenderedCover, {
|
||||||
|
playlistId,
|
||||||
|
albumIds,
|
||||||
|
storageId,
|
||||||
|
})
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stores an uploaded data URL as is; clients send it square. Scheduled by
|
||||||
|
* `uploadPlaylistCover`.
|
||||||
|
*/
|
||||||
|
export const storeUploadedCover = internalAction({
|
||||||
|
args: { playlistId: v.id("playlists"), image: v.string() },
|
||||||
|
handler: async (ctx, { playlistId, image }) => {
|
||||||
|
const storageId = await ctx.storage.store(
|
||||||
|
imageDataUrlToBlob(image, "Cover")
|
||||||
|
)
|
||||||
|
await ctx.runMutation(internal.playlistCover.attachUploadedCover, {
|
||||||
|
playlistId,
|
||||||
|
storageId,
|
||||||
|
})
|
||||||
|
},
|
||||||
|
})
|
||||||
@@ -3,7 +3,14 @@ import { ConvexError, v } from "convex/values"
|
|||||||
import type { Doc, Id } from "./_generated/dataModel"
|
import type { Doc, Id } from "./_generated/dataModel"
|
||||||
import type { MutationCtx, QueryCtx } from "./_generated/server"
|
import type { MutationCtx, QueryCtx } from "./_generated/server"
|
||||||
import { mutation, query } from "./_generated/server"
|
import { mutation, query } from "./_generated/server"
|
||||||
import { chunk } from "./limits"
|
import { internal } from "./_generated/api"
|
||||||
|
import { parseImageDataUrl } from "./images"
|
||||||
|
import { chunk, COVER_GRID_ALBUMS } from "./limits"
|
||||||
|
import {
|
||||||
|
drawCoverAlbums,
|
||||||
|
playlistCoverVersion,
|
||||||
|
refreshAutomaticCover,
|
||||||
|
} from "./playlistCover"
|
||||||
import type {
|
import type {
|
||||||
BrowsePlaylistDetail,
|
BrowsePlaylistDetail,
|
||||||
BrowsePlaylistSummary,
|
BrowsePlaylistSummary,
|
||||||
@@ -25,7 +32,7 @@ function toPlaylistSummary(
|
|||||||
id: playlist._id,
|
id: playlist._id,
|
||||||
title: playlist.name,
|
title: playlist.name,
|
||||||
owner: ownerName,
|
owner: ownerName,
|
||||||
cover: playlist.cover,
|
cover: playlistCoverVersion(playlist),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,7 +113,7 @@ async function browsePlaylistSummaryHandler(
|
|||||||
id: owner._id,
|
id: owner._id,
|
||||||
name: owner.name,
|
name: owner.name,
|
||||||
},
|
},
|
||||||
cover: playlist.cover,
|
cover: playlistCoverVersion(playlist),
|
||||||
isOwner,
|
isOwner,
|
||||||
isSubscribed,
|
isSubscribed,
|
||||||
songIds: playlist.songs,
|
songIds: playlist.songs,
|
||||||
@@ -366,6 +373,8 @@ export const createPlaylist = mutation({
|
|||||||
cover: null,
|
cover: null,
|
||||||
songs: uniqueSongIds,
|
songs: uniqueSongIds,
|
||||||
})
|
})
|
||||||
|
const playlist = await ctx.db.get("playlists", playlistId)
|
||||||
|
await refreshAutomaticCover(ctx, playlist!, uniqueSongIds)
|
||||||
if (args.invocationId)
|
if (args.invocationId)
|
||||||
await ctx.db.insert("appIntentPlaylists", {
|
await ctx.db.insert("appIntentPlaylists", {
|
||||||
userId,
|
userId,
|
||||||
@@ -388,9 +397,9 @@ export const addSongsToPlaylist = mutation({
|
|||||||
const toAdd = uniqueIncoming.filter((songId) => !existing.has(songId))
|
const toAdd = uniqueIncoming.filter((songId) => !existing.has(songId))
|
||||||
|
|
||||||
if (toAdd.length > 0) {
|
if (toAdd.length > 0) {
|
||||||
await ctx.db.patch(playlist._id, {
|
const songs = [...playlist.songs, ...toAdd]
|
||||||
songs: [...playlist.songs, ...toAdd],
|
await ctx.db.patch(playlist._id, { songs })
|
||||||
})
|
await refreshAutomaticCover(ctx, playlist, songs)
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -408,9 +417,9 @@ export const removeSongsFromPlaylist = mutation({
|
|||||||
handler: async (ctx, args) => {
|
handler: async (ctx, args) => {
|
||||||
const { playlist } = await requirePlaylistOwner(ctx, args.playlistId)
|
const { playlist } = await requirePlaylistOwner(ctx, args.playlistId)
|
||||||
const remove = new Set(args.songIds)
|
const remove = new Set(args.songIds)
|
||||||
await ctx.db.patch(playlist._id, {
|
const songs = playlist.songs.filter((songId) => !remove.has(songId))
|
||||||
songs: playlist.songs.filter((songId) => !remove.has(songId)),
|
await ctx.db.patch(playlist._id, { songs })
|
||||||
})
|
await refreshAutomaticCover(ctx, playlist, songs)
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -430,13 +439,14 @@ export const reorderPlaylistSongs = mutation({
|
|||||||
throw new ConvexError("Invalid song order")
|
throw new ConvexError("Invalid song order")
|
||||||
}
|
}
|
||||||
await ctx.db.patch(playlist._id, { songs: args.songIds })
|
await ctx.db.patch(playlist._id, { songs: args.songIds })
|
||||||
|
await refreshAutomaticCover(ctx, playlist, args.songIds)
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
export const deletePlaylist = mutation({
|
export const deletePlaylist = mutation({
|
||||||
args: { playlistId: v.id("playlists") },
|
args: { playlistId: v.id("playlists") },
|
||||||
handler: async (ctx, args) => {
|
handler: async (ctx, args) => {
|
||||||
await requirePlaylistOwner(ctx, args.playlistId)
|
const { playlist } = await requirePlaylistOwner(ctx, args.playlistId)
|
||||||
|
|
||||||
const users = await ctx.db.query("users").collect()
|
const users = await ctx.db.query("users").collect()
|
||||||
for (const user of users) {
|
for (const user of users) {
|
||||||
@@ -466,10 +476,57 @@ export const deletePlaylist = mutation({
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (playlist.coverStorageId) {
|
||||||
|
await ctx.storage.delete(playlist.coverStorageId)
|
||||||
|
}
|
||||||
await ctx.db.delete("playlists", args.playlistId)
|
await ctx.db.delete("playlists", args.playlistId)
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Draws these album covers as the playlist's artwork: one alone, or four
|
||||||
|
* tiled 2x2. Replaces automatic or uploaded artwork.
|
||||||
|
*/
|
||||||
|
export const setPlaylistCoverAlbums = mutation({
|
||||||
|
args: {
|
||||||
|
playlistId: v.id("playlists"),
|
||||||
|
albumIds: v.array(v.id("albums")),
|
||||||
|
},
|
||||||
|
handler: async (ctx, { playlistId, albumIds }) => {
|
||||||
|
const { playlist } = await requirePlaylistOwner(ctx, playlistId)
|
||||||
|
if (
|
||||||
|
(albumIds.length !== 1 && albumIds.length !== COVER_GRID_ALBUMS) ||
|
||||||
|
new Set(albumIds).size !== albumIds.length
|
||||||
|
) {
|
||||||
|
throw new ConvexError("Pick one album or four different albums")
|
||||||
|
}
|
||||||
|
for (const albumId of albumIds) {
|
||||||
|
const album = await ctx.db.get("albums", albumId)
|
||||||
|
if (!album?.cover) {
|
||||||
|
throw new ConvexError("Album has no artwork")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
await drawCoverAlbums(ctx, playlist, albumIds, "albums")
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Takes an image data URL. It is cropped into storage in the background and
|
||||||
|
* the cover version changes once it lands.
|
||||||
|
*/
|
||||||
|
export const uploadPlaylistCover = mutation({
|
||||||
|
args: { playlistId: v.id("playlists"), image: v.string() },
|
||||||
|
handler: async (ctx, { playlistId, image }) => {
|
||||||
|
await requirePlaylistOwner(ctx, playlistId)
|
||||||
|
parseImageDataUrl(image, "Cover")
|
||||||
|
await ctx.scheduler.runAfter(
|
||||||
|
0,
|
||||||
|
internal.playlistCoverImages.storeUploadedCover,
|
||||||
|
{ playlistId, image }
|
||||||
|
)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
export const subscribeToPlaylist = mutation({
|
export const subscribeToPlaylist = mutation({
|
||||||
args: { playlistId: v.id("playlists") },
|
args: { playlistId: v.id("playlists") },
|
||||||
handler: async (ctx, args) => {
|
handler: async (ctx, args) => {
|
||||||
|
|||||||
@@ -148,7 +148,14 @@ export default defineSchema({
|
|||||||
playlists: defineTable({
|
playlists: defineTable({
|
||||||
name: v.string(),
|
name: v.string(),
|
||||||
owner: v.id("users"),
|
owner: v.id("users"),
|
||||||
|
/** Legacy and unused; artwork lives in `coverStorageId`. */
|
||||||
cover: v.nullable(v.string()),
|
cover: v.nullable(v.string()),
|
||||||
|
// Artwork, see `playlistCover.ts`. No `coverSource` means automatic.
|
||||||
|
coverSource: v.optional(v.union(v.literal("albums"), v.literal("upload"))),
|
||||||
|
/** Albums behind the drawn artwork: four tile 2x2, one shows alone. */
|
||||||
|
coverAlbums: v.optional(v.array(v.id("albums"))),
|
||||||
|
/** The drawn or uploaded artwork. */
|
||||||
|
coverStorageId: v.optional(v.id("_storage")),
|
||||||
songs: v.array(v.id("songs")),
|
songs: v.array(v.id("songs")),
|
||||||
}).index("by_owner", ["owner"]),
|
}).index("by_owner", ["owner"]),
|
||||||
})
|
})
|
||||||
@@ -15,7 +15,7 @@ import {
|
|||||||
} from "./_generated/server"
|
} from "./_generated/server"
|
||||||
import type { Doc, Id } from "./_generated/dataModel"
|
import type { Doc, Id } from "./_generated/dataModel"
|
||||||
import type { ActionCtx, MutationCtx } from "./_generated/server"
|
import type { ActionCtx, MutationCtx } from "./_generated/server"
|
||||||
import { MAX_PROFILE_IMAGE_BYTES } from "./limits"
|
import { imageDataUrlToBlob, parseImageDataUrl } from "./images"
|
||||||
import { deleteUserFavorites } from "./favoriteStore"
|
import { deleteUserFavorites } from "./favoriteStore"
|
||||||
import { deleteUserPlayback } from "./playbackState"
|
import { deleteUserPlayback } from "./playbackState"
|
||||||
|
|
||||||
@@ -138,6 +138,9 @@ async function deleteOwnedPlaylists(ctx: MutationCtx, userId: Id<"users">) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (const playlist of owned) {
|
for (const playlist of owned) {
|
||||||
|
if (playlist.coverStorageId) {
|
||||||
|
await ctx.storage.delete(playlist.coverStorageId)
|
||||||
|
}
|
||||||
await ctx.db.delete(playlist._id)
|
await ctx.db.delete(playlist._id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -188,23 +191,6 @@ export const listUsers = query({
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
const DATA_URL_PATTERN = /^data:(image\/[\w.+-]+);base64,([A-Za-z0-9+/=]+)$/
|
|
||||||
|
|
||||||
/** Splits an image data URL, rejecting anything over the size cap. */
|
|
||||||
function parseImageDataUrl(image: string) {
|
|
||||||
const match = DATA_URL_PATTERN.exec(image)
|
|
||||||
if (!match) {
|
|
||||||
throw new ConvexError("Profile photo must be an image")
|
|
||||||
}
|
|
||||||
const [, contentType, base64] = match
|
|
||||||
if (Math.floor((base64.length * 3) / 4) > MAX_PROFILE_IMAGE_BYTES) {
|
|
||||||
throw new ConvexError(
|
|
||||||
`Profile photo must be ${MAX_PROFILE_IMAGE_BYTES / 1024 / 1024} MB or smaller`
|
|
||||||
)
|
|
||||||
}
|
|
||||||
return { contentType, base64 }
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Path clients load the avatar from. The web server proxies it from Convex
|
* Path clients load the avatar from. The web server proxies it from Convex
|
||||||
* storage, since the backend's storage URLs are not reachable from browsers
|
* storage, since the backend's storage URLs are not reachable from browsers
|
||||||
@@ -231,7 +217,7 @@ export const updateProfileImage = mutation({
|
|||||||
await setProfileImage(ctx, userId, null)
|
await setProfileImage(ctx, userId, null)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
parseImageDataUrl(image)
|
parseImageDataUrl(image, "Profile photo")
|
||||||
await ctx.scheduler.runAfter(0, internal.users.storeProfileImage, {
|
await ctx.scheduler.runAfter(0, internal.users.storeProfileImage, {
|
||||||
userId,
|
userId,
|
||||||
image,
|
image,
|
||||||
@@ -243,10 +229,8 @@ export const updateProfileImage = mutation({
|
|||||||
export const storeProfileImage = internalAction({
|
export const storeProfileImage = internalAction({
|
||||||
args: { userId: v.id("users"), image: v.string() },
|
args: { userId: v.id("users"), image: v.string() },
|
||||||
handler: async (ctx, { userId, image }) => {
|
handler: async (ctx, { userId, image }) => {
|
||||||
const { contentType, base64 } = parseImageDataUrl(image)
|
|
||||||
const bytes = Uint8Array.from(atob(base64), (char) => char.charCodeAt(0))
|
|
||||||
const storageId = await ctx.storage.store(
|
const storageId = await ctx.storage.store(
|
||||||
new Blob([bytes], { type: contentType })
|
imageDataUrlToBlob(image, "Profile photo")
|
||||||
)
|
)
|
||||||
await ctx.runMutation(internal.users.attachProfileImage, {
|
await ctx.runMutation(internal.users.attachProfileImage, {
|
||||||
userId,
|
userId,
|
||||||
|
|||||||
@@ -51,6 +51,10 @@
|
|||||||
"types": "./client/playback-queue.ts",
|
"types": "./client/playback-queue.ts",
|
||||||
"default": "./client/playback-queue.ts"
|
"default": "./client/playback-queue.ts"
|
||||||
},
|
},
|
||||||
|
"./client/playlist-cover": {
|
||||||
|
"types": "./client/playlist-cover.ts",
|
||||||
|
"default": "./client/playlist-cover.ts"
|
||||||
|
},
|
||||||
"./client/playback-store": {
|
"./client/playback-store": {
|
||||||
"types": "./client/playback-store.ts",
|
"types": "./client/playback-store.ts",
|
||||||
"default": "./client/playback-store.ts"
|
"default": "./client/playback-store.ts"
|
||||||
@@ -74,6 +78,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@convex-dev/auth": "^0.0.93",
|
"@convex-dev/auth": "^0.0.93",
|
||||||
"convex": "^1.40.0",
|
"convex": "^1.40.0",
|
||||||
|
"jpeg-js": "0.4.4",
|
||||||
"music-metadata": "11.12.3"
|
"music-metadata": "11.12.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
Generated
+3
@@ -523,6 +523,9 @@ importers:
|
|||||||
convex:
|
convex:
|
||||||
specifier: ^1.40.0
|
specifier: ^1.40.0
|
||||||
version: 1.42.1(react@19.3.1)
|
version: 1.42.1(react@19.3.1)
|
||||||
|
jpeg-js:
|
||||||
|
specifier: 0.4.4
|
||||||
|
version: 0.4.4
|
||||||
music-metadata:
|
music-metadata:
|
||||||
specifier: ^11.16.0
|
specifier: ^11.16.0
|
||||||
version: 11.16.1(supports-color@8.1.1)
|
version: 11.16.1(supports-color@8.1.1)
|
||||||
|
|||||||
Reference in new issue
Block a user