Files
stpweb/backend/handlers/store.go
T
adamfandClaude Opus 5.5 65fddfcfa2
Test and deploy / deploy (push) Successful in 3m48s
Test and deploy / test (push) Successful in 37s
Give uploads one path end to end, and serve /uploads/ from Garage first
Frontend: src/uploads.js is the one way to store or show a file
(uploadFile, ACCEPT, kindOf, isUploadUrl, tagFor, mediaSize). Chat,
the box editor, canvas drops and the background editor all use it;
views/home3/upload.js and the chat store's own axios call are gone.

Backend: POST /api/uploads (was /messages/upload) is a thin wrapper over
services.Uploads.Save, which owns validation, naming and storage. The
three per-extension maps are now one UploadTypes table, and a refused
file is an *UploadError whose message is safe to show.

nginx now fetches /uploads/ from Garage first and falls back to the
volume on a 404 or any 5xx, so the bucket is the source of truth and
deleting a file in garage-webui unpublishes it, while a Garage outage
still only affects bucket-only files.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 21:04:37 +01:00

223 lines
7.5 KiB
Go

package handlers
import (
"crypto/rand"
"encoding/hex"
"log"
"sync"
"time"
"adam-french.co.uk/backend/services"
"github.com/zmb3/spotify/v2"
spotifyauth "github.com/zmb3/spotify/v2/auth"
"gorm.io/gorm"
)
// Package handlers holds the REST endpoints and the shared Store that every
// request handler — and, through graph.Resolver, every GraphQL resolver —
// depends on.
//
// Store is the backend's dependency container: it is built once in main and
// passed around by pointer, so the caches on it are shared process-wide.
//
// Concurrency note: the third-party caches below are read and written by
// concurrent requests, so every access goes through cacheMu and the accessor
// methods at the bottom of this file. Do not touch the cache fields directly
// — an unsynchronised read of one of them is a data race, not merely a stale
// value.
type Store struct {
DB *gorm.DB
SpotifyAuth *spotifyauth.Authenticator
SpotifyClient *spotify.Client
Auth *services.Auth
Notes *services.Notes
LoginLimiter *services.RateLimiter
// Base URL of the Python service on the Docker network, no trailing
// slash. Used for the calls that bypass nginx, all through
// services.CallPython: reading a rowing display photo (handle_rowing.go).
PythonURL string
// Stores uploaded files and says where they are served
// (services.Uploads.Save). Used by the Upload handler.
Uploads *services.Uploads
// Cached "recently played" tracks, refreshed at most once a minute by the
// GraphQL spotifyRecent resolver. A pointer to a slice so that nil can
// mean "never fetched" distinctly from "fetched, empty".
// cacheMu guards every cache field below (and only those). One mutex for
// all of them keeps the locking obvious; the critical sections are just
// field copies, never network calls, so there is no contention worth
// splitting it up for.
cacheMu sync.RWMutex
RecentSongs *[]spotify.RecentlyPlayedItem
RecentSongsFetchedAt time.Time
// Cached latest Gitea activity entry, 1 minute TTL.
GiteaHost string
GiteaPort string
GiteaFeed *services.GiteaFeedResponse
GiteaFeedFetchedAt time.Time
// Cached Steam status and recent games, 5 minute TTL.
SteamAPIKey string
SteamID string
SteamRecentGames []services.SteamRecentGame
SteamOnline bool
SteamFetchedAt time.Time
// spotifyStates holds the one-shot CSRF nonces handed out by
// GET /spotify/auth, mapped to their creation time. In-memory is enough:
// there is one backend instance, and losing them on restart only means
// restarting an OAuth flow that takes seconds.
spotifyStatesMu sync.Mutex
spotifyStates map[string]time.Time
// spotifyNilLoggedAt rate-limits the "Spotify not authenticated" log line:
// every page visit hits spotifyRecent, so an unauthenticated backend would
// otherwise write a flood of identical lines. nil means "recently logged".
spotifyNilLoggedMu sync.Mutex
spotifyNilLoggedAt time.Time
}
// spotifyStateTTL is how long an unused Spotify OAuth nonce stays valid. Long
// enough to log in at Spotify and approve the scopes, short enough that a
// leaked authorisation URL is not reusable later.
const spotifyStateTTL = 10 * time.Minute
// NewSpotifyState mints a one-shot CSRF nonce for the Spotify authorisation
// URL and remembers it. Expired entries are pruned on the way through, which
// is the only cleanup there is — the map is otherwise unbounded, and only an
// admin can add to it.
func (s *Store) NewSpotifyState() (string, error) {
b := make([]byte, 32)
if _, err := rand.Read(b); err != nil {
return "", err
}
state := hex.EncodeToString(b)
s.spotifyStatesMu.Lock()
defer s.spotifyStatesMu.Unlock()
if s.spotifyStates == nil {
s.spotifyStates = make(map[string]time.Time)
}
for k, created := range s.spotifyStates {
if time.Since(created) > spotifyStateTTL {
delete(s.spotifyStates, k)
}
}
s.spotifyStates[state] = time.Now()
return state, nil
}
// ConsumeSpotifyState checks a nonce returned by Spotify's redirect and
// removes it, so a replayed callback fails. An unknown or expired nonce
// returns false, which the callback must treat as a rejected request.
func (s *Store) ConsumeSpotifyState(state string) bool {
if state == "" {
return false
}
s.spotifyStatesMu.Lock()
defer s.spotifyStatesMu.Unlock()
created, ok := s.spotifyStates[state]
if !ok {
return false
}
delete(s.spotifyStates, state)
return time.Since(created) <= spotifyStateTTL
}
// The cache accessors below are the only supported way to touch the cache
// fields. Each "Cached..." method returns the value plus a bool that is true
// only when the entry is present and inside its TTL; each "Set..." method
// stores a value and stamps it.
// LogSpotifyUnauthenticated logs that the Spotify OAuth flow has never been
// completed (or a stored token stopped working), with a where hint. It is
// rate-limited to one line per hour — see spotifyNilLoggedAt — because
// spotifyRecent is hit by every home-page visit.
func (s *Store) LogSpotifyUnauthenticated(where string) {
s.spotifyNilLoggedMu.Lock()
defer s.spotifyNilLoggedMu.Unlock()
if time.Since(s.spotifyNilLoggedAt) < time.Hour {
return
}
s.spotifyNilLoggedAt = time.Now()
log.Printf("[Spotify] not authenticated (seen in %s) — an admin can reconnect via GET /api/spotify/auth", where)
}
// CachedRecentSongs returns the cached Spotify recently-played list if it is
// within its one-minute TTL. An empty list counts as stale, so an empty
// listening history is re-fetched every time (unchanged behaviour).
func (s *Store) CachedRecentSongs() ([]spotify.RecentlyPlayedItem, bool) {
s.cacheMu.RLock()
defer s.cacheMu.RUnlock()
if s.RecentSongs == nil || len(*s.RecentSongs) == 0 {
return nil, false
}
if time.Since(s.RecentSongsFetchedAt) >= time.Minute {
return nil, false
}
return *s.RecentSongs, true
}
// SetRecentSongs replaces the cached recently-played list.
func (s *Store) SetRecentSongs(songs []spotify.RecentlyPlayedItem) {
s.cacheMu.Lock()
defer s.cacheMu.Unlock()
s.RecentSongs = &songs
s.RecentSongsFetchedAt = time.Now()
}
// CachedGiteaFeed returns the cached Gitea activity entry if it is within its
// one-minute TTL. A nil cache is never fresh, so the first request always hits
// Gitea.
func (s *Store) CachedGiteaFeed() (*services.GiteaFeedResponse, bool) {
s.cacheMu.RLock()
defer s.cacheMu.RUnlock()
if s.GiteaFeed == nil || time.Since(s.GiteaFeedFetchedAt) >= time.Minute {
return nil, false
}
return s.GiteaFeed, true
}
// SetGiteaFeed replaces the cached Gitea feed entry.
func (s *Store) SetGiteaFeed(feed *services.GiteaFeedResponse) {
s.cacheMu.Lock()
defer s.cacheMu.Unlock()
s.GiteaFeed = feed
s.GiteaFeedFetchedAt = time.Now()
}
// CachedSteam returns the cached Steam recent games and online flag if they
// are within their five-minute TTL. As before, freshness is judged only on the
// games list: if Steam legitimately returns no recent games the cache never
// registers as fresh and every request re-queries the API.
func (s *Store) CachedSteam() (games []services.SteamRecentGame, online bool, ok bool) {
s.cacheMu.RLock()
defer s.cacheMu.RUnlock()
if s.SteamRecentGames == nil || time.Since(s.SteamFetchedAt) >= 5*time.Minute {
return nil, false, false
}
return s.SteamRecentGames, s.SteamOnline, true
}
// SetSteam replaces the cached Steam data.
func (s *Store) SetSteam(games []services.SteamRecentGame, online bool) {
s.cacheMu.Lock()
defer s.cacheMu.Unlock()
s.SteamRecentGames = games
s.SteamOnline = online
s.SteamFetchedAt = time.Now()
}