aboutsummaryrefslogtreecommitdiffhomepage
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.go24
1 files changed, 22 insertions, 2 deletions
diff --git a/modules/caddyhttp/fileserver/browsetplcontext.go b/modules/caddyhttp/fileserver/browsetplcontext.go
index 74c14608f..81e260c71 100644
--- a/modules/caddyhttp/fileserver/browsetplcontext.go
+++ b/modules/caddyhttp/fileserver/browsetplcontext.go
@@ -80,6 +80,13 @@ func (fsrv *FileServer) directoryListing(ctx context.Context, fileSystem fs.FS,
}
size := info.Size()
+
+ if !isDir {
+ // increase the total by the symlink's size, not the target's size,
+ // by incrementing before we follow the symlink
+ tplCtx.TotalFileSize += size
+ }
+
fileIsSymlink := isSymlink(info)
symlinkPath := ""
if fileIsSymlink {
@@ -103,7 +110,8 @@ func (fsrv *FileServer) directoryListing(ctx context.Context, fileSystem fs.FS,
}
if !isDir {
- tplCtx.TotalFileSize += size
+ // increase the total including the symlink target's size
+ tplCtx.TotalFileSizeFollowingSymlinks += size
}
u := url.URL{Path: "./" + name} // prepend with "./" to fix paths with ':' in the name
@@ -150,9 +158,15 @@ 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.
+ // The total size of all files in the listing. Only includes the
+ // size of the files themselves, not the size of symlink targets
+ // (i.e. the calculation of this value does not follow symlinks).
TotalFileSize int64 `json:"total_file_size"`
+ // The total size of all files in the listing, including the
+ // size of the files targeted by symlinks.
+ TotalFileSizeFollowingSymlinks int64 `json:"total_file_size_following_symlinks"`
+
// Sort column used
Sort string `json:"sort,omitempty"`
@@ -288,6 +302,12 @@ func (btc browseTemplateContext) HumanTotalFileSize() string {
return humanize.IBytes(uint64(btc.TotalFileSize))
}
+// HumanTotalFileSizeFollowingSymlinks is the same as HumanTotalFileSize
+// except the returned value reflects the size of symlink targets.
+func (btc browseTemplateContext) HumanTotalFileSizeFollowingSymlinks() string {
+ return humanize.IBytes(uint64(btc.TotalFileSizeFollowingSymlinks))
+}
+
// HumanModTime returns the modified time of the file
// as a human-readable string given by format.
func (fi fileInfo) HumanModTime(format string) string {