From 4091b6f6b4fe01876f8720332675f9c69be39541 Mon Sep 17 00:00:00 2001 From: Wim Date: Sun, 30 May 2021 00:25:30 +0200 Subject: Update vendor (#1498) --- .../labstack/echo/v4/middleware/static.go | 80 +++++++++++++++------- 1 file changed, 56 insertions(+), 24 deletions(-) (limited to 'vendor/github.com/labstack/echo/v4/middleware/static.go') diff --git a/vendor/github.com/labstack/echo/v4/middleware/static.go b/vendor/github.com/labstack/echo/v4/middleware/static.go index ae79cb5f..0106f7ce 100644 --- a/vendor/github.com/labstack/echo/v4/middleware/static.go +++ b/vendor/github.com/labstack/echo/v4/middleware/static.go @@ -42,6 +42,10 @@ type ( // the filesystem path is not doubled // Optional. Default value false. IgnoreBase bool `yaml:"ignoreBase"` + + // Filesystem provides access to the static content. + // Optional. Defaults to http.Dir(config.Root) + Filesystem http.FileSystem `yaml:"-"` } ) @@ -146,6 +150,10 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc { if config.Index == "" { config.Index = DefaultStaticConfig.Index } + if config.Filesystem == nil { + config.Filesystem = http.Dir(config.Root) + config.Root = "." + } // Index template t, err := template.New("index").Parse(html) @@ -178,49 +186,73 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc { } } - fi, err := os.Stat(name) + file, err := openFile(config.Filesystem, name) if err != nil { - if os.IsNotExist(err) { - if err = next(c); err != nil { - if he, ok := err.(*echo.HTTPError); ok { - if config.HTML5 && he.Code == http.StatusNotFound { - return c.File(filepath.Join(config.Root, config.Index)) - } - } - return - } + if !os.IsNotExist(err) { + return err + } + + if err = next(c); err == nil { + return err + } + + he, ok := err.(*echo.HTTPError) + if !(ok && config.HTML5 && he.Code == http.StatusNotFound) { + return err + } + + file, err = openFile(config.Filesystem, filepath.Join(config.Root, config.Index)) + if err != nil { + return err } - return } - if fi.IsDir() { - index := filepath.Join(name, config.Index) - fi, err = os.Stat(index) + defer file.Close() + + info, err := file.Stat() + if err != nil { + return err + } + if info.IsDir() { + index, err := openFile(config.Filesystem, filepath.Join(name, config.Index)) if err != nil { if config.Browse { - return listDir(t, name, c.Response()) + return listDir(t, name, file, c.Response()) } + if os.IsNotExist(err) { return next(c) } - return } - return c.File(index) + defer index.Close() + + info, err = index.Stat() + if err != nil { + return err + } + + return serveFile(c, index, info) } - return c.File(name) + return serveFile(c, file, info) } } } -func listDir(t *template.Template, name string, res *echo.Response) (err error) { - file, err := os.Open(name) - if err != nil { - return - } - files, err := file.Readdir(-1) +func openFile(fs http.FileSystem, name string) (http.File, error) { + pathWithSlashes := filepath.ToSlash(name) + return fs.Open(pathWithSlashes) +} + +func serveFile(c echo.Context, file http.File, info os.FileInfo) error { + http.ServeContent(c.Response(), c.Request(), info.Name(), info.ModTime(), file) + return nil +} + +func listDir(t *template.Template, name string, dir http.File, res *echo.Response) (err error) { + files, err := dir.Readdir(-1) if err != nil { return } -- cgit v1.2.3