Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion modules/caddyhttp/fileserver/browse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 "/"
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.
Expand Down
20 changes: 15 additions & 5 deletions modules/caddyhttp/fileserver/browsetplcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"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)

var dirCount, fileCount int
Expand All @@ -52,11 +52,21 @@ func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, root
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{
IsDir: isDir,
IsSymlink: isSymlink(f),
Name: f.Name(),
Size: f.Size(),
IsSymlink: fileIsSymlink,
Name: name,
Size: size,
URL: u.String(),
ModTime: f.ModTime().UTC(),
Mode: f.Mode(),
Expand All @@ -70,7 +80,7 @@ func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, root
Items: fileInfos,
NumDirs: dirCount,
NumFiles: fileCount,
}
}, nil
}

// browseTemplateContext provides the template context for directory listings.
Expand Down