sort_things.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package sort_things
  2. import (
  3. "os"
  4. "sort"
  5. "time"
  6. "github.com/ChineseSubFinder/ChineseSubFinder/pkg/decode"
  7. )
  8. type PathSlice struct {
  9. Path string
  10. }
  11. type PathSlices []PathSlice
  12. func (a PathSlices) Len() int { return len(a) }
  13. func (a PathSlices) Less(i, j int) bool { return len(a[i].Path) < len(a[j].Path) }
  14. func (a PathSlices) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  15. // SortStringSliceByLength 排序得到匹配上的路径,最长的那个
  16. func SortStringSliceByLength(m []string) PathSlices {
  17. p := make(PathSlices, len(m))
  18. i := 0
  19. for _, v := range m {
  20. p[i] = PathSlice{v}
  21. i++
  22. }
  23. sort.Sort(sort.Reverse(p))
  24. return p
  25. }
  26. // -----------------------------------------------------------------------------
  27. // SortByModTime 根据文件的 Mod Time 进行排序,递减
  28. func SortByModTime(fileList []string) []string {
  29. byModTime := make(ByModTime, 0)
  30. byModTime = append(byModTime, fileList...)
  31. sort.Sort(sort.Reverse(byModTime))
  32. return byModTime
  33. }
  34. type ByModTime []string
  35. func (fis ByModTime) Len() int {
  36. return len(fis)
  37. }
  38. func (fis ByModTime) Swap(i, j int) {
  39. fis[i], fis[j] = fis[j], fis[i]
  40. }
  41. func (fis ByModTime) Less(i, j int) bool {
  42. aModTime := GetFileModTime(fis[i])
  43. bModTime := GetFileModTime(fis[j])
  44. return aModTime.Before(bModTime)
  45. }
  46. func GetFileModTime(fileFPath string) time.Time {
  47. if IsFile(fileFPath) == true {
  48. // 存在
  49. fi, err := os.Stat(fileFPath)
  50. if err != nil {
  51. return time.Time{}
  52. }
  53. return fi.ModTime()
  54. } else {
  55. // 不存在才需要考虑蓝光情况
  56. bok, idBDMVFPath, _ := decode.IsFakeBDMVWorked(fileFPath)
  57. if bok == false {
  58. // 也不是蓝光
  59. return time.Time{}
  60. }
  61. // 获取这个蓝光 ID BDMV 文件的时间
  62. fInfo, err := os.Stat(idBDMVFPath)
  63. if err != nil {
  64. return time.Time{}
  65. }
  66. return fInfo.ModTime()
  67. }
  68. }
  69. // -----------------------------------------------------------------------------
  70. // IsFile 存在且是文件
  71. func IsFile(filePath string) bool {
  72. s, err := os.Stat(filePath)
  73. if err != nil {
  74. return false
  75. }
  76. return !s.IsDir()
  77. }