mirror of
https://github.com/filebrowser/filebrowser.git
synced 2024-06-07 23:00:43 +00:00
refactor some things
This commit is contained in:
parent
77e5bd2b9e
commit
9b25214272
@ -12,14 +12,12 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/mholt/caddy/caddyhttp/httpserver"
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
||||||
"github.com/mholt/caddy/caddyhttp/staticfiles"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Template used to show FileManager
|
// Template used to show FileManager
|
||||||
@ -42,61 +40,21 @@ type Config struct {
|
|||||||
Variables interface{}
|
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.
|
// ServeHTTP determines if the request is for this plugin, and if all prerequisites are met.
|
||||||
// If so, control is handed over to ServeListing.
|
// If so, control is handed over to ServeListing.
|
||||||
func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
var fmc *Config
|
var fmc *Config
|
||||||
|
|
||||||
// See if there's a browse configuration to match the path
|
// See if there's a browse configuration to match the path
|
||||||
for i := range f.Configs {
|
for i := range f.Configs {
|
||||||
if httpserver.Path(r.URL.Path).Matches(f.Configs[i].BaseURL) {
|
if httpserver.Path(r.URL.Path).Matches(f.Configs[i].BaseURL) {
|
||||||
fmc = &f.Configs[i]
|
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
|
// Browse works on existing directories; delegate everything else
|
||||||
requestedFilepath, err := fmc.Root.Open(strings.Replace(r.URL.Path, fmc.BaseURL, "", 1))
|
requestedFilepath, err := fmc.Root.Open(strings.Replace(r.URL.Path, fmc.BaseURL, "", 1))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
50
listing.go
50
listing.go
@ -1,9 +1,13 @@
|
|||||||
package filemanager
|
package filemanager
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/mholt/caddy/caddyhttp/httpserver"
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
||||||
|
"github.com/mholt/caddy/caddyhttp/staticfiles"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A Listing is the context used to fill out a template.
|
// A Listing is the context used to fill out a template.
|
||||||
@ -44,6 +48,52 @@ type Listing struct {
|
|||||||
httpserver.Context
|
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
|
// BreadcrumbMap returns l.Path where every element is a map
|
||||||
// of URLs and path segment names.
|
// of URLs and path segment names.
|
||||||
func (l Listing) BreadcrumbMap() map[string]string {
|
func (l Listing) BreadcrumbMap() map[string]string {
|
||||||
|
Loading…
Reference in New Issue
Block a user