From 63b39c78ee7708cfa67b9c4b7c317f900d6e11b3 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Tue, 24 Mar 2015 23:05:42 -0600 Subject: [PATCH] Better default template; other fixes --- middleware/browse/browse.go | 44 +++++------ middleware/browse/template.go | 143 ++++++++++++++++++++++++++++++++++ 2 files changed, 161 insertions(+), 26 deletions(-) create mode 100644 middleware/browse/template.go diff --git a/middleware/browse/browse.go b/middleware/browse/browse.go index 9fa0997f..f2295d07 100644 --- a/middleware/browse/browse.go +++ b/middleware/browse/browse.go @@ -13,6 +13,7 @@ import ( "strings" "time" + "github.com/dustin/go-humanize" "github.com/mholt/caddy/middleware" ) @@ -55,6 +56,14 @@ type FileInfo struct { Mode os.FileMode } +func (fi FileInfo) HumanSize() string { + return humanize.Bytes(uint64(fi.Size)) +} + +func (fi FileInfo) HumanModTime(format string) string { + return fi.ModTime.Format(format) +} + var IndexPages = []string{ "index.html", "index.htm", @@ -205,30 +214,17 @@ func parse(c middleware.Controller) ([]BrowseConfig, error) { for c.Next() { var bc BrowseConfig - if !c.NextArg() { + // First argument is directory to allow browsing; default is site root + if c.NextArg() { + bc.PathScope = c.Val() + } else { bc.PathScope = "/" - err := appendCfg(bc) - if err != nil { - return configs, err - } - continue } - bc.PathScope = c.Val() - - if !c.NextArg() { - err := appendCfg(bc) - if err != nil { - return configs, err - } - continue - } - - tplFile := c.Val() + // Second argument would be the template file to use var tplText string - - if tplFile != "" { - tplBytes, err := ioutil.ReadFile(tplFile) + if c.NextArg() { + tplBytes, err := ioutil.ReadFile(c.Val()) if err != nil { return configs, err } @@ -237,12 +233,14 @@ func parse(c middleware.Controller) ([]BrowseConfig, error) { tplText = defaultTemplate } + // Build the template tpl, err := template.New("listing").Parse(tplText) if err != nil { return configs, err } bc.Template = tpl + // Save configuration err = appendCfg(bc) if err != nil { return configs, err @@ -251,9 +249,3 @@ func parse(c middleware.Controller) ([]BrowseConfig, error) { return configs, nil } - -const defaultTemplate = ` -{{range .}} - {{.Name}}
-{{end}} -` diff --git a/middleware/browse/template.go b/middleware/browse/template.go new file mode 100644 index 00000000..7bc6cc4d --- /dev/null +++ b/middleware/browse/template.go @@ -0,0 +1,143 @@ +package browse + +// The default template to use when serving up directory listings +const defaultTemplate = ` + + + {{.Name}} + + + + +
+ {{if .CanGoUp}} + + {{else}} +
 
+ {{end}} + +

{{.Path}}

+
+
+ + + + + + + {{range .Items}} + + + + + + {{end}} +
NameSizeModified
+ {{if .IsDir}}📂{{else}}📄{{end}} + {{.Name}} + {{.HumanSize}}{{.HumanModTime "01/02/2006 3:04:05 PM -0700"}}
+
+ +`