summaryrefslogtreecommitdiffhomepage
path: root/modules/caddyhttp/fileserver/browsetplcontext.go
diff options
context:
space:
mode:
authorMatthew Holt <[email protected]>2023-05-19 09:59:40 -0600
committerMatthew Holt <[email protected]>2023-05-19 09:59:44 -0600
commitbd34cb6b4e171567243ff541ca303ac32fec91d8 (patch)
tree6233ba2a67697eed25ed58208f6095e729a455bd /modules/caddyhttp/fileserver/browsetplcontext.go
parent2d236ead3e0476ae3470a89b3b1b250cf4e4d7d3 (diff)
downloadcaddy-bd34cb6b4e171567243ff541ca303ac32fec91d8.tar.gz
caddy-bd34cb6b4e171567243ff541ca303ac32fec91d8.zip
fileserver: More filetypes for browse icons
Diffstat (limited to 'modules/caddyhttp/fileserver/browsetplcontext.go')
-rw-r--r--modules/caddyhttp/fileserver/browsetplcontext.go32
1 files changed, 17 insertions, 15 deletions
diff --git a/modules/caddyhttp/fileserver/browsetplcontext.go b/modules/caddyhttp/fileserver/browsetplcontext.go
index bd21aa825..e06d07267 100644
--- a/modules/caddyhttp/fileserver/browsetplcontext.go
+++ b/modules/caddyhttp/fileserver/browsetplcontext.go
@@ -31,11 +31,16 @@ import (
"go.uber.org/zap"
)
-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 +66,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 +87,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 +95,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.
@@ -230,6 +229,9 @@ 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.