diff --git a/main.go b/main.go index e350ef1..48ac460 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,7 @@ import ( Controllers "micrach/controllers" Db "micrach/db" Repositories "micrach/repositories" - // Utils "micrach/utils" + Utils "micrach/utils" ) func main() { @@ -21,6 +21,11 @@ func main() { gin.SetMode(Config.App.Env) Repositories.Seed() + err := Utils.CreateUploadsFolder() + if err != nil { + log.Panicln(err) + } + router := gin.Default() router.LoadHTMLGlob("templates/*.html") router.Static("/uploads", "./uploads") diff --git a/utils/utils.go b/utils/utils.go new file mode 100644 index 0000000..a308011 --- /dev/null +++ b/utils/utils.go @@ -0,0 +1,48 @@ +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 + } + + return nil +}