rename.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 := helper.UnescapeURL(json.BasePath)
  34. decodedOrigName := helper.UnescapeURL(json.OrigName)
  35. decodedNewName, err := url.QueryUnescape(json.NewName)
  36. if err != nil {
  37. cosy.ErrHandler(c, err)
  38. return
  39. }
  40. origFullPath := nginx.GetConfPath(decodedBasePath, decodedOrigName)
  41. newFullPath := nginx.GetConfPath(decodedBasePath, decodedNewName)
  42. if !helper.IsUnderDirectory(origFullPath, nginx.GetConfPath()) ||
  43. !helper.IsUnderDirectory(newFullPath, nginx.GetConfPath()) {
  44. c.JSON(http.StatusForbidden, gin.H{
  45. "message": "you are not allowed to rename a file " +
  46. "outside of the nginx config path",
  47. })
  48. return
  49. }
  50. stat, err := os.Stat(origFullPath)
  51. if err != nil {
  52. cosy.ErrHandler(c, err)
  53. return
  54. }
  55. if helper.FileExists(newFullPath) {
  56. c.JSON(http.StatusNotAcceptable, gin.H{
  57. "message": "target file already exists",
  58. })
  59. return
  60. }
  61. err = os.Rename(origFullPath, newFullPath)
  62. if err != nil {
  63. cosy.ErrHandler(c, err)
  64. return
  65. }
  66. // update ChatGPT records
  67. g := query.ChatGPTLog
  68. q := query.Config
  69. cfg, err := q.Where(q.Filepath.Eq(origFullPath)).FirstOrInit()
  70. if err != nil {
  71. cosy.ErrHandler(c, err)
  72. return
  73. }
  74. if !stat.IsDir() {
  75. _, _ = g.Where(g.Name.Eq(newFullPath)).Delete()
  76. _, _ = g.Where(g.Name.Eq(origFullPath)).Update(g.Name, newFullPath)
  77. // for file, the sync policy for this file is used
  78. json.SyncNodeIds = cfg.SyncNodeIds
  79. } else {
  80. // is directory, update all records under the directory
  81. _, _ = g.Where(g.Name.Like(origFullPath+"%")).Update(g.Name, g.Name.Replace(origFullPath, newFullPath))
  82. }
  83. _, err = q.Where(q.Filepath.Eq(origFullPath)).Updates(&model.Config{
  84. Filepath: newFullPath,
  85. Name: json.NewName,
  86. })
  87. if err != nil {
  88. cosy.ErrHandler(c, err)
  89. return
  90. }
  91. b := query.ConfigBackup
  92. _, _ = b.Where(b.FilePath.Eq(origFullPath)).Updates(map[string]interface{}{
  93. "filepath": newFullPath,
  94. "name": json.NewName,
  95. })
  96. if len(json.SyncNodeIds) > 0 {
  97. err = config.SyncRenameOnRemoteServer(origFullPath, newFullPath, json.SyncNodeIds)
  98. if err != nil {
  99. cosy.ErrHandler(c, err)
  100. return
  101. }
  102. }
  103. c.JSON(http.StatusOK, gin.H{
  104. "path": strings.TrimLeft(filepath.Join(json.BasePath, json.NewName), "/"),
  105. })
  106. }