list.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package config
  2. import (
  3. "net/http"
  4. "net/url"
  5. "os"
  6. "strings"
  7. "github.com/0xJacky/Nginx-UI/internal/config"
  8. "github.com/gin-gonic/gin"
  9. "github.com/uozi-tech/cosy"
  10. )
  11. func GetConfigs(c *gin.Context) {
  12. name := c.Query("name")
  13. sortBy := c.Query("sort_by")
  14. order := c.DefaultQuery("order", "desc")
  15. // Get directory parameter
  16. encodedDir := c.DefaultQuery("dir", "/")
  17. // Handle cases where the path might be encoded multiple times
  18. dir := encodedDir
  19. // Try decoding until the path no longer changes
  20. for {
  21. newDecodedDir, decodeErr := url.QueryUnescape(dir)
  22. if decodeErr != nil {
  23. cosy.ErrHandler(c, decodeErr)
  24. return
  25. }
  26. if newDecodedDir == dir {
  27. break
  28. }
  29. dir = newDecodedDir
  30. }
  31. // Ensure the directory path format is correct
  32. dir = strings.TrimSpace(dir)
  33. if dir != "/" && strings.HasSuffix(dir, "/") {
  34. dir = strings.TrimSuffix(dir, "/")
  35. }
  36. configs, err := config.GetConfigList(dir, func(file os.FileInfo) bool {
  37. return name == "" || strings.Contains(file.Name(), name)
  38. })
  39. if err != nil {
  40. cosy.ErrHandler(c, err)
  41. return
  42. }
  43. configs = config.Sort(sortBy, order, configs)
  44. c.JSON(http.StatusOK, gin.H{
  45. "data": configs,
  46. })
  47. }