Skip to content

Commit

Permalink
Add direct download option when serving static files (#1729)
Browse files Browse the repository at this point in the history
* enable download for app.Static


wording

* Add unit test.

Co-authored-by: Muhammed Efe Çetin <efectn@protonmail.com>
  • Loading branch information
Lian1230 and efectn committed Feb 3, 2022
1 parent 4d3cc6c commit af10fab
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app.go
Expand Up @@ -379,6 +379,10 @@ type Static struct {
// Optional. Default value false.
Browse bool `json:"browse"`

// When set to true, enables direct download.
// Optional. Default value false.
Download bool `json:"download"`

// The name of the index file for serving a directory.
// Optional. Default value "index.html".
Index string `json:"index"`
Expand Down
16 changes: 16 additions & 0 deletions app_test.go
Expand Up @@ -678,6 +678,22 @@ func Test_App_Static_MaxAge(t *testing.T) {
utils.AssertEqual(t, "public, max-age=100", resp.Header.Get(HeaderCacheControl), "CacheControl Control")
}

// go test -run Test_App_Static_Download
func Test_App_Static_Download(t *testing.T) {
app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)

app.Static("/fiber.png", "./.github/testdata/fs/img/fiber.png", Static{Download: true})

resp, err := app.Test(httptest.NewRequest("GET", "/fiber.png", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
utils.AssertEqual(t, false, resp.Header.Get(HeaderContentLength) == "")
utils.AssertEqual(t, "image/png", resp.Header.Get(HeaderContentType))
utils.AssertEqual(t, `attachment`, resp.Header.Get(HeaderContentDisposition))
}

// go test -run Test_App_Static_Group
func Test_App_Static_Group(t *testing.T) {
app := New()
Expand Down
4 changes: 4 additions & 0 deletions router.go
Expand Up @@ -377,6 +377,10 @@ func (app *App) registerStatic(prefix, root string, config ...Static) Router {
}
// Serve file
fileHandler(c.fasthttp)
// Sets the response Content-Disposition header to attachment if the Download option is true
if len(config) > 0 && config[0].Download {
c.Attachment()
}
// Return request if found and not forbidden
status := c.fasthttp.Response.StatusCode()
if status != StatusNotFound && status != StatusForbidden {
Expand Down

1 comment on commit af10fab

@Fenny
Copy link
Member

@Fenny Fenny commented on af10fab Feb 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: af10fab Previous: 4d3cc6c Ratio
Benchmark_App_ETag_Weak 13923 ns/op 1076 B/op 4 allocs/op 6497 ns/op 1076 B/op 4 allocs/op 2.14
Benchmark_Ctx_Accepts 511 ns/op 0 B/op 0 allocs/op 163 ns/op 0 B/op 0 allocs/op 3.13
Benchmark_SlashRecognition/indexBytes 10.8 ns/op 0 B/op 0 allocs/op 3.63 ns/op 0 B/op 0 allocs/op 2.98
Benchmark_Router_WithCompression 637 ns/op 0 B/op 0 allocs/op 211 ns/op 0 B/op 0 allocs/op 3.02
Benchmark_Router_Handler_CaseSensitive 531 ns/op 0 B/op 0 allocs/op 171 ns/op 0 B/op 0 allocs/op 3.11
Benchmark_Middleware_BasicAuth 862 ns/op 48 B/op 3 allocs/op 412 ns/op 48 B/op 3 allocs/op 2.09
Benchmark_Limiter 818 ns/op 8 B/op 1 allocs/op 351 ns/op 8 B/op 1 allocs/op 2.33
Benchmark_UUID/default 1152 ns/op 208 B/op 6 allocs/op 491 ns/op 208 B/op 6 allocs/op 2.35

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.