Four extractions, each chosen because the two halves have different callers,
different failure modes or different testing needs. Not a line-count exercise.
internal/home out of internal/media
- media serves item RESOURCES (detail, file, cover, progress); home computes
AGGREGATES (the five shelves). The 2328-line package was doing both.
- media keeps POST /items/{id}/view because it is item-scoped and must stay in
the package that authorizes the item; it imports home.RecordView for that one
thing. home does not import media.
- Verified live: all five shelves non-empty, and two consecutive /home calls
return different explore ids, which is the non-determinism the Explore rewrite
exists for.
internal/scanner out of internal/library
- The scanner walks the filesystem and reconciles; library CRUD is an admin HTTP
surface. Different callers (a job vs a request), different failure modes.
- The registry deliberately STAYED in internal/library: three of its four callers
are not the scanner (modules register types, handlers serve the list, metadata
reads the group mode, subsonic calls CleanTitle). It is a property of the kind,
not of the scan. The one genuinely scanner-owned helper became
library.ExtensionsFor.
- Two tests moved with the code rather than being rewritten; they now exercise
the boundary by calling library.ListMissing across it.
- Verified live: first scan updates, rescan reports unchanged=N, so change
detection still short-circuits.
internal/providers out of internal/metadata
- Providers talk to third-party APIs; the enrich job owns the matching policy and
the database writes. The split's whole point is that a provider is testable
WITHOUT a database, so a DB-free test was added and OpenLibrary gained a
BaseURL override to make it possible.
- providers imports zero internal packages — a true leaf.
- The matching policy (searchTitle, artistHint, pickBest) is untouched. It exists
because real false matches were found.
dto packages out of internal/jellyfin and internal/subsonic
- The wire types and their mapping are now separate from the handler logic, so
the contract with clients we do not control is reviewable on its own.
- Both dto packages are PURE: zero references to sql.DB or net/http, verified by
grep. That is what makes them reviewable.
- No field name, tag or error code changed.
internal/arch did its job: it failed the build twice during this phase, once for
each new package that was not yet placed in a layer. That is the rule working,
not an obstacle.
Verified: gofmt clean, build exit 0, vet exit 0, 23 packages pass, svelte-check
0/0, and end to end against a running server — home shelves, a queued scan
reporting unchanged, Jellyfin /Items, and Subsonic ping.
Process note: one agent (dto) stopped mid-task and left the tree red — the dto
packages unlayered and its test still referencing constants that had moved. The
arch test caught it immediately. I finished it by hand: exported constant
references in the test, and both dto packages placed at layer 3.
Still open from Phase 1: internal/jellyfin has no test files, catalog is
item-centric while Subsonic is file-first, and subsonic loadAlbum now scopes to
the item's own library.
127 lines
3.9 KiB
Go
127 lines
3.9 KiB
Go
package jellyfin
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/mithral/vellum/internal/auth"
|
|
"github.com/mithral/vellum/internal/catalog"
|
|
"github.com/mithral/vellum/internal/httpx"
|
|
"github.com/mithral/vellum/internal/jellyfin/dto"
|
|
"github.com/mithral/vellum/internal/stream"
|
|
)
|
|
|
|
// handlePlaybackInfo answers GET /Items/{itemId}/PlaybackInfo.
|
|
//
|
|
// The response always describes the one thing Vellum can do — direct play — and
|
|
// reports SupportsTranscoding=false. If the client has disabled both direct
|
|
// play and direct stream it is asking for a transcode; that is refused with
|
|
// ErrorCode "NoCompatibleStream" and a warning, rather than by handing back a
|
|
// TranscodingUrl that would not exist.
|
|
func (h *Handlers) handlePlaybackInfo(w http.ResponseWriter, r *http.Request, u auth.User) {
|
|
id, isLib, ok := dto.DecodeID(r.PathValue("itemId"))
|
|
if !ok || isLib {
|
|
httpx.WriteError(w, http.StatusNotFound, "item not found")
|
|
return
|
|
}
|
|
it, ok := h.authorizedItem(w, r, u, id)
|
|
if !ok {
|
|
return
|
|
}
|
|
f, err := h.firstFile(r.Context(), id)
|
|
if err != nil {
|
|
h.fail(w, "load file facts", err)
|
|
return
|
|
}
|
|
|
|
q := r.URL.Query()
|
|
directPlay := queryBoolDefault(q.Get("EnableDirectPlay"), true)
|
|
directStream := queryBoolDefault(q.Get("EnableDirectStream"), true)
|
|
|
|
src := dto.BuildMediaSource(it, f, tokenFromRequest(r))
|
|
resp := dto.PlaybackInfoResponse{MediaSources: []dto.MediaSourceInfo{src}}
|
|
if !directPlay && !directStream {
|
|
resp.ErrorCode = "NoCompatibleStream"
|
|
h.log.Warn("jellyfin client requires transcoding; refusing",
|
|
"item", it.ID, "kind", it.Kind)
|
|
}
|
|
httpx.WriteJSON(w, http.StatusOK, resp)
|
|
}
|
|
|
|
// handleVideoStream serves a video item's file.
|
|
func (h *Handlers) handleVideoStream(w http.ResponseWriter, r *http.Request, u auth.User) {
|
|
h.serveStream(w, r, u)
|
|
}
|
|
|
|
// handleAudioStream serves an audio (or other non-video) item's file. Jellyfin
|
|
// clients fetch audiobook and music tracks here.
|
|
func (h *Handlers) handleAudioStream(w http.ResponseWriter, r *http.Request, u auth.User) {
|
|
h.serveStream(w, r, u)
|
|
}
|
|
|
|
// serveStream streams the item's first file through internal/stream, so Range
|
|
// requests (seeking) work exactly as they do for the web app. Auth already came
|
|
// from tokenFromRequest, which includes the ?api_key= form that <video>/<audio>
|
|
// elements require.
|
|
func (h *Handlers) serveStream(w http.ResponseWriter, r *http.Request, u auth.User) {
|
|
// Accept /stream and /stream.<ext> (stream.mp4, stream.mkv, ...). The
|
|
// subtree pattern also catches other suffixes; refuse those.
|
|
last := path.Base(strings.TrimSuffix(r.URL.Path, "/"))
|
|
if last != "stream" && !strings.HasPrefix(last, "stream.") {
|
|
httpx.WriteError(w, http.StatusNotFound, "not found")
|
|
return
|
|
}
|
|
|
|
id, isLib, ok := dto.DecodeID(r.PathValue("itemId"))
|
|
if !ok || isLib {
|
|
httpx.WriteError(w, http.StatusNotFound, "item not found")
|
|
return
|
|
}
|
|
it, ok := h.authorizedItem(w, r, u, id)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
f, err := catalog.FirstFile(r.Context(), h.db, id)
|
|
if errors.Is(err, catalog.ErrNotFound) {
|
|
httpx.WriteError(w, http.StatusNotFound, "item has no file")
|
|
return
|
|
}
|
|
if err != nil {
|
|
h.fail(w, "load file", err)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Disposition", "inline")
|
|
abs := filepath.Join(it.RootPath, filepath.FromSlash(f.Path))
|
|
if err := stream.ServeFile(w, r, abs); err != nil {
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
httpx.WriteError(w, http.StatusNotFound, "file is not available")
|
|
return
|
|
}
|
|
if errors.Is(err, stream.ErrNotAFile) {
|
|
httpx.WriteError(w, http.StatusBadRequest, "item is not a file")
|
|
return
|
|
}
|
|
h.fail(w, "serve file", err)
|
|
return
|
|
}
|
|
}
|
|
|
|
// queryBoolDefault parses a Jellyfin boolean query parameter, returning def
|
|
// when it is absent or unparseable.
|
|
func queryBoolDefault(v string, def bool) bool {
|
|
switch strings.ToLower(strings.TrimSpace(v)) {
|
|
case "":
|
|
return def
|
|
case "1", "true", "yes":
|
|
return true
|
|
case "0", "false", "no":
|
|
return false
|
|
}
|
|
return def
|
|
}
|