Template
1
0
Fork 0

re-worked the endpoints and stylegen logic

This commit is contained in:
Atridad Lahiji 2024-02-22 14:39:14 -07:00
parent 3bebca6b48
commit acd9ac6cdb
No known key found for this signature in database
6 changed files with 46 additions and 33 deletions

View file

@ -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)
}

View file

@ -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
}

View file

@ -7,7 +7,7 @@ Atridad Lahiji // Tools // Resizer
{{end}}
{{define "head"}}
<link rel="stylesheet" href="/public/css/styles.resize.css" />
<link rel="stylesheet" href="/public/css/styles.tools.resize.css" />
{{end}}
{{define "main"}}

View file

@ -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