From 9b2521427222c897ab25b0dabf43d391cd91e89e Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Fri, 10 Jun 2016 22:44:33 +0100 Subject: [PATCH] refactor some things --- filemanager.go | 54 ++++++-------------------------------------------- listing.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++ setup.go | 2 +- 3 files changed, 57 insertions(+), 49 deletions(-) diff --git a/filemanager.go b/filemanager.go index c46c5b4d..f457261d 100644 --- a/filemanager.go +++ b/filemanager.go @@ -12,14 +12,12 @@ import ( "bytes" "encoding/json" "net/http" - "net/url" "os" "path" "strings" "text/template" "github.com/mholt/caddy/caddyhttp/httpserver" - "github.com/mholt/caddy/caddyhttp/staticfiles" ) // Template used to show FileManager @@ -42,61 +40,21 @@ type Config struct { Variables interface{} } -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 -} - // ServeHTTP determines if the request is for this plugin, and if all prerequisites are met. // If so, control is handed over to ServeListing. func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { var fmc *Config + // See if there's a browse configuration to match the path for i := range f.Configs { if httpserver.Path(r.URL.Path).Matches(f.Configs[i].BaseURL) { fmc = &f.Configs[i] + // Serve Assets + if httpserver.Path(r.URL.Path).Matches(fmc.BaseURL + "/_filemanagerinternal") { + return ServeAssets(w, r, fmc) + } + // Browse works on existing directories; delegate everything else requestedFilepath, err := fmc.Root.Open(strings.Replace(r.URL.Path, fmc.BaseURL, "", 1)) if err != nil { diff --git a/listing.go b/listing.go index a7a2e1e1..8fe9b98b 100644 --- a/listing.go +++ b/listing.go @@ -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 { diff --git a/setup.go b/setup.go index f3e815e0..ab43f8cc 100644 --- a/setup.go +++ b/setup.go @@ -52,7 +52,7 @@ func fileManagerParse(c *caddy.Controller) ([]Config, error) { for c.Next() { var fmc = Config{ PathScope: ".", - BaseURL: "/", + BaseURL: "", StyleSheet: "", }