package main import ( "bytes" "image/jpeg" "image/png" "github.com/labstack/echo/v4" ) func CreateServer(db *Db) *echo.Echo { e := echo.New() e.GET("/:key", ImageHandler(db)) return e } func ImageHandler(db *Db) echo.HandlerFunc { store := db.Prefix(ImageItem) return func(c echo.Context) error { key := c.Param("key") isJpeg := c.QueryParam("jpeg") img, err := store.GetItem(key) if err != nil { return c.String(404, err.Error()) } if isJpeg != "1" { return c.Blob(200, "image/png", img) } png, _ := png.Decode(bytes.NewReader(img)) jpg := new(bytes.Buffer) err = jpeg.Encode(jpg, png, &jpeg.Options{Quality: 95}) if err != nil { return c.String(500, err.Error()) } return c.Blob(200, "image/jpeg", jpg.Bytes()) } }