delete.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package config
  2. import (
  3. "os"
  4. "strings"
  5. "github.com/0xJacky/Nginx-UI/internal/nginx"
  6. "github.com/0xJacky/Nginx-UI/query"
  7. )
  8. // CleanupDatabaseRecords removes related database records after deletion
  9. func CleanupDatabaseRecords(fullPath string, isDir bool) error {
  10. q := query.Config
  11. g := query.ChatGPTLog
  12. b := query.ConfigBackup
  13. if isDir {
  14. // For directories, clean up all records under the directory
  15. pathPattern := fullPath + "%"
  16. // Delete ChatGPT logs
  17. _, err := g.Where(g.Name.Like(pathPattern)).Delete()
  18. if err != nil {
  19. return err
  20. }
  21. // Delete config records
  22. _, err = q.Where(q.Filepath.Like(pathPattern)).Delete()
  23. if err != nil {
  24. return err
  25. }
  26. // Delete backup records
  27. _, err = b.Where(b.FilePath.Like(pathPattern)).Delete()
  28. if err != nil {
  29. return err
  30. }
  31. } else {
  32. // For files, delete specific records
  33. _, err := g.Where(g.Name.Eq(fullPath)).Delete()
  34. if err != nil {
  35. return err
  36. }
  37. _, err = q.Where(q.Filepath.Eq(fullPath)).Delete()
  38. if err != nil {
  39. return err
  40. }
  41. _, err = b.Where(b.FilePath.Eq(fullPath)).Delete()
  42. if err != nil {
  43. return err
  44. }
  45. }
  46. return nil
  47. }
  48. // IsProtectedPath checks if the path is protected and should not be deleted
  49. func IsProtectedPath(fullPath, name string) bool {
  50. // Get nginx main config file path
  51. nginxConfPath := nginx.GetConfEntryPath()
  52. if fullPath == nginxConfPath {
  53. return true
  54. }
  55. // Protected directory names
  56. protectedDirs := []string{
  57. "sites-enabled",
  58. "sites-available",
  59. "streams-enabled",
  60. "streams-available",
  61. "conf.d",
  62. }
  63. for _, protected := range protectedDirs {
  64. if name == protected || strings.HasSuffix(fullPath, "/"+protected) {
  65. return true
  66. }
  67. }
  68. return false
  69. }
  70. // ValidateDeletePath validates that the path is safe to delete
  71. func ValidateDeletePath(fullPath string) error {
  72. nginxConfPath := nginx.GetConfPath()
  73. if !IsUnderNginxConfDir(fullPath, nginxConfPath) {
  74. return ErrDeletePathNotUnderNginxConfDir
  75. }
  76. return nil
  77. }
  78. // IsUnderNginxConfDir checks if the given path is under nginx config directory
  79. func IsUnderNginxConfDir(path, nginxConfPath string) bool {
  80. // Normalize paths
  81. path = strings.TrimSuffix(path, "/")
  82. nginxConfPath = strings.TrimSuffix(nginxConfPath, "/")
  83. // Check if path starts with nginx config path
  84. return strings.HasPrefix(path, nginxConfPath)
  85. }
  86. // CheckFileExists checks if file or directory exists and returns file info
  87. func CheckFileExists(fullPath string) (os.FileInfo, error) {
  88. stat, err := os.Stat(fullPath)
  89. if err != nil {
  90. if os.IsNotExist(err) {
  91. return nil, ErrFileNotFound
  92. }
  93. return nil, err
  94. }
  95. return stat, nil
  96. }