Skip to content

Commit

Permalink
Fix: file opening path and directory browsing path of filesystem (#1547)
Browse files Browse the repository at this point in the history
* Fix: file opening path and directory browsing path of filesystem

* Update: utils.TrimRight instead of strings.TrimSuffix
  • Loading branch information
fufuok committed Sep 29, 2021
1 parent afa53ae commit 8b3a06b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
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

0 comments on commit 8b3a06b

Please sign in to comment.