rename.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package config
  2. import (
  3. "net/http"
  4. "net/url"
  5. "os"
  6. "path/filepath"
  7. "strings"
  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/uozi-tech/cosy"
  15. )
  16. func Rename(c *gin.Context) {
  17. var json struct {
  18. BasePath string `json:"base_path"`
  19. OrigName string `json:"orig_name"`
  20. NewName string `json:"new_name"`
  21. SyncNodeIds []uint64 `json:"sync_node_ids" gorm:"serializer:json"`
  22. }
  23. if !cosy.BindAndValid(c, &json) {
  24. return
  25. }
  26. if json.OrigName == json.NewName {
  27. c.JSON(http.StatusOK, gin.H{
  28. "message": "ok",
  29. })
  30. return
  31. }
  32. // Decode paths from URL encoding
  33. decodedBasePath, err := url.QueryUnescape(json.BasePath)
  34. if err != nil {
  35. cosy.ErrHandler(c, err)
  36. return
  37. }
  38. decodedOrigName, err := url.QueryUnescape(json.OrigName)
  39. if err != nil {
  40. cosy.ErrHandler(c, err)
  41. return
  42. }
  43. decodedNewName, err := url.QueryUnescape(json.NewName)
  44. if err != nil {
  45. cosy.ErrHandler(c, err)
  46. return
  47. }
  48. origFullPath := nginx.GetConfPath(decodedBasePath, decodedOrigName)
  49. newFullPath := nginx.GetConfPath(decodedBasePath, decodedNewName)
  50. if !helper.IsUnderDirectory(origFullPath, nginx.GetConfPath()) ||
  51. !helper.IsUnderDirectory(newFullPath, nginx.GetConfPath()) {
  52. c.JSON(http.StatusForbidden, gin.H{
  53. "message": "you are not allowed to rename a file " +
  54. "outside of the nginx config path",
  55. })
  56. return
  57. }
  58. stat, err := os.Stat(origFullPath)
  59. if err != nil {
  60. cosy.ErrHandler(c, err)
  61. return
  62. }
  63. if helper.FileExists(newFullPath) {
  64. c.JSON(http.StatusNotAcceptable, gin.H{
  65. "message": "target file already exists",
  66. })
  67. return
  68. }
  69. err = os.Rename(origFullPath, newFullPath)
  70. if err != nil {
  71. cosy.ErrHandler(c, err)
  72. return
  73. }
  74. // update ChatGPT records
  75. g := query.ChatGPTLog
  76. q := query.Config
  77. cfg, err := q.Where(q.Filepath.Eq(origFullPath)).FirstOrInit()
  78. if err != nil {
  79. cosy.ErrHandler(c, err)
  80. return
  81. }
  82. if !stat.IsDir() {
  83. _, _ = g.Where(g.Name.Eq(newFullPath)).Delete()
  84. _, _ = g.Where(g.Name.Eq(origFullPath)).Update(g.Name, newFullPath)
  85. // for file, the sync policy for this file is used
  86. json.SyncNodeIds = cfg.SyncNodeIds
  87. } else {
  88. // is directory, update all records under the directory
  89. _, _ = g.Where(g.Name.Like(origFullPath+"%")).Update(g.Name, g.Name.Replace(origFullPath, newFullPath))
  90. }
  91. _, err = q.Where(q.Filepath.Eq(origFullPath)).Updates(&model.Config{
  92. Filepath: newFullPath,
  93. Name: json.NewName,
  94. })
  95. if err != nil {
  96. cosy.ErrHandler(c, err)
  97. return
  98. }
  99. if len(json.SyncNodeIds) > 0 {
  100. err = config.SyncRenameOnRemoteServer(origFullPath, newFullPath, json.SyncNodeIds)
  101. if err != nil {
  102. cosy.ErrHandler(c, err)
  103. return
  104. }
  105. }
  106. c.JSON(http.StatusOK, gin.H{
  107. "path": strings.TrimLeft(filepath.Join(json.BasePath, json.NewName), "/"),
  108. })
  109. }