68 lines
1.5 KiB
Go
68 lines
1.5 KiB
Go
// backend\record_preview_sprite.go
|
|
|
|
package main
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
func recordPreviewSprite(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet && r.Method != http.MethodHead {
|
|
http.Error(w, "Nur GET/HEAD", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
// Unterstützt beide Prefixe (falls du mal testweise /api/preview-sprite/ nutzt)
|
|
id := strings.TrimPrefix(r.URL.Path, "/api/record/preview-sprite/")
|
|
if id == r.URL.Path {
|
|
id = strings.TrimPrefix(r.URL.Path, "/api/preview-sprite/")
|
|
}
|
|
id = strings.TrimSpace(id)
|
|
|
|
// Falls jemand versehentlich einen Slash am Ende schickt
|
|
id = strings.Trim(id, "/")
|
|
|
|
if id == "" {
|
|
http.Error(w, "id fehlt", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
var err error
|
|
id, err = sanitizeID(id)
|
|
if err != nil {
|
|
http.Error(w, "ungültige id", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
dir, err := generatedDirForID(id)
|
|
if err != nil {
|
|
http.Error(w, "ungültige id", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
spritePath := filepath.Join(dir, "preview-sprite.webp")
|
|
|
|
fi, err := os.Stat(spritePath)
|
|
if err != nil || fi.IsDir() || fi.Size() <= 0 {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
|
|
f, err := os.Open(spritePath)
|
|
if err != nil {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
defer f.Close()
|
|
|
|
// Cachebar (du hängst im Frontend ?v=updatedAtUnix dran)
|
|
w.Header().Set("Content-Type", "image/webp")
|
|
w.Header().Set("Cache-Control", "private, max-age=31536000, immutable")
|
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
|
|
|
http.ServeContent(w, r, "preview-sprite.webp", fi.ModTime(), f)
|
|
}
|