kuvia2/image.go

68 lines
1.4 KiB
Go

package main
import (
"bytes"
"fmt"
"github.com/flamego/flamego"
"github.com/minio/minio-go/v7"
"github.com/nfnt/resize"
"image"
"image/png"
"os"
"strconv"
)
func getImage(c flamego.Context) {
path := c.Request().FormValue("path")
img, err := s3.GetObject(c.Request().Context(), os.Getenv("S3_BUCKET"), path, minio.GetObjectOptions{})
if err != nil {
panic(err)
}
defer img.Close()
imgO, _ , err := image.Decode(img)
if err != nil {
panic(err)
}
widthString := c.Request().FormValue("width")
heightString := c.Request().FormValue("height")
var width uint
var height uint
height = 0
width = 500
if widthString != "" {
w, _ := strconv.ParseUint(widthString, 10, 32)
width = uint(w)
}
if heightString != "" {
h, _ := strconv.ParseUint(heightString, 10, 32)
height = uint(h)
width = 0
}
if height == 250 || width == 500 {
//cache
}
m := resize.Resize(width, height, imgO, resize.Lanczos3)
buf := new(bytes.Buffer)
err = png.Encode(buf, m)
if err != nil {
panic(err)
}
send_s3 := buf.Bytes()
//c.ResponseWriter().WriteHeader(http.StatusOK)
//c.ResponseWriter().Header().Set("Content-Type", "application/octet-stream")
c.ResponseWriter().Header().Set("Content-Length", fmt.Sprintf("%v", len(send_s3)))
c.ResponseWriter().Header().Set("Cache-Control", "max-age=31536000")
c.ResponseWriter().Header().Set("X-Foo", "BAR")
c.ResponseWriter().WriteHeader(200)
c.ResponseWriter().Write(send_s3)
return
}