From 68aaa8fee2415cbc258f568c607c0b01fb692a15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=AE=E5=87=89?= <927625802@qq.com> Date: Mon, 3 Jan 2022 23:30:57 +0800 Subject: [PATCH] :construction: Pagination function --- server/common/common.go | 2 ++ server/controllers/path.go | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/server/common/common.go b/server/common/common.go index d8400c4d..d264d94b 100644 --- a/server/common/common.go +++ b/server/common/common.go @@ -19,6 +19,8 @@ type Resp struct { type PathReq struct { Path string `json:"path"` Password string `json:"password"` + PageNum int `json:"page_num"` + PageSize int `json:"page_size"` } func ParsePath(rawPath string) (*model.Account, string, base.Driver, error) { diff --git a/server/controllers/path.go b/server/controllers/path.go index d9df18b5..bf7a8ce8 100644 --- a/server/controllers/path.go +++ b/server/controllers/path.go @@ -1,6 +1,7 @@ package controllers import ( + "errors" "fmt" "github.com/Xhofe/alist/conf" "github.com/Xhofe/alist/drivers/base" @@ -12,7 +13,7 @@ import ( "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) if meta != nil && meta.Hide != "" { tmpFiles := make([]model.File, 0) @@ -27,9 +28,31 @@ func Hide(meta *model.Meta, files []model.File, path string) []model.File { 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 { Driver string `json:"driver"` Upload bool `json:"upload"` + Total int `json:"total"` + //Pages int `json:"pages"` } type PathResp struct { @@ -52,7 +75,7 @@ func Path(c *gin.Context) { common.ErrorResp(c, err, 500) return } - files = Hide(meta, files, req.Path) + files = Hide(meta, files) c.JSON(200, common.Resp{ Code: 200, Message: "success", @@ -66,6 +89,11 @@ func Path(c *gin.Context) { }) return } + err := CheckPagination(req) + if err != nil { + common.ErrorResp(c, err, 400) + return + } account, path, driver, err := common.ParsePath(req.Path) if err != nil { common.ErrorResp(c, err, 500) @@ -104,10 +132,11 @@ func Path(c *gin.Context) { }, }) } else { - files = Hide(meta, files, req.Path) + files = Hide(meta, files) if driver.Config().LocalSort { model.SortFiles(files, account) } + total, files := Pagination(files, req.PageNum, req.PageSize) c.JSON(200, common.Resp{ Code: 200, Message: "success", @@ -116,6 +145,7 @@ func Path(c *gin.Context) { Meta: Meta{ Driver: driver.Config().Name, Upload: upload, + Total: total, }, Files: files, },