Skip to content

Commit

Permalink
fileserver: Add total file size to directory listing (#6003)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
steffenbusch and mholt committed Dec 30, 2023
1 parent f976c84 commit 8f9ffc5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions modules/caddyhttp/fileserver/browse.html
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions modules/caddyhttp/fileserver/browsetplcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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"`

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 8f9ffc5

Please sign in to comment.