modify.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/query"
  8. "github.com/gin-gonic/gin"
  9. "github.com/sashabaranov/go-openai"
  10. "net/http"
  11. "os"
  12. "time"
  13. )
  14. type EditConfigJson struct {
  15. Content string `json:"content" binding:"required"`
  16. }
  17. func EditConfig(c *gin.Context) {
  18. name := c.Param("name")
  19. var json struct {
  20. Name string `json:"name" binding:"required"`
  21. Filepath string `json:"filepath" binding:"required"`
  22. NewFilepath string `json:"new_filepath" binding:"required"`
  23. Content string `json:"content"`
  24. }
  25. if !api.BindAndValid(c, &json) {
  26. return
  27. }
  28. path := json.Filepath
  29. if !helper.IsUnderDirectory(path, nginx.GetConfPath()) {
  30. c.JSON(http.StatusForbidden, gin.H{
  31. "message": "filepath is not under the nginx conf path",
  32. })
  33. return
  34. }
  35. if !helper.IsUnderDirectory(json.NewFilepath, nginx.GetConfPath()) {
  36. c.JSON(http.StatusForbidden, gin.H{
  37. "message": "new filepath is not under the nginx conf path",
  38. })
  39. return
  40. }
  41. if _, err := os.Stat(path); os.IsNotExist(err) {
  42. c.JSON(http.StatusNotFound, gin.H{
  43. "message": "file not found",
  44. })
  45. return
  46. }
  47. content := json.Content
  48. origContent, err := os.ReadFile(path)
  49. if err != nil {
  50. api.ErrHandler(c, err)
  51. return
  52. }
  53. if content != "" && content != string(origContent) {
  54. err = os.WriteFile(path, []byte(content), 0644)
  55. if err != nil {
  56. api.ErrHandler(c, err)
  57. return
  58. }
  59. }
  60. g := query.ChatGPTLog
  61. // handle rename
  62. if path != json.NewFilepath {
  63. if helper.FileExists(json.NewFilepath) {
  64. c.JSON(http.StatusNotAcceptable, gin.H{
  65. "message": "File exists",
  66. })
  67. return
  68. }
  69. err := os.Rename(json.Filepath, json.NewFilepath)
  70. if err != nil {
  71. api.ErrHandler(c, err)
  72. return
  73. }
  74. // update ChatGPT record
  75. _, _ = g.Where(g.Name.Eq(json.NewFilepath)).Delete()
  76. _, _ = g.Where(g.Name.Eq(path)).Update(g.Name, json.NewFilepath)
  77. }
  78. output := nginx.Reload()
  79. if nginx.GetLogLevel(output) >= nginx.Warn {
  80. c.JSON(http.StatusInternalServerError, gin.H{
  81. "message": output,
  82. })
  83. return
  84. }
  85. chatgpt, err := g.Where(g.Name.Eq(json.NewFilepath)).FirstOrCreate()
  86. if err != nil {
  87. api.ErrHandler(c, err)
  88. return
  89. }
  90. if chatgpt.Content == nil {
  91. chatgpt.Content = make([]openai.ChatCompletionMessage, 0)
  92. }
  93. c.JSON(http.StatusOK, config.Config{
  94. Name: name,
  95. Content: content,
  96. ChatGPTMessages: chatgpt.Content,
  97. FilePath: json.NewFilepath,
  98. ModifiedAt: time.Now(),
  99. })
  100. }