add view files route func, template not fin
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
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,
|
||||
})
|
||||
}
|
||||
@@ -29,4 +29,15 @@ func SetRoutes(r *gin.Engine) {
|
||||
{
|
||||
apiGroup.GET("/files", context.PatchContext(api.GetFileList))
|
||||
}
|
||||
|
||||
viewGroup := r.Group("/v")
|
||||
{
|
||||
viewGroup.GET("*path", context.PatchContext(FSList))
|
||||
}
|
||||
dlGroup := r.Group("/dl")
|
||||
{
|
||||
dlGroup.GET("*path", func(c *gin.Context) {
|
||||
c.JSON(200, gin.H{})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user