diff options
| author | Paul Buetow <paul@buetow.org> | 2026-05-19 14:18:43 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-05-19 14:18:43 +0300 |
| commit | 53a599e763eb8f105dd12c8e0a2a50e2a850e4d6 (patch) | |
| tree | 02bb08debc8844b77b65a32c8448044bc21a419b /player-server/internal/api/handlers.go | |
| parent | 212e849475701d91d5f173c3638af540fab796dd (diff) | |
Fix four defects flagged by S19/S20/S24; tighten scenarios
1. tagService.AssignTag and RemoveTag now use verifyModifyAccess
(owner role required) instead of verifyAccess. Tags are global
state visible to every user with access to a media item, so a
viewer must not be able to add or remove them. Favorites and
notes stay on verifyAccess because they're per-user data
(favorites.user_id, media_notes.user_id) and don't affect anyone
else. Verified via curl: viewer POST /media/{id}/tags now 403,
admin still 200.
2. serveFileResult now emits a strong ETag header
("<size>-<mtime-nanos>") before calling http.ServeContent. Go's
ServeContent honours If-None-Match when ETag is set, so iOS
audio clients and podcast apps can revalidate cached downloads
with conditional GETs. Verified via curl: ETag present on
/stream; If-None-Match matching the ETag returns 304.
3. MediaFilter gains IncludeDeleted flag; ListMedia skips the
implicit `deleted_at IS NULL` predicate when it is set.
FSScanner.loadExistingMedia now passes IncludeDeleted=true so
the dedup map includes soft-deleted rows. Previously a re-scan
of a soft-deleted file tried to CreateMedia and hit the
UNIQUE(set_id, rel_path) constraint, failing the whole scan and
setting progress.last_error. Now the rescan skips the row
cleanly; soft-delete sticks.
4. FSScanner.reconcileOrphans soft-deletes media rows whose
underlying file disappeared between scans. The scanner used to
only walk files that exist and never compare against the DB,
leaving phantom rows in GET /api/v1/media that 404'd on stream.
Verified via curl: rm /testdata/.../orphan.mp3, rescan, row
now has deleted_at != NULL.
Scenarios updated to lock in the fixed behaviour:
S19 step 15 — viewer tag-add now asserts 403, not 200.
S20 step 14 — asserts ETag is present and If-None-Match → 304.
S24 step 13 — asserts clean rescan (no last_error from UNIQUE).
S24 step 20 — asserts orphan rows are soft-deleted by rescan.
Verified: full Go unit suite passes; 25/25 LLM e2e scenarios;
22/22 Playwright e2e-web.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Diffstat (limited to 'player-server/internal/api/handlers.go')
| -rw-r--r-- | player-server/internal/api/handlers.go | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/player-server/internal/api/handlers.go b/player-server/internal/api/handlers.go index 1423a84..a9c832f 100644 --- a/player-server/internal/api/handlers.go +++ b/player-server/internal/api/handlers.go @@ -8,11 +8,20 @@ import ( "log/slog" "net/http" "strconv" + "time" "codeberg.org/snonux/player/internal/model" "codeberg.org/snonux/player/internal/service" ) +// fileETag returns a strong ETag value (without surrounding quotes) for a +// file of the given size and modification time. Combining size with mtime +// nanoseconds is enough to detect any in-place rewrite or replacement — +// callers wrap the result in quotes when emitting the header. +func fileETag(size int64, modTime time.Time) string { + return fmt.Sprintf("%d-%d", size, modTime.UnixNano()) +} + // ------------------------------------------------------------------ // Helpers // ------------------------------------------------------------------ @@ -177,6 +186,11 @@ func (s *Server) serveFileResult(w http.ResponseWriter, r *http.Request, res *se } w.Header().Set("Content-Type", stream.ContentType) w.Header().Set("Accept-Ranges", "bytes") + // Strong ETag derived from size and mtime nanoseconds. http.ServeContent + // reads If-None-Match / If-Match from the request once ETag is set, so + // clients (iOS audio player, podcast clients) can revalidate cached + // downloads without re-fetching the full body. + w.Header().Set("ETag", fmt.Sprintf("%q", fileETag(stream.Size, stream.ModTime))) s.logger.Info("api stream file", "file", stream.FileName, "size", stream.Size, "range", r.Header.Get("Range")) http.ServeContent(w, r, stream.FileName, stream.ModTime, stream.File) } |
