diff --git a/controllers/threads_controller.go b/controllers/threads_controller.go index e9ef06c..2e04728 100644 --- a/controllers/threads_controller.go +++ b/controllers/threads_controller.go @@ -115,28 +115,11 @@ func CreateThread(c *gin.Context) { // TODO: dat shit crashes if no fields in request text := form.Value["text"][0] title := form.Value["title"][0] - isPostValid := Utils.ValidatePost(title, text) - if !isPostValid { - errorHtmlData := Repositories.BadRequestHtmlData{ - Message: Repositories.InvalidTitleOrTextErrorMessage, - } - c.HTML(http.StatusInternalServerError, "400.html", errorHtmlData) - return - } - filesInRequest := form.File["files"] - isFilesExtsValid := Utils.CheckFilesExt(filesInRequest) - if !isFilesExtsValid { + validationErrorMessage := Utils.ValidatePost(title, text, filesInRequest) + if validationErrorMessage != "" { 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.InvalidFileSizeErrorMessage, + Message: validationErrorMessage, } c.HTML(http.StatusInternalServerError, "400.html", errorHtmlData) return @@ -255,28 +238,11 @@ func UpdateThread(c *gin.Context) { // TODO: dat shit crashes if no fields in request text := form.Value["text"][0] - isPostValid := Utils.ValidatePost("", text) - if !isPostValid { - errorHtmlData := Repositories.BadRequestHtmlData{ - Message: Repositories.InvalidTitleOrTextErrorMessage, - } - c.HTML(http.StatusInternalServerError, "400.html", errorHtmlData) - return - } - filesInRequest := form.File["files"] - isFilesExtsValid := Utils.CheckFilesExt(filesInRequest) - if !isFilesExtsValid { + validationErrorMessage := Utils.ValidatePost("", text, filesInRequest) + if validationErrorMessage != "" { 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.InvalidFileSizeErrorMessage, + Message: validationErrorMessage, } c.HTML(http.StatusInternalServerError, "400.html", errorHtmlData) return diff --git a/repositories/structs.go b/repositories/structs.go index 581c054..6092278 100644 --- a/repositories/structs.go +++ b/repositories/structs.go @@ -57,7 +57,10 @@ type BadRequestHtmlData struct { Message string } -const InvalidTitleOrTextErrorMessage = "TITLE OR TEXT SHOULD NOT BE EMPTY" const InvalidCaptchaErrorMessage = "INVALID CAPTCHA" +const InvalidTextOrFilesErrorMessage = "TEXT OR FILES SHOULD NOT BE EMPTY" +const InvalidTitleLengthErrorMessage = "TITLE SHOULD NOT EXCEED 100 CHARS" +const InvalidTextLengthErrorMessage = "TEXT SHOULD NOT EXCEED 1000 CHARS" +const InvalidFilesLengthErrorMessage = "MAXIMUM 4 FILES CAN BE UPLOADED" 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 bc4ad00..c496314 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -5,6 +5,7 @@ import ( "image" "image/jpeg" "image/png" + Repositories "micrach/repositories" "mime/multipart" "os" "path/filepath" @@ -13,10 +14,10 @@ import ( "github.com/disintegration/imaging" ) -const UPLOADS_DIR_PATH = "uploads" -const FILE_SIZE_IN_BYTES = 3145728 // 3MB type stringSlice []string +const UPLOADS_DIR_PATH = "uploads" +const FILE_SIZE_IN_BYTES = 3145728 // 3MB var PERMITTED_FILE_EXTS = stringSlice{"image/jpeg", "image/png"} // 3MB // Check dir existence. @@ -68,11 +69,34 @@ func CreateThreadFolder(postID int) error { return nil } -// TODO: add files length check -func ValidatePost(title, text string) bool { - return (title == "" && text != "") || - (title != "" && text == "") || - (title != "" && text != "") +func ValidatePost(title, text string, files []*multipart.FileHeader) string { + if text == "" && len(files) == 0 { + return Repositories.InvalidTextOrFilesErrorMessage + } + + if len(title) > 100 { + return Repositories.InvalidTitleLengthErrorMessage + } + + if len(text) > 1000 { + return Repositories.InvalidTextLengthErrorMessage + } + + if len(files) > 4 { + return Repositories.InvalidFilesLengthErrorMessage + } + + isFilesExtsValid := CheckFilesExt(files) + if !isFilesExtsValid { + return Repositories.InvalidFileExtErrorMessage + } + + isFilesSizesNotToBig := CheckFilesSize(files) + if !isFilesSizesNotToBig { + return Repositories.InvalidFileSizeErrorMessage + } + + return "" } func CheckFilesSize(files []*multipart.FileHeader) bool {