alist/internal/model/obj.go

84 lines
1.4 KiB
Go
Raw Normal View History

2022-06-15 12:41:17 +00:00
package model
import (
"io"
2022-06-27 11:10:02 +00:00
"sort"
"strings"
2022-06-15 12:41:17 +00:00
"time"
)
type Obj interface {
2022-06-23 07:57:36 +00:00
GetSize() int64
2022-06-15 12:41:17 +00:00
GetName() string
ModTime() time.Time
IsDir() bool
GetID() string
}
type FileStreamer interface {
io.ReadCloser
Obj
GetMimetype() string
}
type URL interface {
URL() string
}
type Thumbnail interface {
Thumbnail() string
}
2022-06-16 12:25:33 +00:00
type SetID interface {
SetID(id string)
}
2022-06-27 11:10:02 +00:00
func SortFiles(objs []Obj, orderBy, orderDirection string) {
if orderBy == "" {
return
}
sort.Slice(objs, func(i, j int) bool {
switch orderBy {
case "name":
{
c := strings.Compare(objs[i].GetName(), objs[j].GetName())
if orderDirection == "DESC" {
return c >= 0
}
return c <= 0
}
case "size":
{
if orderDirection == "DESC" {
return objs[i].GetSize() >= objs[j].GetSize()
}
return objs[i].GetSize() <= objs[j].GetSize()
}
case "updated_at":
if orderDirection == "DESC" {
return objs[i].ModTime().After(objs[j].ModTime())
}
return objs[i].ModTime().Before(objs[j].ModTime())
}
return false
})
}
func ExtractFolder(objs []Obj, extractFolder string) {
if extractFolder == "" {
return
}
front := extractFolder == "front"
sort.SliceStable(objs, func(i, j int) bool {
if objs[i].IsDir() || objs[j].IsDir() {
if !objs[i].IsDir() {
return !front
}
if !objs[j].IsDir() {
return front
}
}
return false
})
}