rename.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package stream
  2. import (
  3. "fmt"
  4. "net/http"
  5. "os"
  6. "runtime"
  7. "sync"
  8. "github.com/0xJacky/Nginx-UI/internal/helper"
  9. "github.com/0xJacky/Nginx-UI/internal/nginx"
  10. "github.com/0xJacky/Nginx-UI/internal/notification"
  11. "github.com/0xJacky/Nginx-UI/query"
  12. "github.com/go-resty/resty/v2"
  13. "github.com/uozi-tech/cosy"
  14. "github.com/uozi-tech/cosy/logger"
  15. )
  16. func Rename(oldName string, newName string) (err error) {
  17. oldPath := nginx.GetConfPath("streams-available", oldName)
  18. newPath := nginx.GetConfPath("streams-available", newName)
  19. if oldPath == newPath {
  20. return
  21. }
  22. // check if dst file exists, do not rename
  23. if helper.FileExists(newPath) {
  24. return ErrDstFileExists
  25. }
  26. s := query.Site
  27. _, _ = s.Where(s.Path.Eq(oldPath)).Update(s.Path, newPath)
  28. err = os.Rename(oldPath, newPath)
  29. if err != nil {
  30. return
  31. }
  32. // recreate a soft link
  33. oldEnabledConfigFilePath := nginx.GetConfPath("streams-enabled", oldName)
  34. if helper.SymbolLinkExists(oldEnabledConfigFilePath) {
  35. _ = os.Remove(oldEnabledConfigFilePath)
  36. newEnabledConfigFilePath := nginx.GetConfPath("streams-enabled", newName)
  37. err = os.Symlink(newPath, newEnabledConfigFilePath)
  38. if err != nil {
  39. return
  40. }
  41. }
  42. // test nginx configuration
  43. output := nginx.TestConf()
  44. if nginx.GetLogLevel(output) > nginx.Warn {
  45. return cosy.WrapErrorWithParams(ErrNginxTestFailed, output)
  46. }
  47. // reload nginx
  48. output = nginx.Reload()
  49. if nginx.GetLogLevel(output) > nginx.Warn {
  50. return cosy.WrapErrorWithParams(ErrNginxReloadFailed, output)
  51. }
  52. // update ChatGPT history
  53. g := query.ChatGPTLog
  54. _, _ = g.Where(g.Name.Eq(oldName)).Update(g.Name, newName)
  55. // update config history
  56. b := query.ConfigBackup
  57. _, _ = b.Where(b.FilePath.Eq(oldPath)).Updates(map[string]interface{}{
  58. "filepath": newPath,
  59. "name": newName,
  60. })
  61. go syncRename(oldName, newName)
  62. return
  63. }
  64. func syncRename(oldName, newName string) {
  65. nodes := getSyncNodes(newName)
  66. wg := &sync.WaitGroup{}
  67. wg.Add(len(nodes))
  68. for _, node := range nodes {
  69. go func() {
  70. defer func() {
  71. if err := recover(); err != nil {
  72. buf := make([]byte, 1024)
  73. runtime.Stack(buf, false)
  74. logger.Error(err)
  75. }
  76. }()
  77. defer wg.Done()
  78. client := resty.New()
  79. client.SetBaseURL(node.URL)
  80. resp, err := client.R().
  81. SetHeader("X-Node-Secret", node.Token).
  82. SetBody(map[string]string{
  83. "new_name": newName,
  84. }).
  85. Post(fmt.Sprintf("/api/streams/%s/rename", oldName))
  86. if err != nil {
  87. notification.Error("Rename Remote Stream Error", err.Error(), nil)
  88. return
  89. }
  90. if resp.StatusCode() != http.StatusOK {
  91. notification.Error("Rename Remote Stream Error", "Rename stream %{name} to %{new_name} on %{node} failed",
  92. NewSyncResult(node.Name, oldName, resp).
  93. SetNewName(newName))
  94. return
  95. }
  96. notification.Success("Rename Remote Stream Success", "Rename stream %{name} to %{new_name} on %{node} successfully",
  97. NewSyncResult(node.Name, oldName, resp).
  98. SetNewName(newName))
  99. }()
  100. }
  101. wg.Wait()
  102. }