mirror of
https://github.com/onepanelio/onepanel.git
synced 2025-10-11 16:40:03 +08:00
feat: added pagination to listing files
This commit is contained in:
@@ -14,6 +14,7 @@ import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"google.golang.org/grpc/codes"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"math"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -475,6 +476,13 @@ func (s *WorkflowServer) ListFiles(ctx context.Context, req *api.ListFilesReques
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if req.Page < 0 {
|
||||
req.Page = 1
|
||||
}
|
||||
if req.PerPage <= 0 {
|
||||
req.PerPage = 15
|
||||
}
|
||||
|
||||
files, err := client.ListFiles(req.Namespace, req.Path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -493,22 +501,43 @@ func (s *WorkflowServer) ListFiles(ctx context.Context, req *api.ListFilesReques
|
||||
}
|
||||
}
|
||||
|
||||
sort.Slice(apiFiles, func(i, j int) bool {
|
||||
fileI := apiFiles[i]
|
||||
fileJ := apiFiles[j]
|
||||
sort.SliceStable(apiFiles, func(i, j int) bool {
|
||||
lhFile := apiFiles[i]
|
||||
rhFile := apiFiles[j]
|
||||
|
||||
if fileI.Directory && !fileJ.Directory {
|
||||
if (lhFile.Directory && rhFile.Directory) ||
|
||||
(!lhFile.Directory && !rhFile.Directory) {
|
||||
return strings.Compare(strings.ToLower(lhFile.Name), strings.ToLower(rhFile.Name)) < 0
|
||||
}
|
||||
|
||||
if lhFile.Directory {
|
||||
return true
|
||||
}
|
||||
|
||||
return strings.Compare(fileI.Path, fileJ.Path) < 0
|
||||
return false
|
||||
})
|
||||
|
||||
parentPath := v1.FilePathToParentPath(req.Path)
|
||||
start := (req.Page - 1) * req.PerPage
|
||||
if start < 0 {
|
||||
start = 0
|
||||
}
|
||||
|
||||
end := int(start + req.PerPage)
|
||||
if end > len(apiFiles) {
|
||||
end = len(apiFiles)
|
||||
}
|
||||
parts := apiFiles[start:end]
|
||||
|
||||
count := len(parts)
|
||||
totalCount := len(apiFiles)
|
||||
|
||||
return &api.ListFilesResponse{
|
||||
Files: apiFiles,
|
||||
ParentPath: parentPath,
|
||||
Count: int32(count),
|
||||
Page: req.Page,
|
||||
Pages: int32(math.Ceil(float64(totalCount) / float64(req.PerPage))),
|
||||
TotalCount: int32(totalCount),
|
||||
Files: parts,
|
||||
ParentPath: v1.FilePathToParentPath(req.Path),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user