mirror of
https://github.com/yanislav-igonin/micrach
synced 2024-12-22 14:22:33 +03:00
feat: add folders utils
This commit is contained in:
parent
cc5ded555a
commit
8708374f64
7
main.go
7
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")
|
||||
|
48
utils/utils.go
Normal file
48
utils/utils.go
Normal file
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user