file.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package core
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "os"
  7. "path/filepath"
  8. "sort"
  9. "strings"
  10. )
  11. //GetAllFilePathByExts 从指定路径获取指定后后缀的文件列表,ext:.go
  12. func GetAllFilePathByExts(currentPath string, exts []string) (filteredPaths []string, err error) {
  13. Paths, err := GetAllFilePath([]string{}, currentPath)
  14. if err != nil {
  15. XWarning(fmt.Sprintf("GetAllFilePathByExt error : %v\n", err))
  16. return
  17. }
  18. // 过滤是否包含指定后缀
  19. for _, path := range Paths {
  20. for _, ext := range exts {
  21. if strings.HasSuffix(path, ext) {
  22. filteredPaths = append(filteredPaths, path)
  23. }
  24. }
  25. }
  26. return
  27. }
  28. //GetAllFilePathByExt 从指定路径获取指定后后缀的文件列表,ext:.go
  29. func GetAllFilePathByExt(currentPath string, ext string) (filteredPaths []string, err error) {
  30. Paths, err := GetAllFilePath([]string{}, currentPath)
  31. if err != nil {
  32. XWarning(fmt.Sprintf("GetAllFilePathByExt error : %v\n", err))
  33. return
  34. }
  35. // 过滤是否包含指定后缀
  36. for _, path := range Paths {
  37. if strings.HasSuffix(path, ext) {
  38. filteredPaths = append(filteredPaths, path)
  39. }
  40. }
  41. return
  42. }
  43. // GetAllFilePath 获取指定目录内的文件和子目录的文件,递归获取
  44. func GetAllFilePath(filePaths []string, currentPath string) ([]string, error) {
  45. //2021-12-23
  46. files, err := ioutil.ReadDir(currentPath)
  47. if err != nil {
  48. log.Printf("filepath.Glob error : %v\n", err)
  49. return filePaths, err
  50. }
  51. for _, f := range files {
  52. if f.IsDir() {
  53. ret, err := GetAllFilePath(filePaths, filepath.Join(currentPath, f.Name()))
  54. if err != nil {
  55. log.Printf("GetAllFilePath eror: %v\n", err)
  56. return filePaths, err
  57. } else {
  58. filePaths = append(filePaths, ret...)
  59. }
  60. } else {
  61. filePaths = append(filePaths, filepath.Join(currentPath, f.Name()))
  62. }
  63. }
  64. return filePaths, nil
  65. }
  66. // FileExists 检测文件是否存在
  67. func FileExists(filePath string) (bool, error) {
  68. _, err := os.Stat(filePath)
  69. if err == nil {
  70. return true, nil
  71. }
  72. if os.IsNotExist(err) {
  73. return false, nil
  74. }
  75. return false, err
  76. }
  77. // GetFileFromFolder 获取子文件夹里的文件名列表
  78. func GetFileFromFolder(folder string) ([]string, error) {
  79. filePaths := []string{}
  80. files, err := ioutil.ReadDir(folder)
  81. if err != nil {
  82. log.Printf("filepath.Glob error : %v\n", err)
  83. return filePaths, err
  84. }
  85. for _, f := range files {
  86. filePaths = append(filePaths, filepath.Join(folder, f.Name()))
  87. }
  88. return filePaths, nil
  89. }
  90. // RemoveFiles 删除指定路径和指定后缀的文件
  91. func RemoveFiles(folder, suffix string) error {
  92. filePaths, err := GetFileFromFolder(folder)
  93. if err != nil {
  94. log.Printf("GetFileFromFolder error : %v\n", err)
  95. return err
  96. }
  97. for _, filePath := range filePaths {
  98. if filePath[len(filePath)-len(suffix):] == suffix {
  99. err = os.Remove(filePath)
  100. if err != nil {
  101. log.Printf("Remove error : %v\n", err)
  102. }
  103. }
  104. }
  105. return nil
  106. }
  107. // GetSubTitleByModTime 从指定路径获取字幕文件,按mod时间排序
  108. func GetSubTitleByModTime(folder string) (string, error) {
  109. //2021-11-03
  110. filePaths := []string{}
  111. files, err := ioutil.ReadDir(folder)
  112. if err != nil {
  113. log.Printf("filepath.Glob error : %v\n", err)
  114. return filePaths[0], err
  115. }
  116. //按mod时间排序
  117. sort.Slice(files, func(i, j int) bool {
  118. flag := false
  119. if files[i].ModTime().After(files[j].ModTime()) {
  120. flag = true
  121. } else if files[i].ModTime().Equal(files[j].ModTime()) {
  122. if files[i].Name() < files[j].Name() {
  123. flag = true
  124. }
  125. }
  126. return flag
  127. })
  128. for _, f := range files {
  129. filePaths = append(filePaths, filepath.Join(folder, f.Name()))
  130. }
  131. return filePaths[0], err
  132. }