modify.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package config
  2. import (
  3. "github.com/0xJacky/Nginx-UI/api"
  4. "github.com/0xJacky/Nginx-UI/internal/config"
  5. "github.com/0xJacky/Nginx-UI/internal/helper"
  6. "github.com/0xJacky/Nginx-UI/internal/nginx"
  7. "github.com/0xJacky/Nginx-UI/model"
  8. "github.com/0xJacky/Nginx-UI/query"
  9. "github.com/gin-gonic/gin"
  10. "github.com/sashabaranov/go-openai"
  11. "net/http"
  12. "os"
  13. "time"
  14. )
  15. type EditConfigJson struct {
  16. Content string `json:"content" binding:"required"`
  17. }
  18. func EditConfig(c *gin.Context) {
  19. name := c.Param("name")
  20. var json struct {
  21. Name string `json:"name" binding:"required"`
  22. Filepath string `json:"filepath" binding:"required"`
  23. NewFilepath string `json:"new_filepath" binding:"required"`
  24. Content string `json:"content"`
  25. SyncOverwrite bool `json:"sync_overwrite"`
  26. SyncNodeIds []int `json:"sync_node_ids"`
  27. }
  28. if !api.BindAndValid(c, &json) {
  29. return
  30. }
  31. path := json.Filepath
  32. if !helper.IsUnderDirectory(path, nginx.GetConfPath()) {
  33. c.JSON(http.StatusForbidden, gin.H{
  34. "message": "filepath is not under the nginx conf path",
  35. })
  36. return
  37. }
  38. if !helper.IsUnderDirectory(json.NewFilepath, nginx.GetConfPath()) {
  39. c.JSON(http.StatusForbidden, gin.H{
  40. "message": "new filepath is not under the nginx conf path",
  41. })
  42. return
  43. }
  44. if !helper.FileExists(path) {
  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(path)
  52. if err != nil {
  53. api.ErrHandler(c, err)
  54. return
  55. }
  56. if content != "" && content != string(origContent) {
  57. err = os.WriteFile(path, []byte(content), 0644)
  58. if err != nil {
  59. api.ErrHandler(c, err)
  60. return
  61. }
  62. }
  63. q := query.Config
  64. cfg, err := q.Where(q.Filepath.Eq(json.Filepath)).FirstOrCreate()
  65. if err != nil {
  66. api.ErrHandler(c, err)
  67. return
  68. }
  69. _, err = q.Where(q.Filepath.Eq(json.Filepath)).
  70. Select(q.Name, q.Filepath, q.SyncNodeIds, q.SyncOverwrite).
  71. Updates(&model.Config{
  72. Name: json.Name,
  73. Filepath: json.NewFilepath,
  74. SyncNodeIds: json.SyncNodeIds,
  75. SyncOverwrite: json.SyncOverwrite,
  76. })
  77. if err != nil {
  78. api.ErrHandler(c, err)
  79. return
  80. }
  81. g := query.ChatGPTLog
  82. // handle rename
  83. if path != json.NewFilepath {
  84. if helper.FileExists(json.NewFilepath) {
  85. c.JSON(http.StatusNotAcceptable, gin.H{
  86. "message": "File exists",
  87. })
  88. return
  89. }
  90. err := os.Rename(json.Filepath, json.NewFilepath)
  91. if err != nil {
  92. api.ErrHandler(c, err)
  93. return
  94. }
  95. // update ChatGPT record
  96. _, _ = g.Where(g.Name.Eq(json.NewFilepath)).Delete()
  97. _, _ = g.Where(g.Name.Eq(path)).Update(g.Name, json.NewFilepath)
  98. }
  99. err = config.SyncToRemoteServer(cfg, json.NewFilepath)
  100. if err != nil {
  101. api.ErrHandler(c, err)
  102. return
  103. }
  104. output := nginx.Reload()
  105. if nginx.GetLogLevel(output) >= nginx.Warn {
  106. c.JSON(http.StatusInternalServerError, gin.H{
  107. "message": output,
  108. })
  109. return
  110. }
  111. chatgpt, err := g.Where(g.Name.Eq(json.NewFilepath)).FirstOrCreate()
  112. if err != nil {
  113. api.ErrHandler(c, err)
  114. return
  115. }
  116. if chatgpt.Content == nil {
  117. chatgpt.Content = make([]openai.ChatCompletionMessage, 0)
  118. }
  119. c.JSON(http.StatusOK, config.Config{
  120. Name: name,
  121. Content: content,
  122. ChatGPTMessages: chatgpt.Content,
  123. FilePath: json.NewFilepath,
  124. ModifiedAt: time.Now(),
  125. })
  126. }