mirror of
https://github.com/gofiber/fiber.git
synced 2025-02-21 19:32:58 +00:00
Fix: file opening path and directory browsing path of filesystem (#1547)
* Fix: file opening path and directory browsing path of filesystem * Update: utils.TrimRight instead of strings.TrimSuffix
This commit is contained in:
parent
afa53ae1f6
commit
8b3a06b164
@ -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://<server>/static/static/image.png`.
|
||||
app.Use("/static", filesystem.New(filesystem.Config{
|
||||
Root: http.FS(embedDirStatic),
|
||||
PathPrefix: "static"
|
||||
PathPrefix: "static",
|
||||
Browse: true,
|
||||
}))
|
||||
|
||||
|
@ -8,6 +8,7 @@ import (
|
||||
"sync"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/utils"
|
||||
)
|
||||
|
||||
// Config defines the config for middleware.
|
||||
@ -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)
|
||||
@ -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()
|
||||
@ -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()
|
||||
|
@ -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",
|
||||
|
@ -10,6 +10,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/utils"
|
||||
)
|
||||
|
||||
func getFileExtension(path string) string {
|
||||
@ -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)
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user