xonecas.io
494
Ip

image-pal / commit 71fdb37

Clean up resize

sean2024-07-09 71fdb3714404adb28d5f686c83b7df6c0b31ebc2 parent 2b16af0

4 files changed

cmd/main.go+2−75
@@ -1,21 +1,10 @@
11 package main
22
33 import (
4- "io"
5- "log"
6- "net/http"
7- "strconv"
8-
94 "github.com/gin-gonic/gin"
10- imageTools "github.com/sacenox/image-pal/internal"
5+ imagePal "github.com/sacenox/image-pal/internal"
116 )
127
13-type ImageInput struct {
14- ImageUrl string `form:"image_url"`
15- Width string `form:"width"`
16- Height string `form:"height"`
17-}
18-
198 func main() {
209 r := gin.Default()
2110
@@ -25,69 +14,7 @@ func main() {
2514 })
2615 })
2716
28- r.GET("/resize", func(c *gin.Context) {
29- var action ImageInput
30-
31- if err := c.ShouldBind(&action); err != nil {
32- c.JSON(400, gin.H{
33- "msg": err.Error(),
34- })
35- return
36- }
37-
38- log.Print(action)
39-
40- // Get the image:
41- imageRequest, err := http.Get(action.ImageUrl)
42- if err != nil {
43- c.JSON(500, gin.H{
44- "msg": err.Error(),
45- })
46- return
47- }
48- defer imageRequest.Body.Close()
49-
50- buffer, err := io.ReadAll(imageRequest.Body)
51- if err != nil {
52- c.JSON(500, gin.H{
53- "msg": err.Error(),
54- })
55- return
56- }
57-
58- // TODO: Defaults for width and height
59- width, err := strconv.Atoi(action.Width)
60- if err != nil {
61- c.JSON(500, gin.H{
62- "msg": err.Error(),
63- })
64- return
65- }
66-
67- height, err := strconv.Atoi(action.Height)
68- if err != nil {
69- c.JSON(500, gin.H{
70- "msg": err.Error(),
71- })
72- return
73- }
74-
75- resizedBuffer, err := imageTools.Resize(
76- buffer, width, height,
77- )
78- if err != nil {
79- c.JSON(500, gin.H{
80- "msg": err.Error(),
81- })
82- return
83- }
84-
85- // TODO: Should we cache the result?
86- // err = bimg.Write(filename, resizedBuffer)
87-
88- contentType := imageRequest.Header.Get("content-type")
89- c.Data(200, contentType, resizedBuffer)
90- })
17+ r.GET("/resize", imagePal.ResizeRoute)
9118
9219 r.Run()
9320 }
internal/cache/googlelogo_light_color_272x92dp.png+0−0
Binary file, not shown.
internal/image-tools.go+3−3
@@ -1,11 +1,11 @@
1-package imageTools
1+package imagePal
22
33 import (
44 "github.com/h2non/bimg"
55 )
66
7-func Resize(buffer []byte, width int, height int) (resizedBuffer []byte, err error) {
8- resizedBuffer, err = bimg.NewImage(buffer).Resize(width, height)
7+func Resize(buffer *[]byte, width int, height int) (resizedBuffer []byte, err error) {
8+ resizedBuffer, err = bimg.NewImage(*buffer).Resize(width, height)
99 if err != nil {
1010 return nil, err
1111 }
internal/routing.go+83−0
@@ -0,0 +1,83 @@
1+package imagePal
2+
3+import (
4+ "io"
5+ "net/http"
6+ "strconv"
7+
8+ "github.com/gin-gonic/gin"
9+)
10+
11+func GetImage(url string) (buffer []byte, contentType string, err error) {
12+ imageRequest, err := http.Get(url)
13+ if err != nil {
14+ return nil, "", err
15+ }
16+ defer imageRequest.Body.Close()
17+
18+ buffer, err = io.ReadAll(imageRequest.Body)
19+ if err != nil {
20+ return nil, "", err
21+ }
22+
23+ contentType = imageRequest.Header.Get("content-type")
24+
25+ return buffer, contentType, nil
26+}
27+
28+type ResizeImageParams struct {
29+ ImageUrl string `form:"image_url"`
30+ Width string `form:"width"`
31+ Height string `form:"height"`
32+}
33+
34+func ResizeRoute(c *gin.Context) {
35+ var params ResizeImageParams
36+
37+ if err := c.ShouldBind(&params); err != nil {
38+ c.JSON(http.StatusBadRequest, gin.H{
39+ "msg": err.Error(),
40+ })
41+ return
42+ }
43+
44+ // TODO: Defaults for width and height
45+ width, err := strconv.Atoi(params.Width)
46+ if err != nil {
47+ c.JSON(http.StatusInternalServerError, gin.H{
48+ "msg": err.Error(),
49+ })
50+ return
51+ }
52+
53+ height, err := strconv.Atoi(params.Height)
54+ if err != nil {
55+ c.JSON(http.StatusInternalServerError, gin.H{
56+ "msg": err.Error(),
57+ })
58+ return
59+ }
60+
61+ buffer, contentType, err := GetImage(params.ImageUrl)
62+ if err != nil {
63+ c.JSON(http.StatusInternalServerError, gin.H{
64+ "msg": err.Error(),
65+ })
66+ return
67+ }
68+
69+ resizedBuffer, err := Resize(
70+ &buffer, width, height,
71+ )
72+ if err != nil {
73+ c.JSON(http.StatusInternalServerError, gin.H{
74+ "msg": err.Error(),
75+ })
76+ return
77+ }
78+
79+ // TODO: Should we cache the result?
80+ // err = bimg.Write(filename, resizedBuffer)
81+
82+ c.Data(http.StatusOK, contentType, resizedBuffer)
83+}