Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Add filesystem config contentTypeCharset support #2438

Merged
merged 4 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"))
}
Loading