rename.go 2.5 KB

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