[Feature]: Add filesystem config contentTypeCharset support (#2438)

* Update filesystem.go

* Update filesystem_test.go

* Update filesystem.md

* fmt
pull/2445/head
bcd 2023-05-02 14:40:20 +08:00 committed by GitHub
parent 3a7dbd0b48
commit 3c3f12b76c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 15 deletions

View File

@ -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"`
}
```
@ -290,5 +296,6 @@ var ConfigDefault = Config{
Browse: false,
Index: "/index.html",
MaxAge: 0,
ContentTypeCharset: "",
}
```

View File

@ -53,6 +53,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"`
}
// ConfigDefault is the default config
@ -63,6 +69,7 @@ var ConfigDefault = Config{
Browse: false,
Index: "/index.html",
MaxAge: 0,
ContentTypeCharset: "",
}
// New creates a new middleware handler.
@ -176,7 +183,11 @@ func New(config ...Config) fiber.Handler {
contentLength := int(stat.Size())
// Set Content Type header
if cfg.ContentTypeCharset == "" {
c.Type(getFileExtension(stat.Name()))
} else {
c.Type(getFileExtension(stat.Name()), cfg.ContentTypeCharset)
}
// Set Last Modified header
if !modTime.IsZero() {

View File

@ -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"))
}