Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: file opening path and directory browsing path of filesystem #1547

Merged
merged 2 commits into from Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions middleware/filesystem/README.md
Expand Up @@ -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
Expand Down Expand Up @@ -96,7 +96,7 @@ func main() {
// `http://<server>/static/static/image.png`.
app.Use("/static", filesystem.New(filesystem.Config{
Root: http.FS(embedDirStatic),
PathPrefix: "static"
PathPrefix: "static",
Browse: true,
}))

Expand Down
3 changes: 3 additions & 0 deletions middleware/filesystem/filesystem.go
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions middleware/filesystem/filesystem_test.go
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion middleware/filesystem/utils.go
Expand Up @@ -40,7 +40,7 @@ func dirList(c *fiber.Ctx, f http.File) error {
fmt.Fprint(c, "<ul>")

if len(basePathEscaped) > 1 {
parentPathEscaped := html.EscapeString(c.Path() + "/..")
parentPathEscaped := html.EscapeString(strings.TrimSuffix(c.Path(), "/") + "/..")
fmt.Fprintf(c, `<li><a href="%s" class="dir">..</a></li>`, parentPathEscaped)
}

Expand Down