add list template

This commit is contained in:
Jay 2019-02-26 17:42:56 +08:00
parent 6751afdfde
commit c67e110c7e
3 changed files with 43 additions and 7 deletions

View File

@ -48,6 +48,12 @@ func (p *Context) BindData(i interface{}) error {
return p.ShouldBindWith(i, b) return p.ShouldBindWith(i, b)
} }
// HTML -
func (p *Context) HTML(tmpl string, data interface{}) {
p.Context.HTML(http.StatusOK, tmpl, data)
p.Abort()
}
// Success - // Success -
func (p *Context) Success(res ...interface{}) { func (p *Context) Success(res ...interface{}) {
var resBody interface{} var resBody interface{}

View File

@ -1,17 +1,21 @@
package routes package routes
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"path" "path"
"regexp" "regexp"
"time"
"git.trj.tw/golang/go-file-serve/module/config" "git.trj.tw/golang/go-file-serve/module/config"
"git.trj.tw/golang/go-file-serve/module/context" "git.trj.tw/golang/go-file-serve/module/context"
) )
type responseFS struct { type responseFS struct {
Name string `json:"name"` Name string `json:"name"`
Path string `json:"path"` Path string `json:"path"`
Size int64 `json:"size"`
Mtime time.Time `json:"mtime"`
} }
// FSList - // FSList -
@ -39,8 +43,10 @@ func FSList(c *context.Context) {
for _, v := range info { for _, v := range info {
f := responseFS{ f := responseFS{
Name: v.Name(), Name: v.Name(),
Path: path.Join(p, v.Name()), Path: path.Join(p, v.Name()),
Size: v.Size(),
Mtime: v.ModTime(),
} }
if v.IsDir() { if v.IsDir() {
f.Name += "/" f.Name += "/"
@ -50,8 +56,19 @@ func FSList(c *context.Context) {
} }
} }
c.Success(map[string]interface{}{ list := make([]responseFS, 0, len(dirs)+len(files))
"directory": dirs, list = append(list, dirs...)
"file": files, 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,
}) })
// c.Success(map[string]interface{}{
// "directory": dirs,
// "file": files,
// })
} }

View File

@ -1,10 +1,12 @@
package routes package routes
import ( import (
"html/template"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"path" "path"
"regexp"
"git.trj.tw/golang/go-file-serve/module/context" "git.trj.tw/golang/go-file-serve/module/context"
"git.trj.tw/golang/go-file-serve/routes/api" "git.trj.tw/golang/go-file-serve/routes/api"
@ -22,6 +24,17 @@ func NewEngine() *gin.Engine {
// SetRoutes - // SetRoutes -
func SetRoutes(r *gin.Engine) { func SetRoutes(r *gin.Engine) {
r.SetFuncMap(template.FuncMap{
"parsePath": func(p string) string {
regex := regexp.MustCompile("^/")
p = regex.ReplaceAllString(p, "")
regex = regexp.MustCompile("/$")
p = regex.ReplaceAllString(p, "")
return p
},
})
tmpls := make([]string, 0) tmpls := make([]string, 0)
files, err := ioutil.ReadDir("./templates") files, err := ioutil.ReadDir("./templates")
if err != nil { if err != nil {