go-file-serve/routes/files.go

58 lines
1003 B
Go

package routes
import (
"io/ioutil"
"path"
"regexp"
"git.trj.tw/golang/go-file-serve/module/config"
"git.trj.tw/golang/go-file-serve/module/context"
)
type responseFS struct {
Name string `json:"name"`
Path string `json:"path"`
}
// FSList -
func FSList(c *context.Context) {
p := c.Param("path")
conf := config.GetConf()
basePath := conf.FilePath
_ = basePath
regex := regexp.MustCompile("^/")
p = regex.ReplaceAllString(p, "")
regex = regexp.MustCompile("\\.\\./")
p = regex.ReplaceAllString(p, "/")
fullPath := path.Join(basePath, p)
info, err := ioutil.ReadDir(fullPath)
if err != nil {
c.ServerError()
return
}
dirs := make([]responseFS, 0)
files := make([]responseFS, 0)
for _, v := range info {
f := responseFS{
Name: v.Name(),
Path: path.Join(p, v.Name()),
}
if v.IsDir() {
f.Name += "/"
dirs = append(dirs, f)
} else {
files = append(files, f)
}
}
c.Success(map[string]interface{}{
"directory": dirs,
"file": files,
})
}