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 all commits
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
8 changes: 6 additions & 2 deletions middleware/filesystem/filesystem.go
Expand Up @@ -8,6 +8,7 @@ import (
"sync"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
)

// Config defines the config for middleware.
Expand Down Expand Up @@ -131,6 +132,9 @@ func New(config ...Config) fiber.Handler {
stat os.FileInfo
)

if len(path) > 1 {
path = utils.TrimRight(path, '/')
}
file, err = cfg.Root.Open(path)
if err != nil && os.IsNotExist(err) && cfg.NotFoundFile != "" {
file, err = cfg.Root.Open(cfg.NotFoundFile)
Expand All @@ -149,7 +153,7 @@ func New(config ...Config) fiber.Handler {

// Serve index if path is directory
if stat.IsDir() {
indexPath := strings.TrimSuffix(path, "/") + cfg.Index
indexPath := utils.TrimRight(path, '/') + cfg.Index
index, err := cfg.Root.Open(indexPath)
if err == nil {
indexStat, err := index.Stat()
Expand Down Expand Up @@ -222,7 +226,7 @@ func SendFile(c *fiber.Ctx, fs http.FileSystem, path string) (err error) {

// Serve index if path is directory
if stat.IsDir() {
indexPath := strings.TrimSuffix(path, "/") + ConfigDefault.Index
indexPath := utils.TrimRight(path, '/') + ConfigDefault.Index
index, err := fs.Open(indexPath)
if err == nil {
indexStat, err := index.Stat()
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
3 changes: 2 additions & 1 deletion middleware/filesystem/utils.go
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
)

func getFileExtension(path string) string {
Expand Down Expand Up @@ -40,7 +41,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(utils.TrimRight(c.Path(), '/') + "/..")
fmt.Fprintf(c, `<li><a href="%s" class="dir">..</a></li>`, parentPathEscaped)
}

Expand Down