mkdir.go 972 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package config
  2. import (
  3. "net/http"
  4. "os"
  5. "github.com/0xJacky/Nginx-UI/internal/helper"
  6. "github.com/0xJacky/Nginx-UI/internal/nginx"
  7. "github.com/gin-gonic/gin"
  8. "github.com/uozi-tech/cosy"
  9. )
  10. func Mkdir(c *gin.Context) {
  11. var json struct {
  12. BasePath string `json:"base_path"`
  13. FolderName string `json:"folder_name"`
  14. }
  15. if !cosy.BindAndValid(c, &json) {
  16. return
  17. }
  18. // Ensure paths are properly URL unescaped
  19. decodedBasePath := helper.UnescapeURL(json.BasePath)
  20. decodedFolderName := helper.UnescapeURL(json.FolderName)
  21. fullPath := nginx.GetConfPath(decodedBasePath, decodedFolderName)
  22. if !helper.IsUnderDirectory(fullPath, nginx.GetConfPath()) {
  23. c.JSON(http.StatusForbidden, gin.H{
  24. "message": "You are not allowed to create a folder " +
  25. "outside of the nginx configuration directory",
  26. })
  27. return
  28. }
  29. err := os.Mkdir(fullPath, 0755)
  30. if err != nil {
  31. cosy.ErrHandler(c, err)
  32. return
  33. }
  34. c.JSON(http.StatusOK, gin.H{
  35. "message": "ok",
  36. })
  37. }