Files
stpweb/backend/handlers/handle_radio.go
T
adamfandClaude Opus 5.5 4530cb8dc4
Test and deploy / test (push) Successful in 28s
Test and deploy / deploy (push) Successful in 31s
Trim comments to the non-obvious why
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]>
2026-11-24 20:49:03 +00:00

197 lines
5.6 KiB
Go

package handlers
import (
"net/http"
"os"
"path/filepath"
"strings"
"github.com/gin-gonic/gin"
)
// Radio fallback music management. Disabled songs live in a "disabled"
// subdirectory: that is the whole of the radio's state.
const fallbackMusicDir = "/backend/fallback_music"
// 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 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 {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "file is required"})
return
}
const maxSize = 50 << 20 // 50MB
if file.Size > maxSize {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "file too large (max 50MB)"})
return
}
ext := strings.ToLower(filepath.Ext(file.Filename))
if !allowedAudioExtensions[ext] {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "file type not allowed (accepted: .mp3, .ogg, .flac, .wav, .m4a, .opus)"})
return
}
// Base strips traversal; the absolute-path check below is a second guard.
filename := filepath.Base(file.Filename)
dest := filepath.Join(fallbackMusicDir, filename)
// 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
}
// 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
}
if err := ctx.SaveUploadedFile(file, dest); err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "failed to save file"})
return
}
ctx.JSON(http.StatusOK, gin.H{"name": filename})
}
func (store *Store) ListRadioSongs(ctx *gin.Context) {
type songInfo struct {
Name string `json:"name"`
Size int64 `json:"size"`
Modified int64 `json:"modified"`
Disabled bool `json:"disabled"`
}
// [] not null in the JSON.
songs := []songInfo{}
entries, err := os.ReadDir(fallbackMusicDir)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "failed to read music directory"})
return
}
// Skipping directories also skips "disabled".
for _, entry := range entries {
if entry.IsDir() || strings.HasPrefix(entry.Name(), ".") {
continue
}
info, err := entry.Info()
if err != nil {
continue
}
songs = append(songs, songInfo{
Name: entry.Name(),
Size: info.Size(),
Modified: info.ModTime().Unix(),
Disabled: false,
})
}
// Created lazily, so a missing directory just means none disabled.
disabledDir := filepath.Join(fallbackMusicDir, "disabled")
disabledEntries, err := os.ReadDir(disabledDir)
if err == nil {
for _, entry := range disabledEntries {
if entry.IsDir() || strings.HasPrefix(entry.Name(), ".") {
continue
}
info, err := entry.Info()
if err != nil {
continue
}
songs = append(songs, songInfo{
Name: entry.Name(),
Size: info.Size(),
Modified: info.ModTime().Unix(),
Disabled: true,
})
}
}
ctx.JSON(http.StatusOK, gin.H{"songs": songs})
}
func (store *Store) DisableRadioSong(ctx *gin.Context) {
// 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"})
return
}
src := filepath.Join(fallbackMusicDir, filename)
if _, err := os.Stat(src); os.IsNotExist(err) {
ctx.JSON(http.StatusNotFound, gin.H{"error": "file not found"})
return
}
disabledDir := filepath.Join(fallbackMusicDir, "disabled")
if err := os.MkdirAll(disabledDir, 0o755); err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "failed to create disabled directory"})
return
}
dst := filepath.Join(disabledDir, filename)
if err := os.Rename(src, dst); err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "failed to disable song"})
return
}
ctx.JSON(http.StatusOK, gin.H{"disabled": filename})
}
func (store *Store) EnableRadioSong(ctx *gin.Context) {
filename := filepath.Base(ctx.Param("filename"))
if filename == "." || filename == "/" {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid filename"})
return
}
src := filepath.Join(fallbackMusicDir, "disabled", filename)
if _, err := os.Stat(src); os.IsNotExist(err) {
ctx.JSON(http.StatusNotFound, gin.H{"error": "file not found in disabled directory"})
return
}
dst := filepath.Join(fallbackMusicDir, filename)
if err := os.Rename(src, dst); err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "failed to enable song"})
return
}
ctx.JSON(http.StatusOK, gin.H{"enabled": filename})
}
// 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 == "/" {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid filename"})
return
}
path := filepath.Join(fallbackMusicDir, filename)
if _, err := os.Stat(path); os.IsNotExist(err) {
ctx.JSON(http.StatusNotFound, gin.H{"error": "file not found"})
return
}
if err := os.Remove(path); err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "failed to delete file"})
return
}
ctx.JSON(http.StatusOK, gin.H{"deleted": filename})
}