1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-22 14:13:29 +00:00

🔥 Add PathPrefix parameter to filesystem middleware (#1326)

See #1308

This adds a parameter called `PathPrefix` to `filesystem.Config`
that is prepended to any filepath being read from
`filesystem.Root`.

Intended to be used with Go 1.16's `embed.FS` type.

Signed-off-by: Tom <tom@tdpain.net>
This commit is contained in:
Tom 2021-05-12 07:58:01 +01:00 committed by GitHub
parent e7d5759733
commit 21c9fb4936
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 13 deletions

View File

@ -92,11 +92,11 @@ func main() {
}))
// Access file "image.png" under `static/` directory via URL: `http://<server>/static/image.png`.
// With `http.FS(embedDirStatic)`, you have to access it via URL:
// Without `PathPrefix`, you have to access it via URL:
// `http://<server>/static/static/image.png`.
subFS, _ := fs.Sub(embedDirStatic, "static")
app.Use("/static", filesystem.New(filesystem.Config{
Root: http.FS(subFS),
Root: http.FS(embedDirStatic),
PathPrefix: "static"
Browse: true,
}))
@ -251,6 +251,14 @@ type Config struct {
// Required. Default: nil
Root http.FileSystem `json:"-"`
// PathPrefix defines a prefix to be added to a filepath when
// reading a file from the FileSystem.
//
// Use when using Go 1.16 embed.FS
//
// Optional. Default ""
PathPrefix string `json:"path_prefix"`
// Enable directory browsing.
//
// Optional. Default: false
@ -278,10 +286,11 @@ type Config struct {
```go
var ConfigDefault = Config{
Next: nil,
Root: nil,
Browse: false,
Index: "/index.html",
MaxAge: 0,
Next: nil,
Root: nil,
PathPrefix: "",
Browse: false,
Index: "/index.html",
MaxAge: 0,
}
```

View File

@ -23,6 +23,14 @@ type Config struct {
// Required. Default: nil
Root http.FileSystem `json:"-"`
// PathPrefix defines a prefix to be added to a filepath when
// reading a file from the FileSystem.
//
// Use when using Go 1.16 embed.FS
//
// Optional. Default ""
PathPrefix string `json:"path_prefix"`
// Enable directory browsing.
//
// Optional. Default: false
@ -47,11 +55,12 @@ type Config struct {
// ConfigDefault is the default config
var ConfigDefault = Config{
Next: nil,
Root: nil,
Browse: false,
Index: "/index.html",
MaxAge: 0,
Next: nil,
Root: nil,
PathPrefix: "",
Browse: false,
Index: "/index.html",
MaxAge: 0,
}
// New creates a new middleware handler
@ -79,6 +88,10 @@ func New(config ...Config) fiber.Handler {
panic("filesystem: Root cannot be nil")
}
if cfg.PathPrefix != "" && !strings.HasPrefix(cfg.PathPrefix, "/") {
cfg.PathPrefix = "/" + cfg.PathPrefix
}
var once sync.Once
var prefix string
var cacheControlStr = "public, max-age=" + strconv.Itoa(cfg.MaxAge)
@ -107,6 +120,11 @@ func New(config ...Config) fiber.Handler {
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
// Add PathPrefix
if cfg.PathPrefix != "" {
// PathPrefix already has a "/" prefix
path = cfg.PathPrefix + path
}
var (
file http.File

View File

@ -32,6 +32,11 @@ func Test_FileSystem(t *testing.T) {
NotFoundFile: "index.html",
}))
app.Use("/prefix", New(Config{
Root: http.Dir("../../.github/testdata/fs"),
PathPrefix: "img",
}))
tests := []struct {
name string
url string
@ -96,6 +101,12 @@ func Test_FileSystem(t *testing.T) {
statusCode: 200,
contentType: "text/html",
},
{
name: "PathPrefix should be applied",
url: "/prefix/fiber.png",
statusCode: 200,
contentType: "image/png",
},
}
for _, tt := range tests {