mirror of https://github.com/gofiber/fiber.git
[Feature]: Add filesystem config contentTypeCharset support (#2438)
* Update filesystem.go * Update filesystem_test.go * Update filesystem.md * fmtpull/2445/head
parent
3a7dbd0b48
commit
3c3f12b76c
|
@ -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: "",
|
||||
}
|
||||
```
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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…
Reference in New Issue