From 797e7f0b45d613499f6d9ebf6d609161b15286b5 Mon Sep 17 00:00:00 2001 From: maliavko Date: Thu, 19 Nov 2026 19:09:32 +0100 Subject: [PATCH] fix(plex): stream parts as the original file Plex answered HTTP 500 for newly added FLAC albums. Its log showed why: for a /library/parts request it checks the file against the client's direct-play profile, cliamp sends none, and the generic profile has no FLAC over HTTP ("No direct play music profile exists for protocol http, with container flac"). Request parts with download=1, as Plexamp does, so Plex serves the original file without a profile check. Verified with make check (gofmt, go vet, go test ./...) and staticcheck. The new test fails without the fix. On the live server a part answered 500 before and 206 with download=1 (2026-10-27). Co-Authored-By: Claude Opus 5.5 --- docs/plex.md | 4 ++-- external/plex/client.go | 7 ++++++- external/plex/client_test.go | 32 +++++++++++++++++++++++++++++++- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/docs/plex.md b/docs/plex.md index 66177b642..88d0167cb 100644 --- a/docs/plex.md +++ b/docs/plex.md @@ -73,10 +73,10 @@ provider = "plex" cliamp calls the Plex HTTP API to list audio playlists (`/playlists`) and music library albums. When you select an entry, cliamp gets its track list from the playlist-items or album-children endpoint. It then creates authenticated streaming URLs in this form: ``` -http://:32400/library/parts///file.?X-Plex-Token= +http://:32400/library/parts///file.?download=1&X-Plex-Token= ``` -These URLs serve files directly. Plex serves the original file without transcoding. cliamp handles playback through its HTTP streaming pipeline. cliamp can play MP3, FLAC, AAC, OGG, OPUS, WAV, and other supported source formats. +These URLs serve files directly. Plex serves the original file without transcoding: cliamp asks for it with `download=1`, as Plexamp does, so Plex does not check the file against a client profile. Without it Plex refuses FLAC files it has analyzed with HTTP 500. cliamp handles playback through its HTTP streaming pipeline. cliamp can play MP3, FLAC, AAC, OGG, OPUS, WAV, and other supported source formats. ## Troubleshooting diff --git a/external/plex/client.go b/external/plex/client.go index 1a8eacc16..24d304cd0 100644 --- a/external/plex/client.go +++ b/external/plex/client.go @@ -377,8 +377,13 @@ func (c *Client) Search(query string) ([]Track, error) { // StreamURL returns the authenticated HTTP URL for streaming a track part. // partKey is the relative path from the Part element, e.g. "/library/parts/…/file.flac". // The token is appended as a query parameter; Plex accepts it in either header or query form. +// +// download=1 asks for the original file, as Plexamp does. Without it Plex +// checks the part against the client's direct-play profile; cliamp has none, +// so Plex falls back to a generic profile without FLAC and answers 500 for +// every FLAC whose audio stream it has analyzed. func (c *Client) StreamURL(partKey string) string { - return c.baseURL + partKey + "?X-Plex-Token=" + url.QueryEscape(c.token) + return c.baseURL + partKey + "?download=1&X-Plex-Token=" + url.QueryEscape(c.token) } // IsStreamURL reports whether the given URL looks like a Plex library part diff --git a/external/plex/client_test.go b/external/plex/client_test.go index 2c9cf224c..88394108c 100644 --- a/external/plex/client_test.go +++ b/external/plex/client_test.go @@ -622,7 +622,7 @@ func TestTracks_MissingMedia(t *testing.T) { func TestStreamURL_Format(t *testing.T) { c := NewClient("http://192.168.1.10:32400", "mytoken") got := c.StreamURL("/library/parts/100/111/file.flac") - want := "http://192.168.1.10:32400/library/parts/100/111/file.flac?X-Plex-Token=mytoken" + want := "http://192.168.1.10:32400/library/parts/100/111/file.flac?download=1&X-Plex-Token=mytoken" if got != want { t.Errorf("StreamURL:\n got %q\n want %q", got, want) } @@ -720,3 +720,33 @@ func TestPingRejectsOversizedBody(t *testing.T) { t.Fatalf("Ping() error = %v, want httpclient.ErrTooLarge", err) } } + +// A part whose analyzed stream fails Plex's generic client profile answers +// 500 to a plain request; the original file request with download=1, as +// Plexamp sends it, plays. +func TestStreamURLPlaysAnalyzedFLAC(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Query().Get("download") != "1" { + http.Error(w, "No direct play music profile exists for protocol http, with container flac", http.StatusInternalServerError) + return + } + if got := r.URL.Query().Get("X-Plex-Token"); got != "tok" { + t.Errorf("token = %q", got) + } + w.Header().Set("Content-Type", "audio/flac") + _, _ = w.Write([]byte("fLaC")) + })) + defer srv.Close() + + resp, err := http.Get(NewClient(srv.URL, "tok").StreamURL("/library/parts/60716/1793101096/file.flac")) + if err != nil { + t.Fatalf("GET: %v", err) + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + t.Fatalf("status = %d, want 200", resp.StatusCode) + } + if !IsStreamURL(resp.Request.URL.String()) { + t.Fatal("stream URL no longer routes through the buffered pipeline") + } +}