mirror of
https://github.com/filebrowser/filebrowser.git
synced 2026-07-17 16:36:49 +00:00
fix: include filename in Content-Disposition header for inline downloads (#5860)
This commit is contained in:
parent
0616f6811f
commit
8f81b77cf2
2 changed files with 77 additions and 1 deletions
|
|
@ -72,7 +72,8 @@ func parseQueryAlgorithm(r *http.Request) (string, archives.Archival, error) {
|
|||
|
||||
func setContentDisposition(w http.ResponseWriter, r *http.Request, file *files.FileInfo) {
|
||||
if r.URL.Query().Get("inline") == "true" {
|
||||
w.Header().Set("Content-Disposition", "inline")
|
||||
// As per RFC6266 section 4.3
|
||||
w.Header().Set("Content-Disposition", "inline; filename*=utf-8''"+url.PathEscape(file.Name))
|
||||
} else {
|
||||
// As per RFC6266 section 4.3
|
||||
w.Header().Set("Content-Disposition", "attachment; filename*=utf-8''"+url.PathEscape(file.Name))
|
||||
|
|
|
|||
75
http/raw_test.go
Normal file
75
http/raw_test.go
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
package fbhttp
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"github.com/filebrowser/filebrowser/v2/files"
|
||||
)
|
||||
|
||||
func TestSetContentDisposition(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
testCases := map[string]struct {
|
||||
filename string
|
||||
inline bool
|
||||
expected string
|
||||
}{
|
||||
"inline simple filename": {
|
||||
filename: "document.pdf",
|
||||
inline: true,
|
||||
expected: "inline; filename*=utf-8''" + url.PathEscape("document.pdf"),
|
||||
},
|
||||
"attachment simple filename": {
|
||||
filename: "document.pdf",
|
||||
inline: false,
|
||||
expected: "attachment; filename*=utf-8''" + url.PathEscape("document.pdf"),
|
||||
},
|
||||
"inline non-ASCII filename": {
|
||||
filename: "日本語.txt",
|
||||
inline: true,
|
||||
expected: "inline; filename*=utf-8''" + url.PathEscape("日本語.txt"),
|
||||
},
|
||||
"attachment non-ASCII filename": {
|
||||
filename: "日本語.txt",
|
||||
inline: false,
|
||||
expected: "attachment; filename*=utf-8''" + url.PathEscape("日本語.txt"),
|
||||
},
|
||||
"inline filename with spaces": {
|
||||
filename: "my file.txt",
|
||||
inline: true,
|
||||
expected: "inline; filename*=utf-8''" + url.PathEscape("my file.txt"),
|
||||
},
|
||||
"attachment filename with spaces": {
|
||||
filename: "my file.txt",
|
||||
inline: false,
|
||||
expected: "attachment; filename*=utf-8''" + url.PathEscape("my file.txt"),
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
req, err := http.NewRequest(http.MethodGet, "/test", http.NoBody)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create request: %v", err)
|
||||
}
|
||||
if tc.inline {
|
||||
req.URL.RawQuery = "inline=true"
|
||||
}
|
||||
|
||||
file := &files.FileInfo{Name: tc.filename}
|
||||
|
||||
setContentDisposition(recorder, req, file)
|
||||
|
||||
got := recorder.Header().Get("Content-Disposition")
|
||||
if got != tc.expected {
|
||||
t.Errorf("Content-Disposition = %q, want %q", got, tc.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue