Skip to content

Commit

Permalink
[Feature]: Add filesystem config contentTypeCharset support (#2438)
Browse files Browse the repository at this point in the history
* Update filesystem.go

* Update filesystem_test.go

* Update filesystem.md

* fmt
  • Loading branch information
baichangda authored May 2, 2023
1 parent 3a7dbd0 commit 3c3f12b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
23 changes: 15 additions & 8 deletions docs/api/middleware/filesystem.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,13 @@ 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"`
// 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.
//
Expand All @@ -277,6 +277,12 @@ type Config struct {
//
// Optional. Default: ""
NotFoundFile string `json:"not_found_file"`

// The value for the Content-Type HTTP-header
// that is set on the file response
//
// Optional. Default: ""
ContentTypeCharset string `json:"content_type_charset"`
}
```

Expand All @@ -286,9 +292,10 @@ type Config struct {
var ConfigDefault = Config{
Next: nil,
Root: nil,
PathPrefix: "",
PathPrefix: "",
Browse: false,
Index: "/index.html",
MaxAge: 0,
ContentTypeCharset: "",
}
```
25 changes: 18 additions & 7 deletions middleware/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,23 @@ type Config struct {
//
// Optional. Default: ""
NotFoundFile string `json:"not_found_file"`

// The value for the Content-Type HTTP-header
// that is set on the file response
//
// Optional. Default: ""
ContentTypeCharset string `json:"content_type_charset"`
}

// ConfigDefault is the default config
var ConfigDefault = Config{
Next: nil,
Root: nil,
PathPrefix: "",
Browse: false,
Index: "/index.html",
MaxAge: 0,
Next: nil,
Root: nil,
PathPrefix: "",
Browse: false,
Index: "/index.html",
MaxAge: 0,
ContentTypeCharset: "",
}

// New creates a new middleware handler.
Expand Down Expand Up @@ -176,7 +183,11 @@ func New(config ...Config) fiber.Handler {
contentLength := int(stat.Size())

// Set Content Type header
c.Type(getFileExtension(stat.Name()))
if cfg.ContentTypeCharset == "" {
c.Type(getFileExtension(stat.Name()))
} else {
c.Type(getFileExtension(stat.Name()), cfg.ContentTypeCharset)
}

// Set Last Modified header
if !modTime.IsZero() {
Expand Down
14 changes: 14 additions & 0 deletions middleware/filesystem/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,17 @@ func Test_FileSystem_UsingParam_NonFile(t *testing.T) {
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 404, resp.StatusCode)
}

func Test_FileSystem_UsingContentTypeCharset(t *testing.T) {
t.Parallel()
app := fiber.New()
app.Use(New(Config{
Root: http.Dir("../../.github/testdata/fs/index.html"),
ContentTypeCharset: "UTF-8",
}))

resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 200, resp.StatusCode)
utils.AssertEqual(t, "text/html; charset=UTF-8", resp.Header.Get("Content-Type"))
}

0 comments on commit 3c3f12b

Please sign in to comment.