modify.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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)).Updates(&model.Config{
  70. Name: json.Name,
  71. Filepath: json.NewFilepath,
  72. SyncNodeIds: json.SyncNodeIds,
  73. SyncOverwrite: json.SyncOverwrite,
  74. })
  75. if err != nil {
  76. api.ErrHandler(c, err)
  77. return
  78. }
  79. g := query.ChatGPTLog
  80. // handle rename
  81. if path != json.NewFilepath {
  82. if helper.FileExists(json.NewFilepath) {
  83. c.JSON(http.StatusNotAcceptable, gin.H{
  84. "message": "File exists",
  85. })
  86. return
  87. }
  88. err := os.Rename(json.Filepath, json.NewFilepath)
  89. if err != nil {
  90. api.ErrHandler(c, err)
  91. return
  92. }
  93. // update ChatGPT record
  94. _, _ = g.Where(g.Name.Eq(json.NewFilepath)).Delete()
  95. _, _ = g.Where(g.Name.Eq(path)).Update(g.Name, json.NewFilepath)
  96. }
  97. err = config.SyncToRemoteServer(cfg, json.NewFilepath)
  98. if err != nil {
  99. api.ErrHandler(c, err)
  100. return
  101. }
  102. output := nginx.Reload()
  103. if nginx.GetLogLevel(output) >= nginx.Warn {
  104. c.JSON(http.StatusInternalServerError, gin.H{
  105. "message": output,
  106. })
  107. return
  108. }
  109. chatgpt, err := g.Where(g.Name.Eq(json.NewFilepath)).FirstOrCreate()
  110. if err != nil {
  111. api.ErrHandler(c, err)
  112. return
  113. }
  114. if chatgpt.Content == nil {
  115. chatgpt.Content = make([]openai.ChatCompletionMessage, 0)
  116. }
  117. c.JSON(http.StatusOK, config.Config{
  118. Name: name,
  119. Content: content,
  120. ChatGPTMessages: chatgpt.Content,
  121. FilePath: json.NewFilepath,
  122. ModifiedAt: time.Now(),
  123. })
  124. }