feat: add files ext check

This commit is contained in:
Yanislav Igonin 2021-10-10 11:11:31 +03:00
parent a87101d1ea
commit 86b7750a36
3 changed files with 41 additions and 5 deletions

View File

@ -125,10 +125,18 @@ func CreateThread(c *gin.Context) {
}
filesInRequest := form.File["files"]
isFilesExtsValid := Utils.CheckFilesExt(filesInRequest)
if !isFilesExtsValid {
errorHtmlData := Repositories.BadRequestHtmlData{
Message: Repositories.InvalidFileExtErrorMessage,
}
c.HTML(http.StatusInternalServerError, "400.html", errorHtmlData)
return
}
isFilesSizesNotToBig := Utils.CheckFilesSize(filesInRequest)
if !isFilesSizesNotToBig {
errorHtmlData := Repositories.BadRequestHtmlData{
Message: Repositories.InvalidFileSizeMessage,
Message: Repositories.InvalidFileSizeErrorMessage,
}
c.HTML(http.StatusInternalServerError, "400.html", errorHtmlData)
return
@ -257,10 +265,18 @@ func UpdateThread(c *gin.Context) {
}
filesInRequest := form.File["files"]
isFilesExtsValid := Utils.CheckFilesExt(filesInRequest)
if !isFilesExtsValid {
errorHtmlData := Repositories.BadRequestHtmlData{
Message: Repositories.InvalidFileExtErrorMessage,
}
c.HTML(http.StatusInternalServerError, "400.html", errorHtmlData)
return
}
isFilesSizesNotToBig := Utils.CheckFilesSize(filesInRequest)
if !isFilesSizesNotToBig {
errorHtmlData := Repositories.BadRequestHtmlData{
Message: Repositories.InvalidFileSizeMessage,
Message: Repositories.InvalidFileSizeErrorMessage,
}
c.HTML(http.StatusInternalServerError, "400.html", errorHtmlData)
return

View File

@ -59,4 +59,5 @@ type BadRequestHtmlData struct {
const InvalidTitleOrTextErrorMessage = "TITLE OR TEXT SHOULD NOT BE EMPTY"
const InvalidCaptchaErrorMessage = "INVALID CAPTCHA"
const InvalidFileSizeMessage = "FILE SIZE EXCIDED (3MB PER FILE)"
const InvalidFileSizeErrorMessage = "FILE SIZE EXCIDED (3MB PER FILE)"
const InvalidFileExtErrorMessage = "AVALIABLE FILE EXT: PNG, JPG"

View File

@ -15,6 +15,9 @@ import (
const UPLOADS_DIR_PATH = "uploads"
const FILE_SIZE_IN_BYTES = 3145728 // 3MB
type stringSlice []string
var PERMITTED_FILE_EXTS = stringSlice{"image/jpeg", "image/png"} // 3MB
// Check dir existence.
func CheckIfFolderExists(path string) bool {
@ -82,9 +85,25 @@ func CheckFilesSize(files []*multipart.FileHeader) bool {
return true
}
// func CheckFilesExt(){
func CheckFilesExt(files []*multipart.FileHeader) bool {
for _, file := range files {
ext := file.Header.Get("Content-Type")
if !PERMITTED_FILE_EXTS.includes(ext) {
return false
}
}
// }
return true
}
func (ss stringSlice) includes(toCheck string) bool {
for _, s := range ss {
if toCheck == s {
return true
}
}
return false
}
func MakeImageThumbnail(originalPath, ext string, threadID, fileID int) (*image.NRGBA, error) {
img, err := imaging.Open(originalPath, imaging.AutoOrientation(true))