summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/fileserver/browsetplcontext.go
diff options
context:
space:
mode:
Diffstat (limited to 'modules/caddyhttp/fileserver/browsetplcontext.go')
-rw-r--r--modules/caddyhttp/fileserver/browsetplcontext.go50
1 files changed, 33 insertions, 17 deletions
diff --git a/modules/caddyhttp/fileserver/browsetplcontext.go b/modules/caddyhttp/fileserver/browsetplcontext.go
index 172fa50..682273c 100644
--- a/modules/caddyhttp/fileserver/browsetplcontext.go
+++ b/modules/caddyhttp/fileserver/browsetplcontext.go
@@ -25,17 +25,23 @@ import (
"strings"
"time"
- "github.com/caddyserver/caddy/v2"
- "github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/dustin/go-humanize"
"go.uber.org/zap"
+
+ "github.com/caddyserver/caddy/v2"
+ "github.com/caddyserver/caddy/v2/modules/caddyhttp"
)
-func (fsrv *FileServer) directoryListing(ctx context.Context, entries []fs.DirEntry, canGoUp bool, root, urlPath string, repl *caddy.Replacer) browseTemplateContext {
+func (fsrv *FileServer) directoryListing(ctx context.Context, entries []fs.DirEntry, canGoUp bool, root, urlPath string, repl *caddy.Replacer) *browseTemplateContext {
filesToHide := fsrv.transformHidePaths(repl)
- var dirCount, fileCount int
- fileInfos := []fileInfo{}
+ name, _ := url.PathUnescape(urlPath)
+
+ tplCtx := &browseTemplateContext{
+ Name: path.Base(name),
+ Path: urlPath,
+ CanGoUp: canGoUp,
+ }
for _, entry := range entries {
if err := ctx.Err(); err != nil {
@@ -61,9 +67,9 @@ func (fsrv *FileServer) directoryListing(ctx context.Context, entries []fs.DirEn
// add the slash after the escape of path to avoid escaping the slash as well
if isDir {
name += "/"
- dirCount++
+ tplCtx.NumDirs++
} else {
- fileCount++
+ tplCtx.NumFiles++
}
size := info.Size()
@@ -82,7 +88,7 @@ func (fsrv *FileServer) directoryListing(ctx context.Context, entries []fs.DirEn
u := url.URL{Path: "./" + name} // prepend with "./" to fix paths with ':' in the name
- fileInfos = append(fileInfos, fileInfo{
+ tplCtx.Items = append(tplCtx.Items, fileInfo{
IsDir: isDir,
IsSymlink: fileIsSymlink,
Name: name,
@@ -90,17 +96,11 @@ func (fsrv *FileServer) directoryListing(ctx context.Context, entries []fs.DirEn
URL: u.String(),
ModTime: info.ModTime().UTC(),
Mode: info.Mode(),
+ Tpl: tplCtx, // a reference up to the template context is useful
})
}
- name, _ := url.PathUnescape(urlPath)
- return browseTemplateContext{
- Name: path.Base(name),
- Path: urlPath,
- CanGoUp: canGoUp,
- Items: fileInfos,
- NumDirs: dirCount,
- NumFiles: fileCount,
- }
+
+ return tplCtx
}
// browseTemplateContext provides the template context for directory listings.
@@ -134,6 +134,9 @@ type browseTemplateContext struct {
// Sorting order
Order string `json:"order,omitempty"`
+
+ // Display format (list or grid)
+ Layout string `json:"layout,omitempty"`
}
// Breadcrumbs returns l.Path where every element maps
@@ -227,6 +230,19 @@ type fileInfo struct {
Mode os.FileMode `json:"mode"`
IsDir bool `json:"is_dir"`
IsSymlink bool `json:"is_symlink"`
+
+ // a pointer to the template context is useful inside nested templates
+ Tpl *browseTemplateContext `json:"-"`
+}
+
+// HasExt returns true if the filename has any of the given suffixes, case-insensitive.
+func (fi fileInfo) HasExt(exts ...string) bool {
+ for _, ext := range exts {
+ if strings.HasSuffix(strings.ToLower(fi.Name), strings.ToLower(ext)) {
+ return true
+ }
+ }
+ return false
}
// HumanSize returns the size of the file as a