rename.go 2.5 KB

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