|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "regexp" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "github.com/bcc-code/mediabank-bridge/log" |
| 9 | + lru "github.com/hashicorp/golang-lru/v2" |
| 10 | +) |
| 11 | + |
| 12 | +// vaultWaveformHandler proxies Vidispine's pre-rendered waveform PNG endpoint |
| 13 | +// (/item/{vxid}/waveform/image), with an LRU cache. Item shape analysis must |
| 14 | +// have been performed upstream — items without analysis return upstream's |
| 15 | +// error (typically 404). |
| 16 | +// GET /vault/waveform?vxid=VX-123[&width=400&height=80&bgcolor=000000&fgcolor=ffffff]. |
| 17 | +type vaultWaveformHandler struct { |
| 18 | + vault *VaultAPI |
| 19 | + cache *lru.Cache[string, []byte] |
| 20 | +} |
| 21 | + |
| 22 | +func newVaultWaveformHandler(vault *VaultAPI) *vaultWaveformHandler { |
| 23 | + cache, _ := lru.New[string, []byte](2048) |
| 24 | + return &vaultWaveformHandler{vault: vault, cache: cache} |
| 25 | +} |
| 26 | + |
| 27 | +// hexColor is 6 lowercase hex chars; the # prefix is added before forwarding |
| 28 | +// to Vidispine. Anything else is dropped so we don't pass garbage upstream. |
| 29 | +var hexColor = regexp.MustCompile(`^[0-9a-fA-F]{6}$`) |
| 30 | + |
| 31 | +func (h *vaultWaveformHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 32 | + if !PermissionsForEmail(getEmailFromHttp(r)).CanVault() { |
| 33 | + http.Error(w, "forbidden", http.StatusForbidden) |
| 34 | + return |
| 35 | + } |
| 36 | + vxID := r.URL.Query().Get("vxid") |
| 37 | + if vxID == "" { |
| 38 | + http.Error(w, "missing vxid", http.StatusBadRequest) |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + width := clampInt(r.URL.Query().Get("width"), 400, 1, 2000) |
| 43 | + height := clampInt(r.URL.Query().Get("height"), 80, 8, 400) |
| 44 | + bgColor := sanitizeColor(r.URL.Query().Get("bgcolor")) |
| 45 | + fgColor := sanitizeColor(r.URL.Query().Get("fgcolor")) |
| 46 | + |
| 47 | + cacheKey := vxID + "|" + strconv.Itoa(width) + "x" + strconv.Itoa(height) + "|" + bgColor + "|" + fgColor |
| 48 | + if data, ok := h.cache.Get(cacheKey); ok { |
| 49 | + writeWaveform(w, data, "HIT") |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + data, err := h.vault.fetchWaveform(vxID, width, height, bgColor, fgColor) |
| 54 | + if err != nil { |
| 55 | + log.L.Debug().Err(err).Str("vxid", vxID).Msg("vault waveform: fetch failed") |
| 56 | + http.Error(w, "no waveform", http.StatusNotFound) |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + h.cache.Add(cacheKey, data) |
| 61 | + writeWaveform(w, data, "MISS") |
| 62 | +} |
| 63 | + |
| 64 | +func sanitizeColor(s string) string { |
| 65 | + if !hexColor.MatchString(s) { |
| 66 | + return "" |
| 67 | + } |
| 68 | + return "#" + s |
| 69 | +} |
| 70 | + |
| 71 | +func writeWaveform(w http.ResponseWriter, data []byte, cacheStatus string) { |
| 72 | + w.Header().Set("Content-Type", "image/png") |
| 73 | + w.Header().Set("Cache-Control", "private, max-age=86400") |
| 74 | + w.Header().Set("X-Cache", cacheStatus) |
| 75 | + _, _ = w.Write(data) |
| 76 | +} |
| 77 | + |
| 78 | +func clampInt(s string, def, min, max int) int { |
| 79 | + if s == "" { |
| 80 | + return def |
| 81 | + } |
| 82 | + n, err := strconv.Atoi(s) |
| 83 | + if err != nil { |
| 84 | + return def |
| 85 | + } |
| 86 | + if n < min { |
| 87 | + return min |
| 88 | + } |
| 89 | + if n > max { |
| 90 | + return max |
| 91 | + } |
| 92 | + return n |
| 93 | +} |
0 commit comments