2019-07-01 06:07:58 +08:00
|
|
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-05-21 11:21:33 +08:00
|
|
|
package fileserver
|
2019-05-21 05:46:34 +08:00
|
|
|
|
|
|
|
import (
|
2022-10-09 02:56:35 +08:00
|
|
|
"context"
|
2022-07-31 03:07:44 +08:00
|
|
|
"io/fs"
|
2019-05-21 05:46:34 +08:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path"
|
2024-02-06 12:31:26 +08:00
|
|
|
"path/filepath"
|
2019-05-21 05:46:34 +08:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/dustin/go-humanize"
|
2022-07-31 03:07:44 +08:00
|
|
|
"go.uber.org/zap"
|
2023-08-14 23:41:15 +08:00
|
|
|
|
|
|
|
"github.com/caddyserver/caddy/v2"
|
|
|
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
2019-05-21 05:46:34 +08:00
|
|
|
)
|
|
|
|
|
2024-01-14 04:12:43 +08:00
|
|
|
func (fsrv *FileServer) directoryListing(ctx context.Context, fileSystem fs.FS, entries []fs.DirEntry, canGoUp bool, root, urlPath string, repl *caddy.Replacer) *browseTemplateContext {
|
2019-05-21 11:21:33 +08:00
|
|
|
filesToHide := fsrv.transformHidePaths(repl)
|
2019-05-21 05:46:34 +08:00
|
|
|
|
2023-05-19 23:59:40 +08:00
|
|
|
name, _ := url.PathUnescape(urlPath)
|
|
|
|
|
|
|
|
tplCtx := &browseTemplateContext{
|
|
|
|
Name: path.Base(name),
|
|
|
|
Path: urlPath,
|
|
|
|
CanGoUp: canGoUp,
|
|
|
|
}
|
2019-05-21 05:46:34 +08:00
|
|
|
|
2022-07-31 03:07:44 +08:00
|
|
|
for _, entry := range entries {
|
2022-10-09 02:56:35 +08:00
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-07-31 03:07:44 +08:00
|
|
|
name := entry.Name()
|
2019-05-21 05:46:34 +08:00
|
|
|
|
|
|
|
if fileHidden(name, filesToHide) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-07-31 03:07:44 +08:00
|
|
|
info, err := entry.Info()
|
|
|
|
if err != nil {
|
|
|
|
fsrv.logger.Error("could not get info about directory entry",
|
|
|
|
zap.String("name", entry.Name()),
|
|
|
|
zap.String("root", root))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-04-18 09:12:03 +08:00
|
|
|
// keep track of the most recently modified item in the listing
|
|
|
|
modTime := info.ModTime()
|
|
|
|
if tplCtx.lastModified.IsZero() || modTime.After(tplCtx.lastModified) {
|
|
|
|
tplCtx.lastModified = modTime
|
|
|
|
}
|
|
|
|
|
2024-01-14 04:12:43 +08:00
|
|
|
isDir := entry.IsDir() || fsrv.isSymlinkTargetDir(fileSystem, info, root, urlPath)
|
2019-05-21 05:46:34 +08:00
|
|
|
|
2021-09-17 04:40:31 +08:00
|
|
|
// add the slash after the escape of path to avoid escaping the slash as well
|
2019-05-21 05:46:34 +08:00
|
|
|
if isDir {
|
2021-12-11 22:26:21 +08:00
|
|
|
name += "/"
|
2023-05-19 23:59:40 +08:00
|
|
|
tplCtx.NumDirs++
|
2019-05-21 05:46:34 +08:00
|
|
|
} else {
|
2023-05-19 23:59:40 +08:00
|
|
|
tplCtx.NumFiles++
|
2019-05-21 05:46:34 +08:00
|
|
|
}
|
|
|
|
|
2022-07-31 03:07:44 +08:00
|
|
|
size := info.Size()
|
2024-07-07 21:01:07 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-07-31 03:07:44 +08:00
|
|
|
fileIsSymlink := isSymlink(info)
|
2024-02-06 12:31:26 +08:00
|
|
|
symlinkPath := ""
|
2021-09-18 19:51:59 +08:00
|
|
|
if fileIsSymlink {
|
2022-07-31 03:07:44 +08:00
|
|
|
path := caddyhttp.SanitizedPathJoin(root, path.Join(urlPath, info.Name()))
|
2024-01-14 04:12:43 +08:00
|
|
|
fileInfo, err := fs.Stat(fileSystem, path)
|
2021-11-23 05:59:09 +08:00
|
|
|
if err == nil {
|
|
|
|
size = fileInfo.Size()
|
2021-09-18 19:51:59 +08:00
|
|
|
}
|
2024-02-06 12:31:26 +08:00
|
|
|
|
|
|
|
if fsrv.Browse.RevealSymlinks {
|
|
|
|
symLinkTarget, err := filepath.EvalSymlinks(path)
|
|
|
|
if err == nil {
|
|
|
|
symlinkPath = symLinkTarget
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-23 05:59:09 +08:00
|
|
|
// An error most likely means the symlink target doesn't exist,
|
|
|
|
// which isn't entirely unusual and shouldn't fail the listing.
|
|
|
|
// In this case, just use the size of the symlink itself, which
|
|
|
|
// was already set above.
|
2021-09-18 19:51:59 +08:00
|
|
|
}
|
|
|
|
|
2023-12-31 02:47:13 +08:00
|
|
|
if !isDir {
|
2024-07-07 21:01:07 +08:00
|
|
|
// increase the total including the symlink target's size
|
|
|
|
tplCtx.TotalFileSizeFollowingSymlinks += size
|
2023-12-31 02:47:13 +08:00
|
|
|
}
|
|
|
|
|
2021-12-11 22:26:21 +08:00
|
|
|
u := url.URL{Path: "./" + name} // prepend with "./" to fix paths with ':' in the name
|
|
|
|
|
2023-05-19 23:59:40 +08:00
|
|
|
tplCtx.Items = append(tplCtx.Items, fileInfo{
|
2024-02-06 12:31:26 +08:00
|
|
|
IsDir: isDir,
|
|
|
|
IsSymlink: fileIsSymlink,
|
|
|
|
Name: name,
|
|
|
|
Size: size,
|
|
|
|
URL: u.String(),
|
2024-04-18 09:12:03 +08:00
|
|
|
ModTime: modTime.UTC(),
|
2024-02-06 12:31:26 +08:00
|
|
|
Mode: info.Mode(),
|
|
|
|
Tpl: tplCtx, // a reference up to the template context is useful
|
|
|
|
SymlinkPath: symlinkPath,
|
2019-05-21 05:46:34 +08:00
|
|
|
})
|
|
|
|
}
|
2023-05-19 23:59:40 +08:00
|
|
|
|
|
|
|
return tplCtx
|
2019-05-21 05:46:34 +08:00
|
|
|
}
|
|
|
|
|
2021-05-01 10:17:23 +08:00
|
|
|
// browseTemplateContext provides the template context for directory listings.
|
|
|
|
type browseTemplateContext struct {
|
2019-05-21 05:46:34 +08:00
|
|
|
// The name of the directory (the last element of the path).
|
2020-09-01 02:33:43 +08:00
|
|
|
Name string `json:"name"`
|
2019-05-21 05:46:34 +08:00
|
|
|
|
|
|
|
// The full path of the request.
|
2020-09-01 02:33:43 +08:00
|
|
|
Path string `json:"path"`
|
2019-05-21 05:46:34 +08:00
|
|
|
|
2024-05-10 22:08:54 +08:00
|
|
|
// Whether the parent directory is browsable.
|
2020-09-01 02:33:43 +08:00
|
|
|
CanGoUp bool `json:"can_go_up"`
|
2019-05-21 05:46:34 +08:00
|
|
|
|
|
|
|
// The items (files and folders) in the path.
|
2020-09-01 02:33:43 +08:00
|
|
|
Items []fileInfo `json:"items,omitempty"`
|
|
|
|
|
|
|
|
// If ≠0 then Items starting from that many elements.
|
|
|
|
Offset int `json:"offset,omitempty"`
|
|
|
|
|
|
|
|
// If ≠0 then Items have been limited to that many elements.
|
|
|
|
Limit int `json:"limit,omitempty"`
|
2019-05-21 05:46:34 +08:00
|
|
|
|
|
|
|
// The number of directories in the listing.
|
2020-09-01 02:33:43 +08:00
|
|
|
NumDirs int `json:"num_dirs"`
|
2019-05-21 05:46:34 +08:00
|
|
|
|
|
|
|
// The number of files (items that aren't directories) in the listing.
|
2020-09-01 02:33:43 +08:00
|
|
|
NumFiles int `json:"num_files"`
|
2019-05-21 05:46:34 +08:00
|
|
|
|
2024-07-07 21:01:07 +08:00
|
|
|
// 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).
|
2023-12-31 02:47:13 +08:00
|
|
|
TotalFileSize int64 `json:"total_file_size"`
|
|
|
|
|
2024-07-07 21:01:07 +08:00
|
|
|
// 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"`
|
|
|
|
|
2019-05-21 05:46:34 +08:00
|
|
|
// Sort column used
|
2020-09-01 02:33:43 +08:00
|
|
|
Sort string `json:"sort,omitempty"`
|
2019-05-21 05:46:34 +08:00
|
|
|
|
|
|
|
// Sorting order
|
2020-09-01 02:33:43 +08:00
|
|
|
Order string `json:"order,omitempty"`
|
2023-03-11 02:19:31 +08:00
|
|
|
|
|
|
|
// Display format (list or grid)
|
|
|
|
Layout string `json:"layout,omitempty"`
|
2024-04-18 09:12:03 +08:00
|
|
|
|
|
|
|
// The most recent file modification date in the listing.
|
|
|
|
// Used for HTTP header purposes.
|
|
|
|
lastModified time.Time
|
2019-05-21 05:46:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Breadcrumbs returns l.Path where every element maps
|
|
|
|
// the link to the text to display.
|
2021-05-01 10:17:23 +08:00
|
|
|
func (l browseTemplateContext) Breadcrumbs() []crumb {
|
2019-05-21 05:46:34 +08:00
|
|
|
if len(l.Path) == 0 {
|
2020-11-23 05:50:29 +08:00
|
|
|
return []crumb{}
|
2019-05-21 05:46:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// skip trailing slash
|
|
|
|
lpath := l.Path
|
|
|
|
if lpath[len(lpath)-1] == '/' {
|
|
|
|
lpath = lpath[:len(lpath)-1]
|
|
|
|
}
|
|
|
|
parts := strings.Split(lpath, "/")
|
2020-11-23 05:50:29 +08:00
|
|
|
result := make([]crumb, len(parts))
|
|
|
|
for i, p := range parts {
|
|
|
|
if i == 0 && p == "" {
|
|
|
|
p = "/"
|
2019-05-21 05:46:34 +08:00
|
|
|
}
|
2021-11-23 16:13:09 +08:00
|
|
|
// the directory name could include an encoded slash in its path,
|
|
|
|
// so the item name should be unescaped in the loop rather than unescaping the
|
|
|
|
// entire path outside the loop.
|
|
|
|
p, _ = url.PathUnescape(p)
|
2019-05-21 05:46:34 +08:00
|
|
|
lnk := strings.Repeat("../", len(parts)-i-1)
|
2020-11-23 05:50:29 +08:00
|
|
|
result[i] = crumb{Link: lnk, Text: p}
|
2019-05-21 05:46:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-05-01 10:17:23 +08:00
|
|
|
func (l *browseTemplateContext) applySortAndLimit(sortParam, orderParam, limitParam string, offsetParam string) {
|
2019-05-21 05:46:34 +08:00
|
|
|
l.Sort = sortParam
|
|
|
|
l.Order = orderParam
|
|
|
|
|
|
|
|
if l.Order == "desc" {
|
|
|
|
switch l.Sort {
|
|
|
|
case sortByName:
|
|
|
|
sort.Sort(sort.Reverse(byName(*l)))
|
|
|
|
case sortByNameDirFirst:
|
|
|
|
sort.Sort(sort.Reverse(byNameDirFirst(*l)))
|
|
|
|
case sortBySize:
|
|
|
|
sort.Sort(sort.Reverse(bySize(*l)))
|
|
|
|
case sortByTime:
|
|
|
|
sort.Sort(sort.Reverse(byTime(*l)))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch l.Sort {
|
|
|
|
case sortByName:
|
|
|
|
sort.Sort(byName(*l))
|
|
|
|
case sortByNameDirFirst:
|
|
|
|
sort.Sort(byNameDirFirst(*l))
|
|
|
|
case sortBySize:
|
|
|
|
sort.Sort(bySize(*l))
|
|
|
|
case sortByTime:
|
|
|
|
sort.Sort(byTime(*l))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 13:56:15 +08:00
|
|
|
if offsetParam != "" {
|
|
|
|
offset, _ := strconv.Atoi(offsetParam)
|
|
|
|
if offset > 0 && offset <= len(l.Items) {
|
|
|
|
l.Items = l.Items[offset:]
|
2020-09-01 02:33:43 +08:00
|
|
|
l.Offset = offset
|
2020-07-09 13:56:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-21 05:46:34 +08:00
|
|
|
if limitParam != "" {
|
|
|
|
limit, _ := strconv.Atoi(limitParam)
|
2020-07-09 13:56:15 +08:00
|
|
|
|
2019-05-21 05:46:34 +08:00
|
|
|
if limit > 0 && limit <= len(l.Items) {
|
|
|
|
l.Items = l.Items[:limit]
|
2020-09-01 02:33:43 +08:00
|
|
|
l.Limit = limit
|
2019-05-21 05:46:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// crumb represents part of a breadcrumb menu,
|
|
|
|
// pairing a link with the text to display.
|
|
|
|
type crumb struct {
|
|
|
|
Link, Text string
|
|
|
|
}
|
|
|
|
|
|
|
|
// fileInfo contains serializable information
|
|
|
|
// about a file or directory.
|
|
|
|
type fileInfo struct {
|
2024-02-06 12:31:26 +08:00
|
|
|
Name string `json:"name"`
|
|
|
|
Size int64 `json:"size"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
ModTime time.Time `json:"mod_time"`
|
|
|
|
Mode os.FileMode `json:"mode"`
|
|
|
|
IsDir bool `json:"is_dir"`
|
|
|
|
IsSymlink bool `json:"is_symlink"`
|
|
|
|
SymlinkPath string `json:"symlink_path,omitempty"`
|
2023-05-19 23:59:40 +08:00
|
|
|
|
|
|
|
// a pointer to the template context is useful inside nested templates
|
|
|
|
Tpl *browseTemplateContext `json:"-"`
|
2019-05-21 05:46:34 +08:00
|
|
|
}
|
|
|
|
|
2023-03-11 02:19:31 +08:00
|
|
|
// HasExt returns true if the filename has any of the given suffixes, case-insensitive.
|
|
|
|
func (fi fileInfo) HasExt(exts ...string) bool {
|
|
|
|
for _, ext := range exts {
|
|
|
|
if strings.HasSuffix(strings.ToLower(fi.Name), strings.ToLower(ext)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-05-21 05:46:34 +08:00
|
|
|
// HumanSize returns the size of the file as a
|
|
|
|
// human-readable string in IEC format (i.e.
|
|
|
|
// power of 2 or base 1024).
|
|
|
|
func (fi fileInfo) HumanSize() string {
|
|
|
|
return humanize.IBytes(uint64(fi.Size))
|
|
|
|
}
|
|
|
|
|
2023-12-31 02:47:13 +08:00
|
|
|
// 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))
|
|
|
|
}
|
|
|
|
|
2024-07-07 21:01:07 +08:00
|
|
|
// 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))
|
|
|
|
}
|
|
|
|
|
2019-05-21 05:46:34 +08:00
|
|
|
// HumanModTime returns the modified time of the file
|
|
|
|
// as a human-readable string given by format.
|
|
|
|
func (fi fileInfo) HumanModTime(format string) string {
|
|
|
|
return fi.ModTime.Format(format)
|
|
|
|
}
|
|
|
|
|
2021-05-01 10:17:23 +08:00
|
|
|
type (
|
|
|
|
byName browseTemplateContext
|
|
|
|
byNameDirFirst browseTemplateContext
|
|
|
|
bySize browseTemplateContext
|
|
|
|
byTime browseTemplateContext
|
|
|
|
)
|
2019-05-21 05:46:34 +08:00
|
|
|
|
|
|
|
func (l byName) Len() int { return len(l.Items) }
|
|
|
|
func (l byName) Swap(i, j int) { l.Items[i], l.Items[j] = l.Items[j], l.Items[i] }
|
|
|
|
|
|
|
|
func (l byName) Less(i, j int) bool {
|
|
|
|
return strings.ToLower(l.Items[i].Name) < strings.ToLower(l.Items[j].Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l byNameDirFirst) Len() int { return len(l.Items) }
|
|
|
|
func (l byNameDirFirst) Swap(i, j int) { l.Items[i], l.Items[j] = l.Items[j], l.Items[i] }
|
|
|
|
|
|
|
|
func (l byNameDirFirst) Less(i, j int) bool {
|
|
|
|
// sort by name if both are dir or file
|
|
|
|
if l.Items[i].IsDir == l.Items[j].IsDir {
|
|
|
|
return strings.ToLower(l.Items[i].Name) < strings.ToLower(l.Items[j].Name)
|
|
|
|
}
|
|
|
|
// sort dir ahead of file
|
|
|
|
return l.Items[i].IsDir
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l bySize) Len() int { return len(l.Items) }
|
|
|
|
func (l bySize) Swap(i, j int) { l.Items[i], l.Items[j] = l.Items[j], l.Items[i] }
|
|
|
|
|
|
|
|
func (l bySize) Less(i, j int) bool {
|
|
|
|
const directoryOffset = -1 << 31 // = -math.MinInt32
|
|
|
|
|
|
|
|
iSize, jSize := l.Items[i].Size, l.Items[j].Size
|
|
|
|
|
|
|
|
// directory sizes depend on the file system; to
|
|
|
|
// provide a consistent experience, put them up front
|
|
|
|
// and sort them by name
|
|
|
|
if l.Items[i].IsDir {
|
|
|
|
iSize = directoryOffset
|
|
|
|
}
|
|
|
|
if l.Items[j].IsDir {
|
|
|
|
jSize = directoryOffset
|
|
|
|
}
|
|
|
|
if l.Items[i].IsDir && l.Items[j].IsDir {
|
|
|
|
return strings.ToLower(l.Items[i].Name) < strings.ToLower(l.Items[j].Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return iSize < jSize
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l byTime) Len() int { return len(l.Items) }
|
|
|
|
func (l byTime) Swap(i, j int) { l.Items[i], l.Items[j] = l.Items[j], l.Items[i] }
|
|
|
|
func (l byTime) Less(i, j int) bool { return l.Items[i].ModTime.Before(l.Items[j].ModTime) }
|
|
|
|
|
|
|
|
const (
|
|
|
|
sortByName = "name"
|
2021-07-07 14:54:54 +08:00
|
|
|
sortByNameDirFirst = "namedirfirst"
|
2019-05-21 05:46:34 +08:00
|
|
|
sortBySize = "size"
|
|
|
|
sortByTime = "time"
|
2024-08-05 22:27:45 +08:00
|
|
|
|
|
|
|
sortOrderAsc = "asc"
|
|
|
|
sortOrderDesc = "desc"
|
2019-05-21 05:46:34 +08:00
|
|
|
)
|