2019-02-26 06:34:11 +00:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
2019-02-26 09:42:56 +00:00
|
|
|
"fmt"
|
2019-02-26 06:34:11 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"path"
|
|
|
|
"regexp"
|
2019-02-26 09:42:56 +00:00
|
|
|
"time"
|
2019-02-26 06:34:11 +00:00
|
|
|
|
|
|
|
"git.trj.tw/golang/go-file-serve/module/config"
|
|
|
|
"git.trj.tw/golang/go-file-serve/module/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
type responseFS struct {
|
2019-02-26 09:42:56 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Path string `json:"path"`
|
|
|
|
Size int64 `json:"size"`
|
|
|
|
Mtime time.Time `json:"mtime"`
|
2019-02-26 06:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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{
|
2019-02-26 09:42:56 +00:00
|
|
|
Name: v.Name(),
|
|
|
|
Path: path.Join(p, v.Name()),
|
|
|
|
Size: v.Size(),
|
|
|
|
Mtime: v.ModTime(),
|
2019-02-26 06:34:11 +00:00
|
|
|
}
|
|
|
|
if v.IsDir() {
|
|
|
|
f.Name += "/"
|
|
|
|
dirs = append(dirs, f)
|
|
|
|
} else {
|
|
|
|
files = append(files, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 09:42:56 +00:00
|
|
|
list := make([]responseFS, 0, len(dirs)+len(files))
|
|
|
|
list = append(list, dirs...)
|
|
|
|
list = append(list, files...)
|
|
|
|
dirs = nil
|
|
|
|
files = nil
|
|
|
|
|
|
|
|
c.HTML("list.tmpl", map[string]interface{}{
|
|
|
|
"title": fmt.Sprintf("Path: /%s", p),
|
|
|
|
"path": fmt.Sprintf("/%s", p),
|
|
|
|
"list": list,
|
2019-02-26 06:34:11 +00:00
|
|
|
})
|
2019-02-26 09:42:56 +00:00
|
|
|
// c.Success(map[string]interface{}{
|
|
|
|
// "directory": dirs,
|
|
|
|
// "file": files,
|
|
|
|
// })
|
2019-02-26 06:34:11 +00:00
|
|
|
}
|