mirror of https://github.com/Xhofe/alist
✨ resolved #170 native driver sort
parent
163ee1159e
commit
8cfabfd0f5
|
@ -28,6 +28,20 @@ func (n Native) Items() []Item {
|
|||
Type: "string",
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Name: "order_by",
|
||||
Label: "order_by",
|
||||
Type: "select",
|
||||
Values: "name,size,updated_at",
|
||||
Required: false,
|
||||
},
|
||||
{
|
||||
Name: "order_direction",
|
||||
Label: "order_direction",
|
||||
Type: "select",
|
||||
Values: "ASC,DESC",
|
||||
Required: false,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -51,7 +65,6 @@ func (n Native) Save(account *model.Account, old *model.Account) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// TODO sort files
|
||||
func (n Native) Path(path string, account *model.Account) (*model.File, []model.File, error) {
|
||||
fullPath := filepath.Join(account.RootFolder, path)
|
||||
log.Debugf("%s-%s-%s", account.RootFolder, path, fullPath)
|
||||
|
@ -83,6 +96,7 @@ func (n Native) Path(path string, account *model.Account) (*model.File, []model.
|
|||
}
|
||||
result = append(result, file)
|
||||
}
|
||||
model.SortFiles(result, account)
|
||||
return nil, result, nil
|
||||
}
|
||||
f, err := os.Stat(fullPath)
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
package model
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type File struct {
|
||||
Name string `json:"name"`
|
||||
|
@ -11,3 +15,34 @@ type File struct {
|
|||
Thumbnail string `json:"thumbnail"`
|
||||
Url string `json:"url"`
|
||||
}
|
||||
|
||||
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
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue