mirror of https://github.com/Xhofe/alist
🚧 Pagination function
parent
67bf14d428
commit
68aaa8fee2
|
@ -19,6 +19,8 @@ type Resp struct {
|
||||||
type PathReq struct {
|
type PathReq struct {
|
||||||
Path string `json:"path"`
|
Path string `json:"path"`
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
|
PageNum int `json:"page_num"`
|
||||||
|
PageSize int `json:"page_size"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParsePath(rawPath string) (*model.Account, string, base.Driver, error) {
|
func ParsePath(rawPath string) (*model.Account, string, base.Driver, error) {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/Xhofe/alist/conf"
|
"github.com/Xhofe/alist/conf"
|
||||||
"github.com/Xhofe/alist/drivers/base"
|
"github.com/Xhofe/alist/drivers/base"
|
||||||
|
@ -12,7 +13,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Hide(meta *model.Meta, files []model.File, path string) []model.File {
|
func Hide(meta *model.Meta, files []model.File) []model.File {
|
||||||
//meta, _ := model.GetMetaByPath(path)
|
//meta, _ := model.GetMetaByPath(path)
|
||||||
if meta != nil && meta.Hide != "" {
|
if meta != nil && meta.Hide != "" {
|
||||||
tmpFiles := make([]model.File, 0)
|
tmpFiles := make([]model.File, 0)
|
||||||
|
@ -27,9 +28,31 @@ func Hide(meta *model.Meta, files []model.File, path string) []model.File {
|
||||||
return files
|
return files
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Pagination(files []model.File, pageNum, pageSize int) (int, []model.File) {
|
||||||
|
total := len(files)
|
||||||
|
start := (pageNum - 1) * pageSize
|
||||||
|
if start > total {
|
||||||
|
return total, []model.File{}
|
||||||
|
}
|
||||||
|
end := start + pageSize
|
||||||
|
if end > total {
|
||||||
|
end = total
|
||||||
|
}
|
||||||
|
return total, files[start:end]
|
||||||
|
}
|
||||||
|
|
||||||
|
func CheckPagination(req common.PathReq) error {
|
||||||
|
if req.PageNum < 1 {
|
||||||
|
return errors.New("page_num can't be less than 1")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
type Meta struct {
|
type Meta struct {
|
||||||
Driver string `json:"driver"`
|
Driver string `json:"driver"`
|
||||||
Upload bool `json:"upload"`
|
Upload bool `json:"upload"`
|
||||||
|
Total int `json:"total"`
|
||||||
|
//Pages int `json:"pages"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type PathResp struct {
|
type PathResp struct {
|
||||||
|
@ -52,7 +75,7 @@ func Path(c *gin.Context) {
|
||||||
common.ErrorResp(c, err, 500)
|
common.ErrorResp(c, err, 500)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
files = Hide(meta, files, req.Path)
|
files = Hide(meta, files)
|
||||||
c.JSON(200, common.Resp{
|
c.JSON(200, common.Resp{
|
||||||
Code: 200,
|
Code: 200,
|
||||||
Message: "success",
|
Message: "success",
|
||||||
|
@ -66,6 +89,11 @@ func Path(c *gin.Context) {
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
err := CheckPagination(req)
|
||||||
|
if err != nil {
|
||||||
|
common.ErrorResp(c, err, 400)
|
||||||
|
return
|
||||||
|
}
|
||||||
account, path, driver, err := common.ParsePath(req.Path)
|
account, path, driver, err := common.ParsePath(req.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
common.ErrorResp(c, err, 500)
|
common.ErrorResp(c, err, 500)
|
||||||
|
@ -104,10 +132,11 @@ func Path(c *gin.Context) {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
files = Hide(meta, files, req.Path)
|
files = Hide(meta, files)
|
||||||
if driver.Config().LocalSort {
|
if driver.Config().LocalSort {
|
||||||
model.SortFiles(files, account)
|
model.SortFiles(files, account)
|
||||||
}
|
}
|
||||||
|
total, files := Pagination(files, req.PageNum, req.PageSize)
|
||||||
c.JSON(200, common.Resp{
|
c.JSON(200, common.Resp{
|
||||||
Code: 200,
|
Code: 200,
|
||||||
Message: "success",
|
Message: "success",
|
||||||
|
@ -116,6 +145,7 @@ func Path(c *gin.Context) {
|
||||||
Meta: Meta{
|
Meta: Meta{
|
||||||
Driver: driver.Config().Name,
|
Driver: driver.Config().Name,
|
||||||
Upload: upload,
|
Upload: upload,
|
||||||
|
Total: total,
|
||||||
},
|
},
|
||||||
Files: files,
|
Files: files,
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue