utils.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package filemanager
  2. import (
  3. "io"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. )
  8. // isValidFilename checks if a given filename is safe and valid.
  9. func isValidFilename(filename string) bool {
  10. // Define a list of disallowed characters and reserved names
  11. disallowedChars := []string{"/", "\\", ":", "*", "?", "\"", "<", ">", "|"} // Add more if needed
  12. reservedNames := []string{"CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"} // Add more if needed
  13. // Check for disallowed characters
  14. for _, char := range disallowedChars {
  15. if strings.Contains(filename, char) {
  16. return false
  17. }
  18. }
  19. // Check for reserved names (case-insensitive)
  20. lowerFilename := strings.ToUpper(filename)
  21. for _, reserved := range reservedNames {
  22. if lowerFilename == reserved {
  23. return false
  24. }
  25. }
  26. // Check for empty filename
  27. if filename == "" {
  28. return false
  29. }
  30. // The filename is considered valid
  31. return true
  32. }
  33. // sanitizeFilename sanitizes a given filename by removing disallowed characters.
  34. func sanitizeFilename(filename string) string {
  35. disallowedChars := []string{"/", "\\", ":", "*", "?", "\"", "<", ">", "|"} // Add more if needed
  36. // Replace disallowed characters with underscores
  37. for _, char := range disallowedChars {
  38. filename = strings.ReplaceAll(filename, char, "_")
  39. }
  40. return filename
  41. }
  42. // copyFile copies a single file from source to destination
  43. func copyFile(srcPath, destPath string) error {
  44. srcFile, err := os.Open(srcPath)
  45. if err != nil {
  46. return err
  47. }
  48. defer srcFile.Close()
  49. destFile, err := os.Create(destPath)
  50. if err != nil {
  51. return err
  52. }
  53. defer destFile.Close()
  54. _, err = io.Copy(destFile, srcFile)
  55. if err != nil {
  56. return err
  57. }
  58. return nil
  59. }
  60. // copyDirectory recursively copies a directory and its contents from source to destination
  61. func copyDirectory(srcPath, destPath string) error {
  62. // Create the destination directory
  63. err := os.MkdirAll(destPath, os.ModePerm)
  64. if err != nil {
  65. return err
  66. }
  67. entries, err := os.ReadDir(srcPath)
  68. if err != nil {
  69. return err
  70. }
  71. for _, entry := range entries {
  72. srcEntryPath := filepath.Join(srcPath, entry.Name())
  73. destEntryPath := filepath.Join(destPath, entry.Name())
  74. if entry.IsDir() {
  75. err := copyDirectory(srcEntryPath, destEntryPath)
  76. if err != nil {
  77. return err
  78. }
  79. } else {
  80. err := copyFile(srcEntryPath, destEntryPath)
  81. if err != nil {
  82. return err
  83. }
  84. }
  85. }
  86. return nil
  87. }
  88. // isDir checks if the given path is a directory
  89. func isDir(path string) bool {
  90. fileInfo, err := os.Stat(path)
  91. if err != nil {
  92. return false
  93. }
  94. return fileInfo.IsDir()
  95. }
  96. // calculateDirectorySize calculates the total size of a directory and its contents
  97. func calculateDirectorySize(dirPath string) (int64, error) {
  98. var totalSize int64
  99. err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
  100. if err != nil {
  101. return err
  102. }
  103. totalSize += info.Size()
  104. return nil
  105. })
  106. if err != nil {
  107. return 0, err
  108. }
  109. return totalSize, nil
  110. }
  111. // countSubFilesAndFolders counts the number of sub-files and sub-folders within a directory
  112. func countSubFilesAndFolders(dirPath string) (int, int, error) {
  113. var numSubFiles, numSubFolders int
  114. err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
  115. if err != nil {
  116. return err
  117. }
  118. if info.IsDir() {
  119. numSubFolders++
  120. } else {
  121. numSubFiles++
  122. }
  123. return nil
  124. })
  125. if err != nil {
  126. return 0, 0, err
  127. }
  128. // Subtract 1 from numSubFolders to exclude the root directory itself
  129. return numSubFiles, numSubFolders - 1, nil
  130. }