mirror of
https://github.com/aler9/rtsp-simple-server
synced 2025-10-16 04:30:52 +08:00
api: fix crash when itemsPerPage is zero and there are items (#3425)
This commit is contained in:
@@ -10,7 +10,6 @@ import (
|
||||
"os"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -36,58 +35,6 @@ func interfaceIsEmpty(i interface{}) bool {
|
||||
return reflect.ValueOf(i).Kind() != reflect.Ptr || reflect.ValueOf(i).IsNil()
|
||||
}
|
||||
|
||||
func paginate2(itemsPtr interface{}, itemsPerPage int, page int) int {
|
||||
ritems := reflect.ValueOf(itemsPtr).Elem()
|
||||
|
||||
itemsLen := ritems.Len()
|
||||
if itemsLen == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
pageCount := (itemsLen / itemsPerPage)
|
||||
if (itemsLen % itemsPerPage) != 0 {
|
||||
pageCount++
|
||||
}
|
||||
|
||||
min := page * itemsPerPage
|
||||
if min > itemsLen {
|
||||
min = itemsLen
|
||||
}
|
||||
|
||||
max := (page + 1) * itemsPerPage
|
||||
if max > itemsLen {
|
||||
max = itemsLen
|
||||
}
|
||||
|
||||
ritems.Set(ritems.Slice(min, max))
|
||||
|
||||
return pageCount
|
||||
}
|
||||
|
||||
func paginate(itemsPtr interface{}, itemsPerPageStr string, pageStr string) (int, error) {
|
||||
itemsPerPage := 100
|
||||
|
||||
if itemsPerPageStr != "" {
|
||||
tmp, err := strconv.ParseUint(itemsPerPageStr, 10, 31)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
itemsPerPage = int(tmp)
|
||||
}
|
||||
|
||||
page := 0
|
||||
|
||||
if pageStr != "" {
|
||||
tmp, err := strconv.ParseUint(pageStr, 10, 31)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
page = int(tmp)
|
||||
}
|
||||
|
||||
return paginate2(itemsPtr, itemsPerPage, page), nil
|
||||
}
|
||||
|
||||
func sortedKeys(paths map[string]*conf.Path) []string {
|
||||
ret := make([]string, len(paths))
|
||||
i := 0
|
||||
|
Reference in New Issue
Block a user