mirror of
https://github.com/filebrowser/filebrowser.git
synced 2024-06-07 23:00:43 +00:00
124 lines
2.4 KiB
Go
124 lines
2.4 KiB
Go
package filemanager
|
|
|
|
import (
|
|
"net/url"
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
|
"github.com/mholt/caddy/caddyhttp/staticfiles"
|
|
)
|
|
|
|
// A Listing is the context used to fill out a template.
|
|
type Listing struct {
|
|
// The name of the directory (the last element of the path)
|
|
Name string
|
|
|
|
// The full path of the request
|
|
Path string
|
|
|
|
// Whether the parent directory is browsable
|
|
CanGoUp bool
|
|
|
|
// The items (files and folders) in the path
|
|
Items []FileInfo
|
|
|
|
// The number of directories in the listing
|
|
NumDirs int
|
|
|
|
// The number of files (items that aren't directories) in the listing
|
|
NumFiles int
|
|
|
|
// Which sorting order is used
|
|
Sort string
|
|
|
|
// And which order
|
|
Order string
|
|
|
|
// If ≠0 then Items have been limited to that many elements
|
|
ItemsLimitedTo int
|
|
|
|
// Optional custom variables for use in browse templates
|
|
User interface{}
|
|
|
|
// StyleSheet to costumize the page
|
|
StyleSheet string
|
|
|
|
httpserver.Context
|
|
}
|
|
|
|
func directoryListing(files []os.FileInfo, canGoUp bool, urlPath string) (Listing, bool) {
|
|
var (
|
|
fileinfos []FileInfo
|
|
dirCount, fileCount int
|
|
hasIndexFile bool
|
|
)
|
|
|
|
for _, f := range files {
|
|
name := f.Name()
|
|
|
|
for _, indexName := range staticfiles.IndexPages {
|
|
if name == indexName {
|
|
hasIndexFile = true
|
|
break
|
|
}
|
|
}
|
|
|
|
if f.IsDir() {
|
|
name += "/"
|
|
dirCount++
|
|
} else {
|
|
fileCount++
|
|
}
|
|
|
|
url := url.URL{Path: "./" + name} // prepend with "./" to fix paths with ':' in the name
|
|
|
|
fileinfos = append(fileinfos, FileInfo{
|
|
IsDir: f.IsDir(),
|
|
Name: f.Name(),
|
|
Size: f.Size(),
|
|
URL: url.String(),
|
|
ModTime: f.ModTime().UTC(),
|
|
Mode: f.Mode(),
|
|
})
|
|
}
|
|
|
|
return Listing{
|
|
Name: path.Base(urlPath),
|
|
Path: urlPath,
|
|
CanGoUp: canGoUp,
|
|
Items: fileinfos,
|
|
NumDirs: dirCount,
|
|
NumFiles: fileCount,
|
|
}, hasIndexFile
|
|
}
|
|
|
|
// BreadcrumbMap returns l.Path where every element is a map
|
|
// of URLs and path segment names.
|
|
func (l Listing) BreadcrumbMap() map[string]string {
|
|
result := map[string]string{}
|
|
|
|
if len(l.Path) == 0 {
|
|
return result
|
|
}
|
|
|
|
// skip trailing slash
|
|
lpath := l.Path
|
|
if lpath[len(lpath)-1] == '/' {
|
|
lpath = lpath[:len(lpath)-1]
|
|
}
|
|
|
|
parts := strings.Split(lpath, "/")
|
|
for i, part := range parts {
|
|
if i == 0 && part == "" {
|
|
// Leading slash (root)
|
|
result["/"] = "/"
|
|
continue
|
|
}
|
|
result[strings.Join(parts[:i+1], "/")] = part
|
|
}
|
|
|
|
return result
|
|
}
|