forked from AlistGo/alist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
90 lines (81 loc) · 1.76 KB
/
file.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
package model
import (
"github.com/Xhofe/alist/conf"
"sort"
"strings"
"time"
)
type File struct {
Id string `json:"-"`
Name string `json:"name"`
Size int64 `json:"size"`
Type int `json:"type"`
Driver string `json:"driver"`
UpdatedAt *time.Time `json:"updated_at"`
Thumbnail string `json:"thumbnail"`
Url string `json:"url"`
SizeStr string `json:"size_str"`
TimeStr string `json:"time_str"`
}
func SortFiles(files []File, account *Account) {
if account.OrderBy == "" {
return
}
sort.Slice(files, func(i, j int) bool {
switch account.OrderBy {
case "name":
{
c := strings.Compare(files[i].Name, files[j].Name)
if account.OrderDirection == "DESC" {
return c >= 0
}
return c <= 0
}
case "size":
{
if account.OrderDirection == "DESC" {
return files[i].Size >= files[j].Size
}
return files[i].Size <= files[j].Size
}
case "updated_at":
if account.OrderDirection == "DESC" {
return files[i].UpdatedAt.After(*files[j].UpdatedAt)
}
return files[i].UpdatedAt.Before(*files[j].UpdatedAt)
}
return false
})
}
func ExtractFolder(files []File, account *Account) {
if account.ExtractFolder == "" {
return
}
front := account.ExtractFolder == "front"
sort.SliceStable(files, func(i, j int) bool {
if files[i].IsDir() || files[j].IsDir() {
if !files[i].IsDir() {
return !front
}
if !files[j].IsDir() {
return front
}
}
return false
})
}
func (f File) GetSize() uint64 {
return uint64(f.Size)
}
func (f File) GetName() string {
return f.Name
}
func (f File) ModTime() time.Time {
return *f.UpdatedAt
}
func (f File) IsDir() bool {
return f.Type == conf.FOLDER
}
func (f File) GetType() int {
return f.Type
}