Repository: caddyserver/caddy (language: go)

Issue:
file_server browse: Add total size of files to summary
In my point of view, Caddy has the most beautiful and functionally the best directory listings of all web servers.

The summary line below the breadcrumb navigation currently shows the number of directories and number of files. I would like to suggest to add the (human readable) size of all files as well to this summary section:

![image](https://github.com/caddyserver/caddy/assets/37350514/87161125-3d98-4985-b52f-88b21467e3fe)



Discussion / hints:

Fix patch:
diff --git a/modules/caddyhttp/fileserver/browse.html b/modules/caddyhttp/fileserver/browse.html
index e0e12969e99..af1772bfde1 100644
--- a/modules/caddyhttp/fileserver/browse.html
+++ b/modules/caddyhttp/fileserver/browse.html
@@ -790,6 +790,9 @@ <h1>
 						<span class="meta-item">
 							<b>{{.NumFiles}}</b> file{{if ne 1 .NumFiles}}s{{end}}
 						</span>
+						<span class="meta-item">
+							<b>{{.HumanTotalFileSize}}</b> total 
+						</span>
 						{{- if ne 0 .Limit}}
 						<span class="meta-item">
 							(of which only <b>{{.Limit}}</b> are displayed)
diff --git a/modules/caddyhttp/fileserver/browsetplcontext.go b/modules/caddyhttp/fileserver/browsetplcontext.go
index 682273c0a20..a74fe316472 100644
--- a/modules/caddyhttp/fileserver/browsetplcontext.go
+++ b/modules/caddyhttp/fileserver/browsetplcontext.go
@@ -86,6 +86,10 @@ func (fsrv *FileServer) directoryListing(ctx context.Context, entries []fs.DirEn
 			// was already set above.
 		}
 
+		if !isDir {
+			tplCtx.TotalFileSize += size
+		}
+
 		u := url.URL{Path: "./" + name} // prepend with "./" to fix paths with ':' in the name
 
 		tplCtx.Items = append(tplCtx.Items, fileInfo{
@@ -129,6 +133,9 @@ type browseTemplateContext struct {
 	// The number of files (items that aren't directories) in the listing.
 	NumFiles int `json:"num_files"`
 
+	// The total size of all files in the listing.
+	TotalFileSize int64 `json:"total_file_size"`
+
 	// Sort column used
 	Sort string `json:"sort,omitempty"`
 
@@ -252,6 +259,13 @@ func (fi fileInfo) HumanSize() string {
 	return humanize.IBytes(uint64(fi.Size))
 }
 
+// HumanTotalFileSize returns the total size of all files
+// in the listing as a human-readable string in IEC format
+// (i.e. power of 2 or base 1024).
+func (btc browseTemplateContext) HumanTotalFileSize() string {
+	return humanize.IBytes(uint64(btc.TotalFileSize))
+}
+
 // HumanModTime returns the modified time of the file
 // as a human-readable string given by format.
 func (fi fileInfo) HumanModTime(format string) string {


Test patch: