From 86b7750a3658136288d895579cd89802d8cd0ad2 Mon Sep 17 00:00:00 2001 From: Yanislav Igonin Date: Sun, 10 Oct 2021 11:11:31 +0300 Subject: [PATCH] feat: add files ext check --- controllers/threads_controller.go | 20 ++++++++++++++++++-- repositories/structs.go | 3 ++- utils/utils.go | 23 +++++++++++++++++++++-- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/controllers/threads_controller.go b/controllers/threads_controller.go index a7fde5e..e9ef06c 100644 --- a/controllers/threads_controller.go +++ b/controllers/threads_controller.go @@ -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 diff --git a/repositories/structs.go b/repositories/structs.go index 201dd9c..581c054 100644 --- a/repositories/structs.go +++ b/repositories/structs.go @@ -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" diff --git a/utils/utils.go b/utils/utils.go index b34a57d..bc4ad00 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -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))