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