re-worked the endpoints and stylegen logic
This commit is contained in:
parent
3bebca6b48
commit
acd9ac6cdb
6 changed files with 46 additions and 33 deletions
|
@ -1,10 +1,45 @@
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"atri.dad/lib"
|
"atri.dad/lib"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ResizeHandler(c echo.Context) error {
|
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)
|
||||||
}
|
}
|
||||||
|
|
38
lib/img.go
38
lib/img.go
|
@ -2,49 +2,27 @@ package lib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
"image"
|
"image"
|
||||||
"image/png"
|
"image/png"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"mime/multipart"
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/anthonynsimon/bild/transform"
|
"github.com/anthonynsimon/bild/transform"
|
||||||
"github.com/labstack/echo/v4"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func ResizeImg(c echo.Context) error {
|
func ResizeImg(file multipart.File, width int, height int) ([]byte, 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()
|
|
||||||
|
|
||||||
// Read file content
|
// Read file content
|
||||||
fileContent, err := io.ReadAll(file)
|
fileContent, err := io.ReadAll(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.String(http.StatusBadRequest, "Error reading image file")
|
return nil, errors.New("Error reading image file")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode image
|
// Decode image
|
||||||
img, _, err := image.Decode(bytes.NewReader(fileContent))
|
img, _, err := image.Decode(bytes.NewReader(fileContent))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.String(http.StatusBadRequest, "Error decoding image")
|
return nil, errors.New("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")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resize the image
|
// Resize the image
|
||||||
|
@ -53,9 +31,9 @@ func ResizeImg(c echo.Context) error {
|
||||||
// Encode the resized image as PNG
|
// Encode the resized image as PNG
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
if err := png.Encode(buf, resizedImg); err != nil {
|
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 the resized image as response
|
||||||
return c.Blob(http.StatusOK, "image/png", buf.Bytes())
|
return buf.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ Atridad Lahiji // Tools // Resizer
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
{{define "head"}}
|
{{define "head"}}
|
||||||
<link rel="stylesheet" href="/public/css/styles.resize.css" />
|
<link rel="stylesheet" href="/public/css/styles.tools.resize.css" />
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
{{define "main"}}
|
{{define "main"}}
|
|
@ -52,7 +52,7 @@ fi
|
||||||
echo $BINARY
|
echo $BINARY
|
||||||
|
|
||||||
# Infer pages from .html files in the pages directory
|
# 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
|
# Run the binary for each page
|
||||||
for PAGE in $PAGES; do
|
for PAGE in $PAGES; do
|
||||||
|
|
Loading…
Add table
Reference in a new issue