add api route, but not use

This commit is contained in:
Jay
2019-02-25 23:16:16 +08:00
parent 0dc1db02b0
commit 21987754f1
6 changed files with 127 additions and 14 deletions
+1
View File
@@ -0,0 +1 @@
package api
+68
View File
@@ -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,
})
}