Direct play only. The server is a headless GPU-less Xeon: nothing is ever transcoded or remuxed. A file either plays in the client or it does not. Measured, not assumed (ffmpeg-generated files with known codecs, played through Vellum's own stream endpoint in Chrome 155 / Linux): mp4 h264+aac plays mkv h264+aac PLAYS <- container is fine mov h264+aac plays webm vp9/opus plays mp4 av1+opus plays webm vp8/vorbis plays mp4 hevc+aac AUDIO ONLY, videoWidth stays 0, no error mkv hevc+flac AUDIO ONLY avi mpeg4+mp3 fails outright All seven audio formats play. Two conclusions drive the design: codec matters and container barely does (mkv+h264 plays, mp4+HEVC does not), and canPlayType is unreliable in both directions — it says "no" for mov+h264 (which plays) and "maybe" for mkv+hevc (whose video never decodes). Therefore playability is a property of the CLIENT, not the file. The server never declares a file playable; it reports codec facts and each client decides. That is what makes adding mpv/libvlc/MDK later a matter of registering an engine rather than re-architecting. Wave 4 - audio depth - New MusicPlayer.svelte: real queue, shuffle that walks a fixed permutation (so "previous" means something and nothing repeats early), repeat off/all/one, per-track rows, album art, next-track preload. - AudiobookPlayer: MediaSession (OS media keys + lock screen), playback speed that SURVIVES a chapter change and persists, sleep timer with countdown that a manual pause cancels, keyboard shortcuts, next-chapter preload. - Shared mediaSession.ts written by the coordinator before dispatch so both players consume one implementation instead of inventing two. Wave 5 - ffprobe facts - internal/probe (ffprobe wrapper, optional dependency), migration 0010 adding vcodec/acodec/width/height/probed_at to media_files, scanner hook, API fields. - Probing runs only for NEW or CHANGED files and BEFORE the write transaction opens, so it can never hold SQLite's write lock and a rescan never becomes a full-library ffprobe sweep. Proven: 797ms probing scan, then 1ms rescan with probed_at byte-identical. - ffprobe skips attached_pic so an mp3 with cover art reports no video codec. - Fixed a real hole: .avi is in the video registry but had no MIME entry, so it was served as application/octet-stream and could never play. Wave 6 - the video player - VideoPlayer.svelte with resume, progress + keepalive final flush, MediaSession, keyboard shortcuts, multi-file picker, and codec facts shown. - The honest path is the feature: an HEVC file makes Chrome play audio over a blank frame with no error, which is the worst possible outcome. The player detects it and says so, naming the codec and pointing at clients that can. Coordinator fix to Wave 6: the agent's detection hung off `loadedmetadata`, but an explicit `type` the browser cannot satisfy makes it reject the source during selection, so NEITHER loadedmetadata NOR error fires — and the player showed a working-looking 0:00 with a Play button, exactly the silently-broken state the component exists to prevent. Added a load watchdog: if the element says nothing within 2.5s, the source was rejected and the honest notice is shown. Verified per file afterwards (h264/mkv play at 640 wide; hevc and mpeg4 show their notices). Verified end to end, not just compiled: 15 packages pass (including the new internal/probe), gofmt/vet clean, svelte-check 0/0, and every claim above checked against a running server. Note the demo's own mp3s are 100 KB of random bytes and cannot decode; the audio players were proven against generated real audio instead.
374 lines
10 KiB
Go
374 lines
10 KiB
Go
package media
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
"log/slog"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
|
|
"github.com/mithral/vellum/internal/auth"
|
|
"github.com/mithral/vellum/internal/httpx"
|
|
"github.com/mithral/vellum/internal/progress"
|
|
"github.com/mithral/vellum/internal/stream"
|
|
)
|
|
|
|
// Handlers serves the generic per-item endpoints.
|
|
type Handlers struct {
|
|
db *sql.DB
|
|
log *slog.Logger
|
|
auth *auth.Authenticator
|
|
cacheDir string
|
|
}
|
|
|
|
// NewHandlers builds the media HTTP layer. cacheDir is where covers and
|
|
// extracted pages are written.
|
|
func NewHandlers(db *sql.DB, log *slog.Logger, a *auth.Authenticator, cacheDir string) *Handlers {
|
|
return &Handlers{db: db, log: log, auth: a, cacheDir: cacheDir}
|
|
}
|
|
|
|
// Register mounts the routes.
|
|
func (h *Handlers) Register(mux *http.ServeMux) {
|
|
mux.Handle("GET /api/v1/home", h.auth.Require(h.handleHome))
|
|
mux.Handle("GET /api/v1/items", h.auth.Require(h.handleListItems))
|
|
mux.Handle("GET /api/v1/items/{id}", h.auth.Require(h.handleItem))
|
|
mux.Handle("POST /api/v1/items/{id}/view", h.auth.Require(h.handleRecordView))
|
|
mux.Handle("GET /api/v1/items/{id}/authors", h.auth.Require(h.handleAuthors))
|
|
mux.Handle("GET /api/v1/items/{id}/file", h.auth.Require(h.handleFile))
|
|
mux.Handle("GET /api/v1/items/{id}/files/{fileID}", h.auth.Require(h.handleFileByID))
|
|
mux.Handle("GET /api/v1/items/{id}/cover", h.auth.Require(h.handleCover))
|
|
mux.Handle("GET /api/v1/items/{id}/progress", h.auth.Require(h.handleGetProgress))
|
|
mux.Handle("PUT /api/v1/items/{id}/progress", h.auth.Require(h.handlePutProgress))
|
|
mux.Handle("GET /api/v1/progress/recent", h.auth.Require(h.handleRecent))
|
|
}
|
|
|
|
func (h *Handlers) handleItem(w http.ResponseWriter, r *http.Request, u auth.User) {
|
|
it, ok := h.authorized(w, r, u)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
files, err := h.files(r, it.ID)
|
|
if err != nil {
|
|
h.fail(w, "list files", err)
|
|
return
|
|
}
|
|
|
|
prog, has, err := progress.Get(r.Context(), h.db, u.ID, it.ID)
|
|
if err != nil {
|
|
h.fail(w, "get progress", err)
|
|
return
|
|
}
|
|
|
|
authors, err := h.authors(r, it.ID)
|
|
if err != nil {
|
|
h.fail(w, "authors", err)
|
|
return
|
|
}
|
|
|
|
out := map[string]any{
|
|
"item": map[string]any{
|
|
"id": it.ID,
|
|
"library_id": it.LibraryID,
|
|
"kind": it.Kind,
|
|
"path": it.Path,
|
|
"title": it.Title,
|
|
"year": it.Year,
|
|
"missing": it.Missing,
|
|
},
|
|
"files": files,
|
|
"has_cover": h.hasCover(r, it),
|
|
"authors": authors,
|
|
}
|
|
if has {
|
|
out["progress"] = prog
|
|
}
|
|
httpx.WriteJSON(w, http.StatusOK, out)
|
|
}
|
|
|
|
// authors returns the credited people for an item.
|
|
func (h *Handlers) authors(r *http.Request, itemID int64) ([]string, error) {
|
|
rows, err := h.db.QueryContext(r.Context(), `
|
|
SELECT p.name FROM item_people ip
|
|
JOIN people p ON p.id = ip.person_id
|
|
WHERE ip.item_id = ? ORDER BY ip.ord`, itemID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
out := []string{}
|
|
for rows.Next() {
|
|
var name string
|
|
if err := rows.Scan(&name); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, name)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
func (h *Handlers) handleFile(w http.ResponseWriter, r *http.Request, u auth.User) {
|
|
it, ok := h.authorized(w, r, u)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
// Toolbar download vs inline. Inline is the default for readers.
|
|
disposition := "inline"
|
|
if r.URL.Query().Get("download") == "1" {
|
|
disposition = "attachment"
|
|
allowed, err := auth.CanAccessLibrary(r.Context(), h.db, u, it.LibraryID)
|
|
if err != nil || !allowed {
|
|
httpx.WriteError(w, http.StatusForbidden, "download not permitted")
|
|
return
|
|
}
|
|
}
|
|
w.Header().Set("Content-Disposition", disposition+`; filename="`+sanitizeFilename(it.Path)+`"`)
|
|
|
|
if err := stream.ServeFile(w, r, it.AbsPath()); 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
|
|
}
|
|
}
|
|
|
|
// handleFileByID streams one specific file of an item, for multi-file items
|
|
// such as audiobook chapters or album tracks.
|
|
func (h *Handlers) handleFileByID(w http.ResponseWriter, r *http.Request, u auth.User) {
|
|
it, ok := h.authorized(w, r, u)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
fileID, err := strconv.ParseInt(r.PathValue("fileID"), 10, 64)
|
|
if err != nil || fileID <= 0 {
|
|
httpx.WriteError(w, http.StatusBadRequest, "invalid file id")
|
|
return
|
|
}
|
|
|
|
// Confirm the file belongs to this item, so an item id cannot be paired
|
|
// with another item's file id.
|
|
var relPath string
|
|
err = h.db.QueryRowContext(r.Context(),
|
|
`SELECT path FROM media_files WHERE id = ? AND item_id = ?`, fileID, it.ID).Scan(&relPath)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
httpx.WriteError(w, http.StatusNotFound, "file not found")
|
|
return
|
|
}
|
|
if err != nil {
|
|
h.fail(w, "load file", err)
|
|
return
|
|
}
|
|
|
|
abs := filepath.Join(it.RootPath, filepath.FromSlash(relPath))
|
|
w.Header().Set("Content-Disposition", "inline")
|
|
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
|
|
}
|
|
h.fail(w, "serve file", err)
|
|
return
|
|
}
|
|
}
|
|
|
|
func (h *Handlers) handleCover(w http.ResponseWriter, r *http.Request, u auth.User) {
|
|
it, ok := h.authorized(w, r, u)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
path, found, err := CoverPath(r.Context(), h.db, it, h.cacheDir)
|
|
if err != nil {
|
|
h.fail(w, "cover", err)
|
|
return
|
|
}
|
|
if !found {
|
|
httpx.WriteError(w, http.StatusNotFound, "no cover available")
|
|
return
|
|
}
|
|
w.Header().Set("Cache-Control", "public, max-age=86400")
|
|
if err := stream.ServeFile(w, r, path); err != nil {
|
|
h.fail(w, "serve cover", err)
|
|
return
|
|
}
|
|
}
|
|
|
|
func (h *Handlers) handleGetProgress(w http.ResponseWriter, r *http.Request, u auth.User) {
|
|
it, ok := h.authorized(w, r, u)
|
|
if !ok {
|
|
return
|
|
}
|
|
prog, found, err := progress.Get(r.Context(), h.db, u.ID, it.ID)
|
|
if err != nil {
|
|
h.fail(w, "get progress", err)
|
|
return
|
|
}
|
|
if !found {
|
|
httpx.WriteJSON(w, http.StatusOK, map[string]any{"progress": nil})
|
|
return
|
|
}
|
|
httpx.WriteJSON(w, http.StatusOK, map[string]any{"progress": prog})
|
|
}
|
|
|
|
func (h *Handlers) handlePutProgress(w http.ResponseWriter, r *http.Request, u auth.User) {
|
|
it, ok := h.authorized(w, r, u)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
var req struct {
|
|
FileID int64 `json:"file_id"`
|
|
Position *int64 `json:"position"`
|
|
Duration int64 `json:"duration"`
|
|
Percent float64 `json:"percent"`
|
|
Finished bool `json:"finished"`
|
|
}
|
|
if err := httpx.DecodeJSON(r, &req); err != nil {
|
|
httpx.WriteError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
p := progress.Progress{
|
|
ItemID: it.ID,
|
|
FileID: req.FileID,
|
|
Kind: it.Kind,
|
|
Duration: req.Duration,
|
|
Percent: req.Percent,
|
|
Finished: req.Finished,
|
|
}
|
|
if req.Position != nil {
|
|
p.Position = *req.Position
|
|
}
|
|
|
|
saved, err := progress.Put(r.Context(), h.db, u.ID, p)
|
|
if errors.Is(err, progress.ErrNoFile) {
|
|
httpx.WriteError(w, http.StatusConflict, "item has no file to track progress against")
|
|
return
|
|
}
|
|
if err != nil {
|
|
h.fail(w, "save progress", err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(w, http.StatusOK, map[string]any{"progress": saved})
|
|
}
|
|
|
|
func (h *Handlers) handleRecent(w http.ResponseWriter, r *http.Request, u auth.User) {
|
|
items, err := progress.RecentlyUpdated(r.Context(), h.db, u.ID, 20)
|
|
if err != nil {
|
|
h.fail(w, "recent progress", err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(w, http.StatusOK, map[string]any{"progress": items})
|
|
}
|
|
|
|
// ── helpers ────────────────────────────────────────────────────────────
|
|
|
|
// authorized loads an item and confirms the caller may read its library.
|
|
// On failure it has already written the response.
|
|
func (h *Handlers) authorized(w http.ResponseWriter, r *http.Request, u auth.User) (Item, bool) {
|
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
|
if err != nil || id <= 0 {
|
|
httpx.WriteError(w, http.StatusBadRequest, "invalid item id")
|
|
return Item{}, false
|
|
}
|
|
|
|
it, err := LoadItem(r.Context(), h.db, id)
|
|
if errors.Is(err, ErrItemNotFound) {
|
|
httpx.WriteError(w, http.StatusNotFound, "item not found")
|
|
return Item{}, false
|
|
}
|
|
if err != nil {
|
|
h.fail(w, "load item", err)
|
|
return Item{}, false
|
|
}
|
|
|
|
allowed, err := auth.CanAccessLibrary(r.Context(), h.db, u, it.LibraryID)
|
|
if err != nil {
|
|
h.fail(w, "check access", err)
|
|
return Item{}, false
|
|
}
|
|
if !allowed {
|
|
httpx.WriteError(w, http.StatusNotFound, "item not found")
|
|
return Item{}, false
|
|
}
|
|
return it, true
|
|
}
|
|
|
|
// fileDTO is one physical file in an item's detail response. It carries the
|
|
// ffprobe codec facts alongside the container, because direct play is a
|
|
// decision each client makes for itself: the server reports what is in the file
|
|
// and never transcodes. The wire shape is frozen by MediaFile in
|
|
// web/src/lib/api.ts.
|
|
type fileDTO struct {
|
|
ID int64 `json:"id"`
|
|
Path string `json:"path"`
|
|
Size int64 `json:"size"`
|
|
Container string `json:"container"`
|
|
Missing bool `json:"missing"`
|
|
DurationMS int64 `json:"duration_ms"`
|
|
VCodec string `json:"vcodec"`
|
|
ACodec string `json:"acodec"`
|
|
Width int `json:"width"`
|
|
Height int `json:"height"`
|
|
}
|
|
|
|
func (h *Handlers) files(r *http.Request, itemID int64) ([]fileDTO, error) {
|
|
rows, err := h.db.QueryContext(r.Context(), `
|
|
SELECT id, path, size, container, missing,
|
|
duration_ms, vcodec, acodec, width, height
|
|
FROM media_files WHERE item_id = ? ORDER BY id`, itemID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
out := []fileDTO{}
|
|
for rows.Next() {
|
|
var f fileDTO
|
|
var missing int
|
|
if err := rows.Scan(&f.ID, &f.Path, &f.Size, &f.Container, &missing,
|
|
&f.DurationMS, &f.VCodec, &f.ACodec, &f.Width, &f.Height); err != nil {
|
|
return nil, err
|
|
}
|
|
f.Missing = missing == 1
|
|
out = append(out, f)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
func (h *Handlers) hasCover(r *http.Request, it Item) bool {
|
|
var n int
|
|
_ = h.db.QueryRowContext(r.Context(),
|
|
`SELECT count(*) FROM assets WHERE item_id = ? AND role = 'cover'`, it.ID).Scan(&n)
|
|
return n > 0
|
|
}
|
|
|
|
func (h *Handlers) fail(w http.ResponseWriter, what string, err error) {
|
|
h.log.Error(what, "err", err)
|
|
httpx.WriteError(w, http.StatusInternalServerError, "internal server error")
|
|
}
|
|
|
|
// sanitizeFilename strips characters that break a Content-Disposition header.
|
|
func sanitizeFilename(name string) string {
|
|
out := make([]rune, 0, len(name))
|
|
for _, r := range name {
|
|
switch {
|
|
case r == '"' || r == '\\' || r == '\n' || r == '\r':
|
|
out = append(out, '_')
|
|
default:
|
|
out = append(out, r)
|
|
}
|
|
}
|
|
return string(out)
|
|
}
|