refactor some things

This commit is contained in:
Henrique Dias
2016-06-10 22:44:33 +01:00
parent 77e5bd2b9e
commit 9b25214272
3 changed files with 57 additions and 49 deletions

View File

@@ -1,9 +1,13 @@
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.
@@ -44,6 +48,52 @@ type Listing struct {
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 {