From 195aa2a3a79b879e58f9e0d5c009c47b514df36c Mon Sep 17 00:00:00 2001 From: Fufu Date: Tue, 28 Sep 2021 23:37:48 +0800 Subject: [PATCH 1/2] Fix: file opening path and directory browsing path of filesystem --- middleware/filesystem/README.md | 4 ++-- middleware/filesystem/filesystem.go | 3 +++ middleware/filesystem/filesystem_test.go | 6 ++++++ middleware/filesystem/utils.go | 2 +- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/middleware/filesystem/README.md b/middleware/filesystem/README.md index ddf1b4c883..52e0f8bbc8 100644 --- a/middleware/filesystem/README.md +++ b/middleware/filesystem/README.md @@ -44,7 +44,7 @@ Then create a Fiber app with `app := fiber.New()`. ```go // Provide a minimal config app.Use(filesystem.New(filesystem.Config{ - Root: http.Dir("./assets") + Root: http.Dir("./assets"), })) // Or extend your config for customization @@ -96,7 +96,7 @@ func main() { // `http:///static/static/image.png`. app.Use("/static", filesystem.New(filesystem.Config{ Root: http.FS(embedDirStatic), - PathPrefix: "static" + PathPrefix: "static", Browse: true, })) diff --git a/middleware/filesystem/filesystem.go b/middleware/filesystem/filesystem.go index 88eb79958b..be8ab17bcc 100644 --- a/middleware/filesystem/filesystem.go +++ b/middleware/filesystem/filesystem.go @@ -131,6 +131,9 @@ func New(config ...Config) fiber.Handler { stat os.FileInfo ) + if len(path) > 1 { + path = strings.TrimSuffix(path, "/") + } file, err = cfg.Root.Open(path) if err != nil && os.IsNotExist(err) && cfg.NotFoundFile != "" { file, err = cfg.Root.Open(cfg.NotFoundFile) diff --git a/middleware/filesystem/filesystem_test.go b/middleware/filesystem/filesystem_test.go index fb71d40329..0605fc151d 100644 --- a/middleware/filesystem/filesystem_test.go +++ b/middleware/filesystem/filesystem_test.go @@ -89,6 +89,12 @@ func Test_FileSystem(t *testing.T) { statusCode: 200, contentType: "text/html", }, + { + name: "Should list the directory contents", + url: "/dir/img/", + statusCode: 200, + contentType: "text/html", + }, { name: "Should be returns status 200", url: "/dir/img/fiber.png", diff --git a/middleware/filesystem/utils.go b/middleware/filesystem/utils.go index 3d20554be2..3963773f5e 100644 --- a/middleware/filesystem/utils.go +++ b/middleware/filesystem/utils.go @@ -40,7 +40,7 @@ func dirList(c *fiber.Ctx, f http.File) error { fmt.Fprint(c, "