modify.go 3.0 KB

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