feat: sort obj list

This commit is contained in:
Noah Hsu
2022-06-27 19:10:02 +08:00
parent f01a81ee9c
commit c6007aa9e6
4 changed files with 122 additions and 13 deletions

View File

@ -2,6 +2,8 @@ package model
import (
"io"
"sort"
"strings"
"time"
)
@ -30,3 +32,52 @@ type Thumbnail interface {
type SetID interface {
SetID(id string)
}
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
})
}

View File

@ -12,13 +12,15 @@ const (
)
type User struct {
ID uint `json:"id" gorm:"primaryKey"` // unique key
Username string `json:"username" gorm:"unique" binding:"required"` // username
Password string `json:"password"` // password
BasePath string `json:"base_path"` // base path
ReadOnly bool `json:"read_only"` // read only
Webdav bool `json:"webdav"` // allow webdav
Role int `json:"role"` // user's role
ID uint `json:"id" gorm:"primaryKey"` // unique key
Username string `json:"username" gorm:"unique" binding:"required"` // username
Password string `json:"password"` // password
BasePath string `json:"base_path"` // base path
ReadOnly bool `json:"read_only"` // read only
Webdav bool `json:"webdav"` // allow webdav
Role int `json:"role"` // user's role
IgnoreHide bool `json:"can_hide"` // can see hide files
IgnorePassword bool `json:"ignore_password"` // can access without password
}
func (u User) IsGuest() bool {