modify.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package config
  2. import (
  3. "net/http"
  4. "net/url"
  5. "path/filepath"
  6. "time"
  7. "github.com/0xJacky/Nginx-UI/internal/config"
  8. "github.com/0xJacky/Nginx-UI/internal/helper"
  9. "github.com/0xJacky/Nginx-UI/internal/nginx"
  10. "github.com/0xJacky/Nginx-UI/model"
  11. "github.com/0xJacky/Nginx-UI/query"
  12. "github.com/gin-gonic/gin"
  13. "github.com/sashabaranov/go-openai"
  14. "github.com/uozi-tech/cosy"
  15. "gorm.io/gen/field"
  16. )
  17. type EditConfigJson struct {
  18. Content string `json:"content" binding:"required"`
  19. }
  20. func EditConfig(c *gin.Context) {
  21. relativePath := c.Param("path")
  22. // Ensure the path is correctly decoded - handle cases where it might be encoded multiple times
  23. decodedPath := relativePath
  24. var err error
  25. // Try decoding until the path no longer changes
  26. for {
  27. newDecodedPath, decodeErr := url.PathUnescape(decodedPath)
  28. if decodeErr != nil || newDecodedPath == decodedPath {
  29. break
  30. }
  31. decodedPath = newDecodedPath
  32. }
  33. relativePath = decodedPath
  34. var json struct {
  35. Content string `json:"content"`
  36. SyncOverwrite bool `json:"sync_overwrite"`
  37. SyncNodeIds []uint64 `json:"sync_node_ids"`
  38. }
  39. if !cosy.BindAndValid(c, &json) {
  40. return
  41. }
  42. absPath := nginx.GetConfPath(relativePath)
  43. if !helper.FileExists(absPath) {
  44. c.JSON(http.StatusNotFound, gin.H{
  45. "message": "file not found",
  46. })
  47. return
  48. }
  49. q := query.Config
  50. cfg, err := q.Assign(field.Attrs(&model.Config{
  51. Filepath: absPath,
  52. })).Where(q.Filepath.Eq(absPath)).FirstOrCreate()
  53. if err != nil {
  54. return
  55. }
  56. // Update database record
  57. _, err = q.Where(q.Filepath.Eq(absPath)).
  58. Select(q.SyncNodeIds, q.SyncOverwrite).
  59. Updates(&model.Config{
  60. SyncNodeIds: json.SyncNodeIds,
  61. SyncOverwrite: json.SyncOverwrite,
  62. })
  63. if err != nil {
  64. return
  65. }
  66. cfg.SyncNodeIds = json.SyncNodeIds
  67. cfg.SyncOverwrite = json.SyncOverwrite
  68. content := json.Content
  69. err = config.Save(absPath, content, cfg)
  70. if err != nil {
  71. cosy.ErrHandler(c, err)
  72. return
  73. }
  74. g := query.ChatGPTLog
  75. chatgpt, err := g.Where(g.Name.Eq(absPath)).FirstOrCreate()
  76. if err != nil {
  77. cosy.ErrHandler(c, err)
  78. return
  79. }
  80. if chatgpt.Content == nil {
  81. chatgpt.Content = make([]openai.ChatCompletionMessage, 0)
  82. }
  83. c.JSON(http.StatusOK, config.Config{
  84. Name: filepath.Base(absPath),
  85. Content: content,
  86. ChatGPTMessages: chatgpt.Content,
  87. FilePath: absPath,
  88. ModifiedAt: time.Now(),
  89. Dir: filepath.Dir(relativePath),
  90. SyncNodeIds: cfg.SyncNodeIds,
  91. SyncOverwrite: cfg.SyncOverwrite,
  92. })
  93. }