mkdir.go 1.1 KB

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