Decode AVIF via avifdec when Go's image.Decode can't, and pass-through small AVIFs (<=800px wide) by caching the original bytes instead of re-encoding.
303 lines
7.3 KiB
Go
303 lines
7.3 KiB
Go
package web
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"image"
|
|
_ "image/gif"
|
|
_ "image/jpeg"
|
|
"image/png"
|
|
"io"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"golang.org/x/image/draw"
|
|
_ "golang.org/x/image/webp"
|
|
|
|
"pete/internal/storage"
|
|
)
|
|
|
|
const (
|
|
thumbWidth = 800
|
|
thumbMaxBytes = 12 << 20 // 12 MiB cap on source download
|
|
thumbFetchTimeout = 12 * time.Second
|
|
thumbCacheDir = "./data/img-cache"
|
|
thumbQuality = "45"
|
|
thumbSpeed = "8"
|
|
)
|
|
|
|
var (
|
|
thumbClient = &http.Client{Timeout: thumbFetchTimeout}
|
|
|
|
thumbMu sync.Mutex
|
|
thumbInFlight = map[string]*sync.WaitGroup{}
|
|
)
|
|
|
|
func thumbKey(u string) string {
|
|
sum := sha256.Sum256([]byte(u))
|
|
return hex.EncodeToString(sum[:16])
|
|
}
|
|
|
|
// thumbURL maps a source image URL to the /img proxy path. Empty input → "".
|
|
func thumbURL(src string) string {
|
|
if src == "" {
|
|
return ""
|
|
}
|
|
return "/img/" + thumbKey(src) + ".avif?u=" + url.QueryEscape(src)
|
|
}
|
|
|
|
func (s *Server) handleImg(w http.ResponseWriter, r *http.Request) {
|
|
name := r.PathValue("name")
|
|
hash, ok := strings.CutSuffix(name, ".avif")
|
|
if !ok {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
src := r.URL.Query().Get("u")
|
|
if src == "" || thumbKey(src) != hash {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
if !(strings.HasPrefix(src, "http://") || strings.HasPrefix(src, "https://")) {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
if !storage.IsKnownImageURL(src) {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
|
|
if err := os.MkdirAll(thumbCacheDir, 0o755); err != nil {
|
|
http.Error(w, "cache dir", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
cachePath := filepath.Join(thumbCacheDir, hash+".avif")
|
|
if fi, err := os.Stat(cachePath); err == nil && fi.Size() > 0 {
|
|
serveAvif(w, r, cachePath)
|
|
return
|
|
}
|
|
|
|
// Single-flight: collapse concurrent requests for the same image.
|
|
thumbMu.Lock()
|
|
wg, busy := thumbInFlight[hash]
|
|
if !busy {
|
|
wg = &sync.WaitGroup{}
|
|
wg.Add(1)
|
|
thumbInFlight[hash] = wg
|
|
}
|
|
thumbMu.Unlock()
|
|
|
|
if busy {
|
|
wg.Wait()
|
|
if fi, err := os.Stat(cachePath); err == nil && fi.Size() > 0 {
|
|
serveAvif(w, r, cachePath)
|
|
return
|
|
}
|
|
http.Redirect(w, r, src, http.StatusFound)
|
|
return
|
|
}
|
|
defer func() {
|
|
thumbMu.Lock()
|
|
delete(thumbInFlight, hash)
|
|
thumbMu.Unlock()
|
|
wg.Done()
|
|
}()
|
|
|
|
if err := buildAvifThumb(src, cachePath); err != nil {
|
|
slog.Warn("thumb build failed", "url", src, "err", err)
|
|
http.Redirect(w, r, src, http.StatusFound)
|
|
return
|
|
}
|
|
serveAvif(w, r, cachePath)
|
|
}
|
|
|
|
func serveAvif(w http.ResponseWriter, r *http.Request, path string) {
|
|
w.Header().Set("Content-Type", "image/avif")
|
|
w.Header().Set("Cache-Control", "public, max-age=2592000, immutable")
|
|
http.ServeFile(w, r, path)
|
|
}
|
|
|
|
// buildAvifThumb downloads src, decodes + resizes in Go, then shells out to
|
|
// avifenc to produce a compact AVIF at outPath.
|
|
func buildAvifThumb(src, outPath string) error {
|
|
req, err := http.NewRequest(http.MethodGet, src, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.Header.Set("User-Agent", "Pete-Thumb/1.0 (+https://pete.example)")
|
|
resp, err := thumbClient.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != http.StatusOK {
|
|
return fmt.Errorf("upstream %d", resp.StatusCode)
|
|
}
|
|
body, err := io.ReadAll(io.LimitReader(resp.Body, thumbMaxBytes+1))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(body) > thumbMaxBytes {
|
|
return fmt.Errorf("image too big (>%d bytes)", thumbMaxBytes)
|
|
}
|
|
|
|
if isAvif(body) {
|
|
// If the source AVIF is already within our width budget, skip the
|
|
// decode/resize/re-encode roundtrip and cache the original bytes.
|
|
w, _, infoErr := avifInfo(body)
|
|
if infoErr == nil && w > 0 && w <= thumbWidth {
|
|
return writeFile(outPath, body)
|
|
}
|
|
body, err = avifToPNG(body)
|
|
if err != nil {
|
|
return fmt.Errorf("avifdec: %w", err)
|
|
}
|
|
}
|
|
src2, _, err := image.Decode(bytes.NewReader(body))
|
|
if err != nil {
|
|
return fmt.Errorf("decode: %w", err)
|
|
}
|
|
b := src2.Bounds()
|
|
srcW, srcH := b.Dx(), b.Dy()
|
|
if srcW <= 0 || srcH <= 0 {
|
|
return fmt.Errorf("empty image")
|
|
}
|
|
dstW, dstH := srcW, srcH
|
|
if srcW > thumbWidth {
|
|
dstW = thumbWidth
|
|
dstH = int(float64(srcH) * float64(thumbWidth) / float64(srcW))
|
|
if dstH < 1 {
|
|
dstH = 1
|
|
}
|
|
}
|
|
var resized image.Image = src2
|
|
if dstW != srcW || dstH != srcH {
|
|
dst := image.NewRGBA(image.Rect(0, 0, dstW, dstH))
|
|
draw.CatmullRom.Scale(dst, dst.Bounds(), src2, b, draw.Over, nil)
|
|
resized = dst
|
|
}
|
|
|
|
tmp, err := os.CreateTemp("", "pete-thumb-*.png")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
tmpPath := tmp.Name()
|
|
defer os.Remove(tmpPath)
|
|
if err := png.Encode(tmp, resized); err != nil {
|
|
tmp.Close()
|
|
return err
|
|
}
|
|
if err := tmp.Close(); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Encode to a sibling temp file then atomically rename so partial outputs
|
|
// never appear in the cache.
|
|
stagedOut := outPath + ".tmp"
|
|
cmd := exec.Command("avifenc",
|
|
"-q", thumbQuality,
|
|
"-s", thumbSpeed,
|
|
tmpPath, stagedOut,
|
|
)
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
os.Remove(stagedOut)
|
|
return fmt.Errorf("avifenc: %w: %s", err, strings.TrimSpace(string(out)))
|
|
}
|
|
return os.Rename(stagedOut, outPath)
|
|
}
|
|
|
|
// isAvif reports whether body looks like an AVIF file via the ISO-BMFF "ftyp"
|
|
// brand at bytes 4..12 ("ftypavif"/"ftypavis").
|
|
func isAvif(body []byte) bool {
|
|
if len(body) < 12 {
|
|
return false
|
|
}
|
|
if string(body[4:8]) != "ftyp" {
|
|
return false
|
|
}
|
|
brand := string(body[8:12])
|
|
return brand == "avif" || brand == "avis"
|
|
}
|
|
|
|
// avifInfo returns the width and height of an AVIF by shelling out to
|
|
// `avifdec --info`. It still decodes the image internally but doesn't write
|
|
// pixels to disk, so it's cheaper than full avifToPNG.
|
|
func avifInfo(body []byte) (int, int, error) {
|
|
in, err := os.CreateTemp("", "pete-avif-info-*.avif")
|
|
if err != nil {
|
|
return 0, 0, err
|
|
}
|
|
inPath := in.Name()
|
|
defer os.Remove(inPath)
|
|
if _, err := in.Write(body); err != nil {
|
|
in.Close()
|
|
return 0, 0, err
|
|
}
|
|
if err := in.Close(); err != nil {
|
|
return 0, 0, err
|
|
}
|
|
out, err := exec.Command("avifdec", "--info", inPath).CombinedOutput()
|
|
if err != nil {
|
|
return 0, 0, fmt.Errorf("%w: %s", err, strings.TrimSpace(string(out)))
|
|
}
|
|
for _, line := range strings.Split(string(out), "\n") {
|
|
line = strings.TrimSpace(line)
|
|
rest, ok := strings.CutPrefix(line, "* Resolution")
|
|
if !ok {
|
|
continue
|
|
}
|
|
_, dims, ok := strings.Cut(rest, ":")
|
|
if !ok {
|
|
continue
|
|
}
|
|
var w, h int
|
|
if _, err := fmt.Sscanf(strings.TrimSpace(dims), "%dx%d", &w, &h); err == nil {
|
|
return w, h, nil
|
|
}
|
|
}
|
|
return 0, 0, fmt.Errorf("no resolution in avifdec output")
|
|
}
|
|
|
|
// writeFile atomically writes body to path via a sibling temp + rename.
|
|
func writeFile(path string, body []byte) error {
|
|
staged := path + ".tmp"
|
|
if err := os.WriteFile(staged, body, 0o644); err != nil {
|
|
os.Remove(staged)
|
|
return err
|
|
}
|
|
return os.Rename(staged, path)
|
|
}
|
|
|
|
// avifToPNG decodes AVIF bytes to PNG bytes by shelling out to avifdec.
|
|
func avifToPNG(body []byte) ([]byte, error) {
|
|
in, err := os.CreateTemp("", "pete-avif-in-*.avif")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
inPath := in.Name()
|
|
defer os.Remove(inPath)
|
|
if _, err := in.Write(body); err != nil {
|
|
in.Close()
|
|
return nil, err
|
|
}
|
|
if err := in.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
outPath := inPath + ".png"
|
|
defer os.Remove(outPath)
|
|
cmd := exec.Command("avifdec", inPath, outPath)
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
return nil, fmt.Errorf("%w: %s", err, strings.TrimSpace(string(out)))
|
|
}
|
|
return os.ReadFile(outPath)
|
|
}
|