Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 04:00 UTC
fileserver: Fix displayed file size if it is symlink (#4354)
* Fix file size if it is symlink * change the variable name for readability
HayatoShiba committed
Sep 18, 2021 at 20:51 UTC
d3a025994466c3d055fc3fc62bbb048c76f3e1c2
2 files changed
+21
-6
modules/caddyhttp/fileserver/browse.go
+6
-1
@@ -137,7 +137,12 @@ func (fsrv *FileServer) loadDirectoryContents(dir *os.File, root, urlPath string
137
// user can presumably browse "up" to parent folder if path is longer than "/"
138
canGoUp := len(urlPath) > 1
139
140
- return fsrv.directoryListing(files, canGoUp, root, urlPath, repl), nil
140
+ l, err := fsrv.directoryListing(files, canGoUp, root, urlPath, repl)
141
+ if err != nil {
142
+ return browseTemplateContext{}, err
143
+ }
144
+
145
+ return l, nil
146
}
147
148
// browseApplyQueryParams applies query parameters to the listing.
modules/caddyhttp/fileserver/browsetplcontext.go
+15
-5
@@ -27,7 +27,7 @@ import (
27
"github.com/dustin/go-humanize"
28
)
29
30
-func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, root, urlPath string, repl *caddy.Replacer) browseTemplateContext {
30
+func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, root, urlPath string, repl *caddy.Replacer) (browseTemplateContext, error) {
31
filesToHide := fsrv.transformHidePaths(repl)
32
33
var dirCount, fileCount int
@@ -52,11 +52,21 @@ func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, root
52
fileCount++
53
}
54
55
+ fileIsSymlink := isSymlink(f)
56
+ size := f.Size()
57
+ if fileIsSymlink {
58
+ info, err := os.Stat(name)
59
+ if err != nil {
60
+ return browseTemplateContext{}, err
61
+ }
62
+ size = info.Size()
63
+ }
64
+
65
fileInfos = append(fileInfos, fileInfo{
66
IsDir: isDir,
57
- IsSymlink: isSymlink(f),
58
- Name: f.Name(),
59
- Size: f.Size(),
67
+ IsSymlink: fileIsSymlink,
68
+ Name: name,
69
+ Size: size,
70
URL: u.String(),
71
ModTime: f.ModTime().UTC(),
72
Mode: f.Mode(),
@@ -70,7 +80,7 @@ func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, root
80
Items: fileInfos,
81
NumDirs: dirCount,
82
NumFiles: fileCount,
73
- }
83
+ }, nil
84
}
85
86
// browseTemplateContext provides the template context for directory listings.