From acd9ac6cdba8d51bf47f66021afea915e7f70cd4 Mon Sep 17 00:00:00 2001 From: Atridad Lahiji Date: Thu, 22 Feb 2024 14:39:14 -0700 Subject: [PATCH] re-worked the endpoints and stylegen logic --- api/resize.go | 37 +++++++++++++++++- lib/img.go | 38 ++++--------------- .../{resize.html => tools.resize.html} | 2 +- pages/{resize.go => tools.resize.go} | 0 ...les.resize.css => styles.tools.resize.css} | 0 stylegen/gen.sh | 2 +- 6 files changed, 46 insertions(+), 33 deletions(-) rename pages/templates/{resize.html => tools.resize.html} (93%) rename pages/{resize.go => tools.resize.go} (100%) rename public/css/{styles.resize.css => styles.tools.resize.css} (100%) diff --git a/api/resize.go b/api/resize.go index 7ea0a90..b4dd938 100644 --- a/api/resize.go +++ b/api/resize.go @@ -1,10 +1,45 @@ package api import ( + "fmt" + "net/http" + "strconv" + "atri.dad/lib" "github.com/labstack/echo/v4" ) func ResizeHandler(c echo.Context) error { - return lib.ResizeImg(c) + + // Extract file from request + file, _, err := c.Request().FormFile("image") + if err != nil { + return c.String(http.StatusBadRequest, "Error getting image file") + } + defer file.Close() + + // Get dimensions from form data parameters + widthStr := c.FormValue("width") + heightStr := c.FormValue("height") + + // Validate and convert dimensions to integers + width, err := strconv.Atoi(widthStr) + if err != nil { + return c.String(http.StatusBadRequest, "Invalid width parameter") + } + + height, err := strconv.Atoi(heightStr) + if err != nil { + return c.String(http.StatusBadRequest, "Invalid height parameter") + } + + fileBlob, err := lib.ResizeImg(file, width, height) + + if err != nil { + return c.String(http.StatusInternalServerError, err.Error()) + } + + c.Response().Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", "resized.png")) + + return c.Blob(http.StatusOK, "image/png", fileBlob) } diff --git a/lib/img.go b/lib/img.go index 0c373de..b43303e 100644 --- a/lib/img.go +++ b/lib/img.go @@ -2,49 +2,27 @@ package lib import ( "bytes" + "errors" "image" "image/png" "io" - "net/http" - "strconv" + "mime/multipart" "github.com/anthonynsimon/bild/transform" - "github.com/labstack/echo/v4" ) -func ResizeImg(c echo.Context) error { - // Extract file from request - file, _, err := c.Request().FormFile("image") - if err != nil { - return c.String(http.StatusBadRequest, "Error getting image file") - } - defer file.Close() - +func ResizeImg(file multipart.File, width int, height int) ([]byte, error) { // Read file content fileContent, err := io.ReadAll(file) if err != nil { - return c.String(http.StatusBadRequest, "Error reading image file") + return nil, errors.New("Error reading image file") + } // Decode image img, _, err := image.Decode(bytes.NewReader(fileContent)) if err != nil { - return c.String(http.StatusBadRequest, "Error decoding image") - } - - // Get dimensions from form data parameters - widthStr := c.FormValue("width") - heightStr := c.FormValue("height") - - // Validate and convert dimensions to integers - width, err := strconv.Atoi(widthStr) - if err != nil { - return c.String(http.StatusBadRequest, "Invalid width parameter") - } - - height, err := strconv.Atoi(heightStr) - if err != nil { - return c.String(http.StatusBadRequest, "Invalid height parameter") + return nil, errors.New("Error decoding image") } // Resize the image @@ -53,9 +31,9 @@ func ResizeImg(c echo.Context) error { // Encode the resized image as PNG buf := new(bytes.Buffer) if err := png.Encode(buf, resizedImg); err != nil { - return c.String(http.StatusInternalServerError, "Error encoding image to PNG") + return nil, errors.New("Error encoding image to PNG") } // Return the resized image as response - return c.Blob(http.StatusOK, "image/png", buf.Bytes()) + return buf.Bytes(), nil } diff --git a/pages/templates/resize.html b/pages/templates/tools.resize.html similarity index 93% rename from pages/templates/resize.html rename to pages/templates/tools.resize.html index 7884acd..6fc7a35 100644 --- a/pages/templates/resize.html +++ b/pages/templates/tools.resize.html @@ -7,7 +7,7 @@ Atridad Lahiji // Tools // Resizer {{end}} {{define "head"}} - + {{end}} {{define "main"}} diff --git a/pages/resize.go b/pages/tools.resize.go similarity index 100% rename from pages/resize.go rename to pages/tools.resize.go diff --git a/public/css/styles.resize.css b/public/css/styles.tools.resize.css similarity index 100% rename from public/css/styles.resize.css rename to public/css/styles.tools.resize.css diff --git a/stylegen/gen.sh b/stylegen/gen.sh index fb9729a..ee673c6 100755 --- a/stylegen/gen.sh +++ b/stylegen/gen.sh @@ -52,7 +52,7 @@ fi echo $BINARY # Infer pages from .html files in the pages directory -PAGES=$(ls ../pages/templates/*.html | xargs -n 1 basename | cut -d. -f1) +PAGES=$(ls ../pages/templates/*.html | xargs -n 1 basename | sed 's/\.[^.]*$//') # Run the binary for each page for PAGE in $PAGES; do