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
|
// TODO: dat shit crashes if no fields in request
|
||||||
text := form.Value["text"][0]
|
text := form.Value["text"][0]
|
||||||
title := form.Value["title"][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"]
|
filesInRequest := form.File["files"]
|
||||||
isFilesExtsValid := Utils.CheckFilesExt(filesInRequest)
|
validationErrorMessage := Utils.ValidatePost(title, text, filesInRequest)
|
||||||
if !isFilesExtsValid {
|
if validationErrorMessage != "" {
|
||||||
errorHtmlData := Repositories.BadRequestHtmlData{
|
errorHtmlData := Repositories.BadRequestHtmlData{
|
||||||
Message: Repositories.InvalidFileExtErrorMessage,
|
Message: validationErrorMessage,
|
||||||
}
|
|
||||||
c.HTML(http.StatusInternalServerError, "400.html", errorHtmlData)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
isFilesSizesNotToBig := Utils.CheckFilesSize(filesInRequest)
|
|
||||||
if !isFilesSizesNotToBig {
|
|
||||||
errorHtmlData := Repositories.BadRequestHtmlData{
|
|
||||||
Message: Repositories.InvalidFileSizeErrorMessage,
|
|
||||||
}
|
}
|
||||||
c.HTML(http.StatusInternalServerError, "400.html", errorHtmlData)
|
c.HTML(http.StatusInternalServerError, "400.html", errorHtmlData)
|
||||||
return
|
return
|
||||||
@ -255,28 +238,11 @@ func UpdateThread(c *gin.Context) {
|
|||||||
|
|
||||||
// TODO: dat shit crashes if no fields in request
|
// TODO: dat shit crashes if no fields in request
|
||||||
text := form.Value["text"][0]
|
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"]
|
filesInRequest := form.File["files"]
|
||||||
isFilesExtsValid := Utils.CheckFilesExt(filesInRequest)
|
validationErrorMessage := Utils.ValidatePost("", text, filesInRequest)
|
||||||
if !isFilesExtsValid {
|
if validationErrorMessage != "" {
|
||||||
errorHtmlData := Repositories.BadRequestHtmlData{
|
errorHtmlData := Repositories.BadRequestHtmlData{
|
||||||
Message: Repositories.InvalidFileExtErrorMessage,
|
Message: validationErrorMessage,
|
||||||
}
|
|
||||||
c.HTML(http.StatusInternalServerError, "400.html", errorHtmlData)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
isFilesSizesNotToBig := Utils.CheckFilesSize(filesInRequest)
|
|
||||||
if !isFilesSizesNotToBig {
|
|
||||||
errorHtmlData := Repositories.BadRequestHtmlData{
|
|
||||||
Message: Repositories.InvalidFileSizeErrorMessage,
|
|
||||||
}
|
}
|
||||||
c.HTML(http.StatusInternalServerError, "400.html", errorHtmlData)
|
c.HTML(http.StatusInternalServerError, "400.html", errorHtmlData)
|
||||||
return
|
return
|
||||||
|
@ -57,7 +57,10 @@ type BadRequestHtmlData struct {
|
|||||||
Message string
|
Message string
|
||||||
}
|
}
|
||||||
|
|
||||||
const InvalidTitleOrTextErrorMessage = "TITLE OR TEXT SHOULD NOT BE EMPTY"
|
|
||||||
const InvalidCaptchaErrorMessage = "INVALID CAPTCHA"
|
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 InvalidFileSizeErrorMessage = "FILE SIZE EXCIDED (3MB PER FILE)"
|
||||||
const InvalidFileExtErrorMessage = "AVALIABLE FILE EXT: PNG, JPG"
|
const InvalidFileExtErrorMessage = "AVALIABLE FILE EXT: PNG, JPG"
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"image"
|
"image"
|
||||||
"image/jpeg"
|
"image/jpeg"
|
||||||
"image/png"
|
"image/png"
|
||||||
|
Repositories "micrach/repositories"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -13,10 +14,10 @@ import (
|
|||||||
"github.com/disintegration/imaging"
|
"github.com/disintegration/imaging"
|
||||||
)
|
)
|
||||||
|
|
||||||
const UPLOADS_DIR_PATH = "uploads"
|
|
||||||
const FILE_SIZE_IN_BYTES = 3145728 // 3MB
|
|
||||||
type stringSlice []string
|
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
|
var PERMITTED_FILE_EXTS = stringSlice{"image/jpeg", "image/png"} // 3MB
|
||||||
|
|
||||||
// Check dir existence.
|
// Check dir existence.
|
||||||
@ -68,11 +69,34 @@ func CreateThreadFolder(postID int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: add files length check
|
func ValidatePost(title, text string, files []*multipart.FileHeader) string {
|
||||||
func ValidatePost(title, text string) bool {
|
if text == "" && len(files) == 0 {
|
||||||
return (title == "" && text != "") ||
|
return Repositories.InvalidTextOrFilesErrorMessage
|
||||||
(title != "" && text == "") ||
|
}
|
||||||
(title != "" && text != "")
|
|
||||||
|
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 {
|
func CheckFilesSize(files []*multipart.FileHeader) bool {
|
||||||
|
Loading…
Reference in New Issue
Block a user