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) { hash := r.PathValue("hash") 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) } 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) }