feat: add files size check

This commit is contained in:
Yanislav Igonin 2021-10-10 10:41:36 +03:00
parent 2491d5b680
commit 44c1f56e9f
3 changed files with 35 additions and 0 deletions

View File

@ -125,6 +125,14 @@ func CreateThread(c *gin.Context) {
} }
filesInRequest := form.File["files"] filesInRequest := form.File["files"]
isFilesSizesNotToBig := Utils.CheckFilesSize(filesInRequest)
if !isFilesSizesNotToBig {
errorHtmlData := Repositories.BadRequestHtmlData{
Message: Repositories.InvalidFileSizeMessage,
}
c.HTML(http.StatusInternalServerError, "400.html", errorHtmlData)
return
}
conn, err := Db.Pool.Acquire(context.TODO()) conn, err := Db.Pool.Acquire(context.TODO())
if err != nil { if err != nil {
@ -249,6 +257,15 @@ func UpdateThread(c *gin.Context) {
} }
filesInRequest := form.File["files"] filesInRequest := form.File["files"]
isFilesSizesNotToBig := Utils.CheckFilesSize(filesInRequest)
if !isFilesSizesNotToBig {
errorHtmlData := Repositories.BadRequestHtmlData{
Message: Repositories.InvalidFileSizeMessage,
}
c.HTML(http.StatusInternalServerError, "400.html", errorHtmlData)
return
}
isSageField := form.Value["sage"] isSageField := form.Value["sage"]
var isSageString string var isSageString string
if len(isSageField) != 0 { if len(isSageField) != 0 {

View File

@ -59,3 +59,4 @@ type BadRequestHtmlData struct {
var InvalidTitleOrTextErrorMessage = "TITLE OR TEXT SHOULD NOT BE EMPTY" var InvalidTitleOrTextErrorMessage = "TITLE OR TEXT SHOULD NOT BE EMPTY"
var InvalidCaptchaErrorMessage = "INVALID CAPTCHA" var InvalidCaptchaErrorMessage = "INVALID CAPTCHA"
var InvalidFileSizeMessage = "FILE SIZE EXCIDED (3MB PER FILE)"

View File

@ -5,6 +5,7 @@ import (
"image" "image"
"image/jpeg" "image/jpeg"
"image/png" "image/png"
"mime/multipart"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
@ -13,6 +14,7 @@ import (
) )
var UPLOADS_DIR_PATH = "uploads" var UPLOADS_DIR_PATH = "uploads"
var FILE_SIZE_IN_BYTES = 3145728 // 3MB
// Check dir existence. // Check dir existence.
func CheckIfFolderExists(path string) bool { func CheckIfFolderExists(path string) bool {
@ -63,12 +65,27 @@ func CreateThreadFolder(postID int) error {
return nil return nil
} }
// TODO: add files length check
func ValidatePost(title, text string) bool { func ValidatePost(title, text string) bool {
return (title == "" && text != "") || return (title == "" && text != "") ||
(title != "" && text == "") || (title != "" && text == "") ||
(title != "" && text != "") (title != "" && text != "")
} }
func CheckFilesSize(files []*multipart.FileHeader) bool {
for _, file := range files {
if file.Size > int64(FILE_SIZE_IN_BYTES) {
return false
}
}
return true
}
// func CheckFilesExt(){
// }
func MakeImageThumbnail(originalPath, ext string, threadID, fileID int) (*image.NRGBA, error) { func MakeImageThumbnail(originalPath, ext string, threadID, fileID int) (*image.NRGBA, error) {
img, err := imaging.Open(originalPath, imaging.AutoOrientation(true)) img, err := imaging.Open(originalPath, imaging.AutoOrientation(true))
if err != nil { if err != nil {