mirror of
https://github.com/navidrome/navidrome.git
synced 2026-08-31 07:30:32 +00:00
* test(jellyfin): use canonical ids in dto fixtures
Fixtures used short placeholder strings, which are not valid Navidrome ids. Deriving them from
id.NewHash keeps the labels readable while exercising the real id shape.
* test(jellyfin): use canonical ids in handler fixtures
Fixtures used short placeholder strings, which are not valid Navidrome ids. Deriving them from
id.NewHash keeps the labels readable while exercising the real id shape. Playlist entry positions
stay decimal, matching the integer playlist_tracks.id column.
* test(jellyfin): use canonical ids in e2e fixtures
Fixtures used short placeholder strings, which are not valid Navidrome ids. Deriving them from
id.NewHash keeps the labels readable while exercising the real id shape.
* test(jellyfin): use canonical ids in audiomuse fixtures
audiomuse_test.go passes ids as bare function args (mf(id, ...), call(query, user)) rather than
via ID: struct-literal fields, so the original grep-built file list missed it. Same conversion as
the rest of the fixtures: fake labels through id.NewHash via testID.
* test(jellyfin): convert remaining nonexistent-id sentinels in e2e tests
Reviewer swept for enc("literal") sites the brief's dto.EncodeID grep missed. These "does not
exist" fixtures must stay well-formed GUIDs under the strict codec, or the test degrades from
"resolves to nothing" to "empty path segment".
* refactor(jellyfin): emit real 128-bit GUIDs as item ids
Navidrome ids are now a canonical 22-char base62 encoding of exactly 128 bits, so they map
losslessly onto Jellyfin GUIDs. Previously the API hex-encoded the id string itself, producing
44 hex chars where Jellyfin uses 32.
Integer library ids, the synthetic playlists folder, and playlist entry positions (a
playlist_tracks.id, an integer column) aren't 128-bit values, so they get a reserved GUID space
tagged by kind. DecodeID is now strict: malformed input returns an empty string instead of
passing through unchanged.
BREAKING: Jellyfin clients see entirely new item ids.
* fix(jellyfin): 404 malformed playlist ids instead of silently creating
updatePlaylist decoded a malformed playlistId to "", the same sentinel core/playlists.Create
uses to mean "make a new playlist" — the overload createPlaylist deliberately relies on. A
malformed id now 404s before reaching Create.
Also tightens id-codec test fixtures: several tests set chi params to a raw canonical id, which
now decodes to "" and only passed because the fakes ignore the id argument; and a batch of
not-found sentinels now use well-formed-but-nonexistent GUIDs so they exercise the intended path
instead of the malformed-id path. READMEs "lossless" claim softened to note the reserved space.
* refactor(jellyfin): drop the id truncation workaround
Finamp's saved-queue packing keeps the first 16 bytes of each item id. That was lossy only
because our ids were 44 hex chars; now they are 32, so the packing round-trips exactly and the
server-side prefix recovery is dead code.
Removes an indexed range scan per restored queue and the ambiguous-prefix path that could
resolve to the wrong item.
* fix(jellyfin): emit ServerId and PlaySessionId in Jellyfin's id format
Jellyfin serializes GUIDs without dashes; ServerId was emitting the dashed UUID form. A
ServerId persisted before this change is normalized on read rather than rewritten.
PlaySessionId was emitting a raw internal id instead of the encoded form.
BREAKING: the ServerId change makes clients treat the server as new, so users re-login once.
* fix(jellyfin): 404 on undecodable id filters instead of widening the query
DecodeID collapsed an absent param and an undecodable one into the empty string, and downstream
an empty id means no filter. A client sending a stale pre-upgrade id therefore had its filter
silently dropped: ParentId, ArtistIds and AlbumArtistIds each returned the whole library instead
of a scoped result. Every existing client hits this on first launch after the id format changes.
Scalar id params now distinguish the two cases and report not-found. List-valued params already
failed closed. EncodeID logs a diagnostic when a non-empty id is not canonical, which should not
happen post-migration and would otherwise ship an unaddressable item silently.
* test(jellyfin): drop comments that restate the spec names
* refactor(jellyfin): decode reserved GUIDs from bytes, not hex strings
DecodeID already had the 16 decoded bytes, then re-derived the kind tag and payload by slicing the
hex string and parsing it a second time. Reading them off the byte slice matches how the format is
specified and removes the duplicate parse.
Bounding the payload inside encodeReserved gives both encoders the 32-char guarantee, which only
EncodePlaylistEntryID enforced before.
Playlist entries now decode through DecodePlaylistEntryID, which rejects other kinds. The tag was
being encoded and then discarded, so a song id passed as an EntryId reached RemoveTracks as a
playlist_tracks position.
Drops the per-field log.Warn from EncodeID: it sat in a leaf codec without a ctx and would emit
once per item per request on exactly the bad-data population it was meant to surface.
* refactor(jellyfin): make DecodeID report whether the id was decodable
DecodeID returned the empty string for both an absent param and an undecodable one, and
downstream an empty id means no filter. That conflation is what let a stale id widen /Items to
the whole library; it had been patched at two call sites, leaving three different policies for an
undecodable id in one package and ~16 handlers correct only because a repo Get("") happens to fail.
Returning (string, bool) makes the ambiguity unrepresentable, and the compiler forces each of the
~22 sites to decide. URL params share one itemIDParam helper that 404s; id lists go through
DecodeIDs, which is all-or-nothing because dropping bad entries would empty a list and make its
len() > 0 filter gate vanish — the original bug by another route.
A well-formed but unknown id is still 200 with zero results; only malformed ids 404. Malformed
ids now also 404 on the image and similar/instant-mix routes, which previously answered with a
placeholder or an empty list.
144 lines
4.2 KiB
Go
144 lines
4.2 KiB
Go
package dto
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/hex"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/navidrome/navidrome/model/id"
|
|
)
|
|
|
|
// guidLen is the length of a Jellyfin GUID on the wire: 16 bytes as lowercase hex, no dashes
|
|
// (what Guid.ToString("N") produces).
|
|
const guidLen = 32
|
|
|
|
// Reserved GUIDs stand in for ids that aren't 128-bit values: 12 zero bytes, a non-zero kind tag,
|
|
// then a 24-bit payload. The tag is never zero because Jellyfin serializes the all-zero GUID as null.
|
|
const (
|
|
kindIdx = 12
|
|
maxPayload = 1<<24 - 1
|
|
)
|
|
|
|
const (
|
|
kindLibrary byte = iota + 1
|
|
kindPlaylistsFolder
|
|
kindPlaylistEntry
|
|
)
|
|
|
|
var zeroPrefix [kindIdx]byte
|
|
|
|
// PlaylistsFolderID is the internal id of the synthetic "playlists library" folder. It can't be
|
|
// mistaken for a real id, which is always 22-char base62.
|
|
const PlaylistsFolderID = "playlists"
|
|
|
|
// PlaylistsFolderGUID is the wire form of PlaylistsFolderID.
|
|
var PlaylistsFolderGUID = encodeReserved(kindPlaylistsFolder, 0)
|
|
|
|
// EncodeID renders a canonical Navidrome id as a Jellyfin GUID. Anything that isn't one encodes
|
|
// to "" rather than to a shape clients can't parse.
|
|
func EncodeID(ndID string) string {
|
|
b, err := id.Decode(ndID)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return hex.EncodeToString(b)
|
|
}
|
|
|
|
// EncodeLibraryID renders a library's integer id in the reserved GUID space.
|
|
func EncodeLibraryID(libID int) string {
|
|
return encodeReserved(kindLibrary, libID)
|
|
}
|
|
|
|
// EncodePlaylistEntryID renders a playlist entry's position (model.PlaylistTrack.ID, an integer
|
|
// column) in the reserved GUID space. Clients echo it back to remove one occurrence of a song.
|
|
func EncodePlaylistEntryID(entryID string) string {
|
|
n, err := strconv.Atoi(entryID)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return encodeReserved(kindPlaylistEntry, n)
|
|
}
|
|
|
|
func encodeReserved(kind byte, payload int) string {
|
|
if payload < 0 || payload > maxPayload {
|
|
return ""
|
|
}
|
|
var b [16]byte
|
|
b[kindIdx] = kind
|
|
b[13], b[14], b[15] = byte(payload>>16), byte(payload>>8), byte(payload)
|
|
return hex.EncodeToString(b[:])
|
|
}
|
|
|
|
// DecodeID maps an inbound GUID back to the identifier the rest of the API uses: a canonical id,
|
|
// a decimal library id, or PlaylistsFolderID. ok is false for anything that isn't a well-formed
|
|
// GUID — including the empty string — so an undecodable id can't reach a caller as "no filter".
|
|
// Dashed and uppercase forms are accepted, as Jellyfin's Guid.Parse accepts them. Playlist entries
|
|
// decode through DecodePlaylistEntryID instead, so a position can't reach a caller expecting an
|
|
// entity id.
|
|
func DecodeID(guid string) (string, bool) {
|
|
b, ok := decodeGUID(guid)
|
|
if !ok {
|
|
return "", false
|
|
}
|
|
kind, payload, reserved := reservedFields(b)
|
|
if !reserved {
|
|
return id.Encode(b), true
|
|
}
|
|
switch kind {
|
|
case kindLibrary:
|
|
return strconv.Itoa(payload), true
|
|
case kindPlaylistsFolder:
|
|
return PlaylistsFolderID, true
|
|
}
|
|
return "", false
|
|
}
|
|
|
|
// DecodeIDs decodes a list of GUIDs, all-or-nothing: ok is false if any entry is malformed, so a
|
|
// caller can't mistake "every entry failed" for "no filter" (see DecodeID).
|
|
func DecodeIDs(guids []string) ([]string, bool) {
|
|
out := make([]string, len(guids))
|
|
for i, guid := range guids {
|
|
decoded, ok := DecodeID(guid)
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
out[i] = decoded
|
|
}
|
|
return out, true
|
|
}
|
|
|
|
// DecodePlaylistEntryID decodes a playlist entry GUID to its position. It rejects every other kind,
|
|
// so an entity id can't be taken for a playlist_tracks row.
|
|
func DecodePlaylistEntryID(guid string) (string, bool) {
|
|
b, ok := decodeGUID(guid)
|
|
if !ok {
|
|
return "", false
|
|
}
|
|
if kind, payload, reserved := reservedFields(b); reserved && kind == kindPlaylistEntry {
|
|
return strconv.Itoa(payload), true
|
|
}
|
|
return "", false
|
|
}
|
|
|
|
func decodeGUID(guid string) ([16]byte, bool) {
|
|
guid = strings.ToLower(strings.ReplaceAll(guid, "-", ""))
|
|
if len(guid) != guidLen {
|
|
return [16]byte{}, false
|
|
}
|
|
bs, err := hex.DecodeString(guid)
|
|
if err != nil {
|
|
return [16]byte{}, false
|
|
}
|
|
return [16]byte(bs), true
|
|
}
|
|
|
|
// reservedFields reports the kind tag and payload of a reserved GUID; reserved is false for the
|
|
// entity GUIDs that make up almost all traffic.
|
|
func reservedFields(b [16]byte) (kind byte, payload int, reserved bool) {
|
|
if !bytes.Equal(b[:kindIdx], zeroPrefix[:]) {
|
|
return 0, 0, false
|
|
}
|
|
return b[kindIdx], int(b[13])<<16 | int(b[14])<<8 | int(b[15]), true
|
|
}
|