1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-18 10:27:06 +00:00

file-server: fix ShowList on root dir

Former-commit-id: 6795382235d76942bcfd31ecc0b4ab02ecb85a8a
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-07-05 05:39:48 +03:00
parent 0a1b500c8b
commit 57dc64625d
12 changed files with 142 additions and 131 deletions

View File

@@ -2,10 +2,6 @@ package context
import (
"io"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/kataras/iris/v12/macro"
@@ -66,13 +62,6 @@ type RouteReadOnly interface {
// MainHandlerIndex returns the first registered handler's index for the route.
MainHandlerIndex() int
// StaticSites if not empty, refers to the system (or virtual if embedded) directory
// and sub directories that this "GET" route was registered to serve files and folders
// that contain index.html (a site). The index handler may registered by other
// route, manually or automatic by the framework,
// get the route by `Application#GetRouteByPath(staticSite.RequestPath)`.
StaticSites() []StaticSite
// Sitemap properties: https://www.sitemaps.org/protocol.html
// GetLastMod returns the date of last modification of the file served by this route.
@@ -82,51 +71,3 @@ type RouteReadOnly interface {
// GetPriority returns the priority of this route's URL relative to other URLs on your site.
GetPriority() float32
}
// StaticSite is a structure which is used as field on the `Route`
// and route registration on the `APIBuilder#HandleDir`.
// See `GetStaticSites` and `APIBuilder#HandleDir`.
type StaticSite struct {
Dir string `json:"dir"`
RequestPath string `json:"requestPath"`
}
// GetStaticSites search for a relative filename of "indexName" in "rootDir" and all its subdirectories
// and returns a list of structures which contains the directory found an "indexName" and the request path
// that a route should be registered to handle this "indexName".
// The request path is given by the directory which an index exists on.
func GetStaticSites(rootDir, rootRequestPath, indexName string) (sites []StaticSite) {
f, err := os.Open(rootDir)
if err != nil {
return nil
}
list, err := f.Readdir(-1)
f.Close()
if err != nil {
return nil
}
if len(list) == 0 {
return nil
}
for _, l := range list {
dir := filepath.Join(rootDir, l.Name())
if l.IsDir() {
sites = append(sites, GetStaticSites(dir, path.Join(rootRequestPath, l.Name()), indexName)...)
continue
}
if l.Name() == strings.TrimPrefix(indexName, "/") {
sites = append(sites, StaticSite{
Dir: filepath.FromSlash(rootDir),
RequestPath: rootRequestPath,
})
continue
}
}
return
}