inou/api/api_image.go

48 lines
966 B
Go

package main
import (
"net/http"
"strconv"
"strings"
"inou/lib"
)
func handleImage(w http.ResponseWriter, r *http.Request) {
ctx := getAccessContextOrFail(w, r)
if ctx == nil {
return
}
hexID := strings.TrimPrefix(r.URL.Path, "/image/")
if len(hexID) != 16 {
http.Error(w, "Invalid ID", http.StatusBadRequest)
return
}
opts := &lib.ImageOpts{}
if wc := r.URL.Query().Get("wc"); wc != "" {
opts.WC, _ = strconv.ParseFloat(wc, 64)
}
if ww := r.URL.Query().Get("ww"); ww != "" {
opts.WW, _ = strconv.ParseFloat(ww, 64)
}
maxDim := 2000
if md := r.URL.Query().Get("maxdim"); md != "" {
if v, err := strconv.Atoi(md); err == nil && v > 0 {
maxDim = v
}
}
body, err := lib.RenderImage(ctx.AccessorID, hexID, opts, maxDim)
if err != nil {
http.Error(w, "Image not found", http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "image/webp")
w.Header().Set("Cache-Control", "public, max-age=86400")
w.Write(body)
}