fileserver: Fix displayed file size if it is symlink (#4354)

* Fix file size if it is symlink

* change the variable name for readability
This commit is contained in:
HayatoShiba 2021-09-18 20:51:59 +09:00 committed by GitHub
parent 5fda9610f9
commit d3a0259944
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 6 deletions

View file

@ -137,7 +137,12 @@ func (fsrv *FileServer) loadDirectoryContents(dir *os.File, root, urlPath string
// user can presumably browse "up" to parent folder if path is longer than "/" // user can presumably browse "up" to parent folder if path is longer than "/"
canGoUp := len(urlPath) > 1 canGoUp := len(urlPath) > 1
return fsrv.directoryListing(files, canGoUp, root, urlPath, repl), nil l, err := fsrv.directoryListing(files, canGoUp, root, urlPath, repl)
if err != nil {
return browseTemplateContext{}, err
}
return l, nil
} }
// browseApplyQueryParams applies query parameters to the listing. // browseApplyQueryParams applies query parameters to the listing.

View file

@ -27,7 +27,7 @@ import (
"github.com/dustin/go-humanize" "github.com/dustin/go-humanize"
) )
func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, root, urlPath string, repl *caddy.Replacer) browseTemplateContext { func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, root, urlPath string, repl *caddy.Replacer) (browseTemplateContext, error) {
filesToHide := fsrv.transformHidePaths(repl) filesToHide := fsrv.transformHidePaths(repl)
var dirCount, fileCount int var dirCount, fileCount int
@ -52,11 +52,21 @@ func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, root
fileCount++ fileCount++
} }
fileIsSymlink := isSymlink(f)
size := f.Size()
if fileIsSymlink {
info, err := os.Stat(name)
if err != nil {
return browseTemplateContext{}, err
}
size = info.Size()
}
fileInfos = append(fileInfos, fileInfo{ fileInfos = append(fileInfos, fileInfo{
IsDir: isDir, IsDir: isDir,
IsSymlink: isSymlink(f), IsSymlink: fileIsSymlink,
Name: f.Name(), Name: name,
Size: f.Size(), Size: size,
URL: u.String(), URL: u.String(),
ModTime: f.ModTime().UTC(), ModTime: f.ModTime().UTC(),
Mode: f.Mode(), Mode: f.Mode(),
@ -70,7 +80,7 @@ func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, root
Items: fileInfos, Items: fileInfos,
NumDirs: dirCount, NumDirs: dirCount,
NumFiles: fileCount, NumFiles: fileCount,
} }, nil
} }
// browseTemplateContext provides the template context for directory listings. // browseTemplateContext provides the template context for directory listings.