Trim comments to the non-obvious why
Test and deploy / test (push) Successful in 28s
Test and deploy / deploy (push) Successful in 31s

Drop narration, history and restated-code comments across the repo,
keep short ones for security boundaries, upstream quirks and ordering
constraints, and record the convention in CLAUDE.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-24 20:49:03 +00:00
co-authored by Claude Opus 5.5
parent effddd2794
commit 4530cb8dc4
222 changed files with 1173 additions and 5423 deletions
+13 -41
View File
@@ -9,27 +9,18 @@ import (
"github.com/gin-gonic/gin"
)
// Admin management of the radio's fallback music. These handlers only move
// files around on disk; the Python service is what actually streams them, and
// it picks up the directory contents on its own.
//
// Disabling a song moves it into a "disabled" subdirectory rather than
// deleting it — so presence in that directory IS the disabled flag, and there
// is no database state for the radio at all.
// Radio fallback music management. Disabled songs live in a "disabled"
// subdirectory: that is the whole of the radio's state.
// fallbackMusicDir is a shared volume, also mounted by the radio service.
const fallbackMusicDir = "/backend/fallback_music"
// allowedAudioExtensions is the upload allow-list, keyed by lower-cased
// extension. Unlike chat uploads, the content is not sniffed: the route is
// admin-only and audio formats sniff unreliably.
// Not sniffed: admin-only, and audio sniffs unreliably.
var allowedAudioExtensions = map[string]bool{
".mp3": true, ".ogg": true, ".flac": true, ".wav": true, ".m4a": true, ".opus": true,
}
// UploadRadioSong backs the admin-only POST /radio/upload. Unlike chat
// uploads the original filename is kept, because it is what the admin sees in
// the song list — hence the sanitisation below.
// UploadRadioSong keeps the original filename, which the admin sees, so it is
// sanitised.
func (store *Store) UploadRadioSong(ctx *gin.Context) {
file, err := ctx.FormFile("file")
if err != nil {
@@ -49,22 +40,18 @@ func (store *Store) UploadRadioSong(ctx *gin.Context) {
return
}
// filepath.Base strips any directory component, so "../../etc/passwd"
// becomes "passwd". The absolute-path check below is a second, redundant
// guard in case Base ever fails to neutralise some input.
// Base strips traversal; the absolute-path check below is a second guard.
filename := filepath.Base(file.Filename)
dest := filepath.Join(fallbackMusicDir, filename)
// The separator is appended before comparing so a sibling directory whose
// name merely starts with the same characters cannot pass.
// The trailing separator stops a same-prefix sibling directory passing.
absDest, err := filepath.Abs(dest)
if err != nil || !strings.HasPrefix(absDest, fallbackMusicDir+string(os.PathSeparator)) {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid filename"})
return
}
// Refuse to overwrite. There is a check-then-write race here, but the
// route is admin-only and single-user in practice.
// Check-then-write race accepted: admin-only.
if _, err := os.Stat(dest); err == nil {
ctx.JSON(http.StatusConflict, gin.H{"error": "file already exists"})
return
@@ -78,11 +65,7 @@ func (store *Store) UploadRadioSong(ctx *gin.Context) {
ctx.JSON(http.StatusOK, gin.H{"name": filename})
}
// ListRadioSongs backs the admin-only GET /radio/songs, returning both the
// enabled songs and those parked in the disabled subdirectory, flagged.
func (store *Store) ListRadioSongs(ctx *gin.Context) {
// Declared inside the function because it is purely the JSON shape of
// this one response.
type songInfo struct {
Name string `json:"name"`
Size int64 `json:"size"`
@@ -90,17 +73,15 @@ func (store *Store) ListRadioSongs(ctx *gin.Context) {
Disabled bool `json:"disabled"`
}
// Initialised empty rather than nil so the response is [] and not null.
// [] not null in the JSON.
songs := []songInfo{}
// Read enabled songs
entries, err := os.ReadDir(fallbackMusicDir)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "failed to read music directory"})
return
}
// Skipping directories is also what keeps the "disabled" subdirectory out
// of the enabled list; dotfiles skip editor and OS cruft.
// Skipping directories also skips "disabled".
for _, entry := range entries {
if entry.IsDir() || strings.HasPrefix(entry.Name(), ".") {
continue
@@ -117,9 +98,7 @@ func (store *Store) ListRadioSongs(ctx *gin.Context) {
})
}
// The disabled directory is created lazily by DisableRadioSong, so a read
// failure here is normally just "nothing has ever been disabled" and is
// ignored rather than reported.
// Created lazily, so a missing directory just means none disabled.
disabledDir := filepath.Join(fallbackMusicDir, "disabled")
disabledEntries, err := os.ReadDir(disabledDir)
if err == nil {
@@ -143,12 +122,8 @@ func (store *Store) ListRadioSongs(ctx *gin.Context) {
ctx.JSON(http.StatusOK, gin.H{"songs": songs})
}
// DisableRadioSong moves a song into the disabled subdirectory so the radio
// stops playing it, keeping the file.
func (store *Store) DisableRadioSong(ctx *gin.Context) {
// filepath.Base neutralises traversal in the :filename route parameter.
// It returns "." for an empty input and "/" for "/", neither of which is
// a real file, so both are rejected explicitly.
// Base returns "." or "/" for degenerate input; both are rejected.
filename := filepath.Base(ctx.Param("filename"))
if filename == "." || filename == "/" {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid filename"})
@@ -176,7 +151,6 @@ func (store *Store) DisableRadioSong(ctx *gin.Context) {
ctx.JSON(http.StatusOK, gin.H{"disabled": filename})
}
// EnableRadioSong moves a song back out of the disabled subdirectory.
func (store *Store) EnableRadioSong(ctx *gin.Context) {
filename := filepath.Base(ctx.Param("filename"))
if filename == "." || filename == "/" {
@@ -199,9 +173,7 @@ func (store *Store) EnableRadioSong(ctx *gin.Context) {
ctx.JSON(http.StatusOK, gin.H{"enabled": filename})
}
// DeleteRadioSong permanently removes an enabled song. It cannot reach the
// disabled subdirectory, so a disabled song must be enabled before it can be
// deleted.
// DeleteRadioSong can't reach disabled songs; enable one first.
func (store *Store) DeleteRadioSong(ctx *gin.Context) {
filename := filepath.Base(ctx.Param("filename"))
if filename == "." || filename == "/" {