-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add image resize handler (#72)
* feat: add image resize handler --------- Co-authored-by: robert <juroberrttyb@gmail.com>
- Loading branch information
1 parent
685da91
commit fdec078
Showing
4 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package handlers | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/anthonynsimon/bild/imgio" | ||
"github.com/anthonynsimon/bild/transform" | ||
"github.com/gin-gonic/gin" | ||
"github.com/kevinanielsen/go-fast-cdn/util" | ||
) | ||
|
||
// TODO: add logging package | ||
func HandleImageResize(c *gin.Context) { | ||
body := struct { | ||
Filename string `json:"filename" binding:"required"` | ||
Width int `json:"width" binding:"required"` | ||
Height int `json:"height" binding:"required"` | ||
}{} | ||
if e := c.BindJSON(&body); e != nil { | ||
// TODO: add shared error handling accross handler package | ||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{ | ||
"error": e.Error(), | ||
}) | ||
return | ||
} | ||
|
||
filename, err := util.FilterFilename(body.Filename) | ||
if err != nil { | ||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{ | ||
"error": err.Error(), | ||
}) | ||
return | ||
} | ||
imgType := strings.Split(filename, ".")[1] | ||
|
||
filepath := filepath.Join(util.ExPath, "uploads", "images", filename) | ||
|
||
img, err := imgio.Open(filepath) | ||
if err != nil { | ||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{ | ||
"error": err.Error(), | ||
}) | ||
return | ||
} | ||
img = transform.Resize(img, body.Width, body.Height, transform.Linear) | ||
|
||
// TODO: a shared accepted image type data could be added to be shared between upload and resize api | ||
var encoder imgio.Encoder | ||
switch imgType { | ||
case "png": | ||
encoder = imgio.PNGEncoder() | ||
case "jpg", "jpeg": | ||
// 75 is the default quality encoding parameter | ||
encoder = imgio.JPEGEncoder(75) | ||
case "bmp": | ||
encoder = imgio.BMPEncoder() | ||
default: | ||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{ | ||
"error": fmt.Sprintf("Image of type %s is not supported", imgType), | ||
}) | ||
return | ||
} | ||
|
||
if err := imgio.Save(filepath, img, encoder); err != nil { | ||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{ | ||
"error": err.Error(), | ||
}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{ | ||
"status": "File resized successfully", | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters