micrach/utils/utils.go

66 lines
1.2 KiB
Go
Raw Normal View History

2021-09-04 21:53:48 +03:00
package utils
import (
"errors"
"os"
"path/filepath"
"strconv"
)
var UPLOADS_DIR_PATH = "uploads"
// 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
func ValidatePost(title, text string) bool {
2021-09-12 00:30:48 +03:00
return (title == "" && text != "") ||
(title != "" && text == "") ||
(title != "" && text != "")
2021-09-11 16:56:22 +03:00
}