add api route, but not use
This commit is contained in:
@@ -0,0 +1 @@
|
||||
package api
|
||||
@@ -0,0 +1,68 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"strconv"
|
||||
|
||||
"git.trj.tw/golang/go-file-serve/module/config"
|
||||
"git.trj.tw/golang/go-file-serve/module/context"
|
||||
)
|
||||
|
||||
// GetFileList -
|
||||
func GetFileList(c *context.Context) {
|
||||
var err error
|
||||
limit := -1
|
||||
skip := 0
|
||||
if str, ok := c.GetQuery("limit"); ok && len(str) > 0 {
|
||||
limit, err = strconv.Atoi(str)
|
||||
if err != nil {
|
||||
limit = -1
|
||||
}
|
||||
if limit <= 0 {
|
||||
limit = -1
|
||||
}
|
||||
}
|
||||
if str, ok := c.GetQuery("skip"); ok && len(str) > 0 {
|
||||
skip, err = strconv.Atoi(str)
|
||||
if err != nil {
|
||||
skip = 0
|
||||
}
|
||||
if skip < 0 {
|
||||
skip = 0
|
||||
}
|
||||
}
|
||||
|
||||
conf := config.GetConf()
|
||||
if conf == nil {
|
||||
c.ServerError()
|
||||
return
|
||||
}
|
||||
|
||||
fileInfo, err := ioutil.ReadDir(conf.FilePath)
|
||||
if err != nil {
|
||||
c.ServerError()
|
||||
return
|
||||
}
|
||||
|
||||
totalItem := len(fileInfo)
|
||||
|
||||
if skip < totalItem {
|
||||
if limit > 0 {
|
||||
fileInfo = fileInfo[skip : skip+limit]
|
||||
} else {
|
||||
fileInfo = fileInfo[skip:]
|
||||
}
|
||||
} else {
|
||||
fileInfo = nil
|
||||
}
|
||||
|
||||
resArr := make([]string, 0, len(fileInfo))
|
||||
for _, v := range fileInfo {
|
||||
resArr = append(resArr, v.Name())
|
||||
}
|
||||
|
||||
c.Success(map[string]interface{}{
|
||||
"files": resArr,
|
||||
"total": totalItem,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user