-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated addTemplateImg(): dimension check (#59)
- Loading branch information
Showing
1 changed file
with
15 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ package routes | |
import ( | ||
"encoding/json" | ||
"fmt" | ||
"image" | ||
_ "image/png" | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
|
@@ -42,8 +44,6 @@ func imageToPixelData(imageData []byte) []byte { | |
} | ||
|
||
func addTemplateImg(w http.ResponseWriter, r *http.Request) { | ||
// TODO: Limit file size / proportions between 5x5 and 64x64 | ||
// Passed like this curl -F "[email protected]" http://localhost:8080/addTemplateImg | ||
file, _, err := r.FormFile("image") | ||
if err != nil { | ||
panic(err) | ||
|
@@ -58,6 +58,19 @@ func addTemplateImg(w http.ResponseWriter, r *http.Request) { | |
} | ||
defer tempFile.Close() | ||
|
||
// Decode the image to check dimensions | ||
img, format, err := image.Decode(file) | ||
if err != nil { | ||
http.Error(w, "Failed to decode the image: "+err.Error()+" - format: "+format, http.StatusBadRequest) | ||
return | ||
} | ||
bounds := img.Bounds() | ||
width, height := bounds.Max.X-bounds.Min.X, bounds.Max.Y-bounds.Min.Y | ||
if width < 5 || width > 50 || height < 5 || height > 50 { | ||
http.Error(w, fmt.Sprintf("Image dimensions out of allowed range (5x5 to 50x50). Uploaded image size: %dx%d", width, height), http.StatusBadRequest) | ||
return | ||
} | ||
|
||
// Read all data from the uploaded file and write it to the temporary file | ||
fileBytes, err := ioutil.ReadAll(file) | ||
if err != nil { | ||
|