alist/internal/model/obj.go

103 lines
1.7 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"
2022-06-15 12:41:17 +00:00
"time"
"github.com/maruel/natural"
2022-06-15 12:41:17 +00:00
)
type UnwrapObj interface {
Unwrap() Obj
}
2022-06-15 12:41:17 +00:00
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
// The internal information of the driver.
// If you want to use it, please understand what it means
2022-06-15 12:41:17 +00:00
GetID() string
2022-09-02 14:46:31 +00:00
GetPath() string
2022-06-15 12:41:17 +00:00
}
type FileStreamer interface {
io.ReadCloser
Obj
GetMimetype() string
2022-07-01 07:04:02 +00:00
SetReadCloser(io.ReadCloser)
NeedStore() bool
GetReadCloser() io.ReadCloser
GetOld() Obj
2022-06-15 12:41:17 +00:00
}
type URL interface {
URL() string
}
2022-08-11 12:32:17 +00:00
type Thumb interface {
Thumb() string
2022-06-15 12:41:17 +00:00
}
2022-06-16 12:25:33 +00:00
2022-09-02 14:46:31 +00:00
type SetPath interface {
SetPath(path string)
2022-06-16 12:25:33 +00:00
}
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 := natural.Less(objs[i].GetName(), objs[j].GetName())
2022-07-27 07:49:18 +00:00
if orderDirection == "desc" {
return !c
2022-06-27 11:10:02 +00:00
}
return c
2022-06-27 11:10:02 +00:00
}
case "size":
{
2022-07-27 07:49:18 +00:00
if orderDirection == "desc" {
2022-06-27 11:10:02 +00:00
return objs[i].GetSize() >= objs[j].GetSize()
}
return objs[i].GetSize() <= objs[j].GetSize()
}
case "modified":
2022-07-27 07:49:18 +00:00
if orderDirection == "desc" {
2022-06-27 11:10:02 +00:00
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
})
}
func WrapObjsName(objs []Obj) {
for i := 0; i < len(objs); i++ {
objs[i] = &ObjWrapName{Obj: objs[i]}
}
}