mirror of
https://github.com/yanislav-igonin/micrach
synced 2024-12-22 14:22:33 +03:00
feat: update post validation
title - 100 text - 1000 files - 4 text or files in post
This commit is contained in:
parent
86b7750a36
commit
fb29be59ae
@ -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
|
||||
|
@ -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"
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user