rename.go 2.4 KB

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