Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 12, 19:30 UTC
fileserver: Add total file size to directory listing (#6003)
* browse: Add total file size to directory listing * Apply suggestion to remove "in " Co-authored-by: Matt Holt <mholt@users.noreply.github.com> --------- Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
Steffen Busch committed
Dec 30, 2023 at 19:47 UTC
8f9ffc587e622b6e48e9816fe69bbb9cff3073fe
2 files changed
+17
modules/caddyhttp/fileserver/browse.html
+3
@@ -790,6 +790,9 @@ footer {
790
<span class="meta-item">
791
<b>{{.NumFiles}}</b> file{{if ne 1 .NumFiles}}s{{end}}
792
</span>
793
+ <span class="meta-item">
794
+ <b>{{.HumanTotalFileSize}}</b> total
795
+ </span>
796
{{- if ne 0 .Limit}}
797
<span class="meta-item">
798
(of which only <b>{{.Limit}}</b> are displayed)
modules/caddyhttp/fileserver/browsetplcontext.go
+14
@@ -86,6 +86,10 @@ func (fsrv *FileServer) directoryListing(ctx context.Context, entries []fs.DirEn
86
// was already set above.
87
}
88
89
+ if !isDir {
90
+ tplCtx.TotalFileSize += size
91
+ }
92
+
93
u := url.URL{Path: "./" + name} // prepend with "./" to fix paths with ':' in the name
94
95
tplCtx.Items = append(tplCtx.Items, fileInfo{
@@ -129,6 +133,9 @@ type browseTemplateContext struct {
133
// The number of files (items that aren't directories) in the listing.
134
NumFiles int `json:"num_files"`
135
136
+ // The total size of all files in the listing.
137
+ TotalFileSize int64 `json:"total_file_size"`
138
+
139
// Sort column used
140
Sort string `json:"sort,omitempty"`
141
@@ -252,6 +259,13 @@ func (fi fileInfo) HumanSize() string {
259
return humanize.IBytes(uint64(fi.Size))
260
}
261
262
+// HumanTotalFileSize returns the total size of all files
263
+// in the listing as a human-readable string in IEC format
264
+// (i.e. power of 2 or base 1024).
265
+func (btc browseTemplateContext) HumanTotalFileSize() string {
266
+ return humanize.IBytes(uint64(btc.TotalFileSize))
267
+}
268
+
269
// HumanModTime returns the modified time of the file
270
// as a human-readable string given by format.
271
func (fi fileInfo) HumanModTime(format string) string {