rename.go 2.6 KB

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