Skip to content

Commit

Permalink
🔥 Add PathPrefix parameter to filesystem middleware (#1326)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
codemicro authored May 12, 2021
1 parent e7d5759 commit 21c9fb4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
25 changes: 17 additions & 8 deletions middleware/filesystem/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}))

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
}
```
28 changes: 23 additions & 5 deletions middleware/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions middleware/filesystem/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 21c9fb4

Please sign in to comment.