micrach/utils/utils.go

214 lines
4.4 KiB
Go
Raw Permalink Normal View History

// TODO: move all functions to different packages
2021-09-04 21:53:48 +03:00
package utils
import (
"errors"
2021-09-12 14:06:52 +03:00
"image"
"image/jpeg"
"image/png"
2022-04-12 11:39:02 +03:00
"micrach/repositories"
2021-10-10 10:41:36 +03:00
"mime/multipart"
2021-09-04 21:53:48 +03:00
"os"
"path/filepath"
"strconv"
2021-09-12 14:06:52 +03:00
"github.com/disintegration/imaging"
2021-09-04 21:53:48 +03:00
)
2021-10-10 11:11:31 +03:00
type stringSlice []string
const UPLOADS_DIR_PATH = "uploads"
const FILE_SIZE_IN_BYTES = 3145728 // 3MB
2021-10-10 11:11:31 +03:00
var PERMITTED_FILE_EXTS = stringSlice{"image/jpeg", "image/png"} // 3MB
2021-09-04 21:53:48 +03:00
// Check dir existence.
func CheckIfFolderExists(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}
// Creates folder for uploads.
func CreateUploadsFolder() error {
isExists := CheckIfFolderExists(UPLOADS_DIR_PATH)
if isExists {
return nil
}
err := os.Mkdir(UPLOADS_DIR_PATH, 0755)
if err != nil {
return err
}
return nil
}
// Creates folder for thread.
func CreateThreadFolder(postID int) error {
threadDirPath := filepath.Join(UPLOADS_DIR_PATH, strconv.Itoa(postID))
isExists := CheckIfFolderExists(threadDirPath)
if isExists {
return errors.New("folder already exists")
}
err := os.Mkdir(threadDirPath, 0755)
if err != nil {
return err
}
originalsFolder := filepath.Join(threadDirPath, "o")
err = os.Mkdir(originalsFolder, 0755)
if err != nil {
return err
}
thumbnailsFolder := filepath.Join(threadDirPath, "t")
err = os.Mkdir(thumbnailsFolder, 0755)
if err != nil {
return err
}
2021-09-04 21:53:48 +03:00
return nil
}
2021-09-11 16:56:22 +03:00
2022-04-12 11:39:02 +03:00
// TODO: Delete after
func ValidatePost(title, text string, files []*multipart.FileHeader) string {
if text == "" && len(files) == 0 {
2022-04-12 11:39:02 +03:00
return repositories.InvalidTextOrFilesErrorMessage
}
2022-02-03 18:22:46 +03:00
if len([]rune(title)) > 100 {
2022-04-12 11:39:02 +03:00
return repositories.InvalidTitleLengthErrorMessage
}
2022-02-03 18:22:46 +03:00
if len([]rune(text)) > 1000 {
2022-04-12 11:39:02 +03:00
return repositories.InvalidTextLengthErrorMessage
}
if len(files) > 4 {
2022-04-12 11:39:02 +03:00
return repositories.InvalidFilesLengthErrorMessage
}
isFilesExtsValid := CheckFilesExt(files)
if !isFilesExtsValid {
2022-04-12 11:39:02 +03:00
return repositories.InvalidFileExtErrorMessage
}
isFilesSizesNotToBig := CheckFilesSize(files)
if !isFilesSizesNotToBig {
2022-04-12 11:39:02 +03:00
return repositories.InvalidFileSizeErrorMessage
}
return ""
2021-09-11 16:56:22 +03:00
}
2021-09-12 14:06:52 +03:00
2022-04-13 09:38:15 +03:00
func ValidatePost2(title, text string, files []*multipart.FileHeader) *repositories.Errors {
validationErrors := repositories.Errors{}
2022-04-12 11:39:02 +03:00
hasErrors := false
if text == "" && len(files) == 0 {
2022-04-12 11:41:31 +03:00
validationErrors.Text = repositories.InvalidTextOrFilesErrorMessage
validationErrors.Files = repositories.InvalidTextOrFilesErrorMessage
2022-04-12 11:39:02 +03:00
hasErrors = true
}
if len([]rune(title)) > 100 {
2022-04-12 11:41:31 +03:00
validationErrors.Title = repositories.InvalidTitleLengthErrorMessage
2022-04-12 11:39:02 +03:00
hasErrors = true
}
if len([]rune(text)) > 1000 {
2022-04-12 11:41:31 +03:00
validationErrors.Text = repositories.InvalidTextLengthErrorMessage
2022-04-12 11:39:02 +03:00
hasErrors = true
}
if len(files) > 4 {
2022-04-12 11:41:31 +03:00
validationErrors.Files = repositories.InvalidFilesLengthErrorMessage
2022-04-12 11:39:02 +03:00
hasErrors = true
}
isFilesExtsValid := CheckFilesExt(files)
if !isFilesExtsValid {
2022-04-12 11:41:31 +03:00
validationErrors.Files = repositories.InvalidFileExtErrorMessage
2022-04-12 11:39:02 +03:00
hasErrors = true
}
isFilesSizesNotToBig := CheckFilesSize(files)
if !isFilesSizesNotToBig {
2022-04-12 11:41:31 +03:00
validationErrors.Files = repositories.InvalidFileSizeErrorMessage
2022-04-12 11:39:02 +03:00
hasErrors = true
}
if hasErrors {
2022-04-13 09:38:15 +03:00
return &validationErrors
2022-04-12 11:39:02 +03:00
}
return nil
}
2021-10-10 10:41:36 +03:00
func CheckFilesSize(files []*multipart.FileHeader) bool {
for _, file := range files {
if file.Size > int64(FILE_SIZE_IN_BYTES) {
return false
}
}
return true
}
2021-10-10 11:11:31 +03:00
func CheckFilesExt(files []*multipart.FileHeader) bool {
for _, file := range files {
ext := file.Header.Get("Content-Type")
if !PERMITTED_FILE_EXTS.includes(ext) {
return false
}
}
2021-10-10 10:41:36 +03:00
2021-10-10 11:11:31 +03:00
return true
}
func (ss stringSlice) includes(toCheck string) bool {
for _, s := range ss {
if toCheck == s {
return true
}
}
return false
}
2021-10-10 10:41:36 +03:00
2021-09-12 14:06:52 +03:00
func MakeImageThumbnail(originalPath, ext string, threadID, fileID int) (*image.NRGBA, error) {
img, err := imaging.Open(originalPath, imaging.AutoOrientation(true))
if err != nil {
return nil, err
}
dstImage := imaging.Resize(img, 0, 150, imaging.NearestNeighbor)
return dstImage, nil
}
func SaveImageThumbnail(img *image.NRGBA, threadID, fileID int, ext string) error {
thumbnailPath := filepath.Join(
UPLOADS_DIR_PATH,
strconv.Itoa(threadID),
"t",
strconv.Itoa(fileID)+"."+ext,
)
f, err := os.Create(thumbnailPath)
if err != nil {
return err
}
switch ext {
case "png":
err = png.Encode(f, img)
case "jpeg":
err = jpeg.Encode(f, img, nil)
}
if err != nil {
return err
}
return nil
}