Skip to content

Commit

Permalink
Merge pull request ipfs/kubo#7677 from ipfs/feat/content-disposition-…
Browse files Browse the repository at this point in the history
…attachment

feat(gateway): Content-Disposition improvements

This commit was moved from ipfs/kubo@9b1171d
  • Loading branch information
aschmahmann authored Oct 2, 2020
2 parents 8a6f4fe + d92fd64 commit 20aa771
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion gateway/core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const (
ipnsPathPrefix = "/ipns/"
)

var onlyAscii = regexp.MustCompile("[[:^ascii:]]")

// gatewayHandler is a HTTP handler that serves IPFS objects (accessible by default at /ipfs/<path>)
// (it serves requests like GET /ipfs/QmVRzPKPzNtSrEzBFm2UZfxmPAgnaLke4DMcerbsGGSaFe/link)
type gatewayHandler struct {
Expand Down Expand Up @@ -261,7 +263,13 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
urlFilename := r.URL.Query().Get("filename")
var name string
if urlFilename != "" {
w.Header().Set("Content-Disposition", fmt.Sprintf("inline; filename*=UTF-8''%s", url.PathEscape(urlFilename)))
disposition := "inline"
if r.URL.Query().Get("download") == "true" {
disposition = "attachment"
}
utf8Name := url.PathEscape(urlFilename)
asciiName := url.PathEscape(onlyAscii.ReplaceAllLiteralString(urlFilename, "_"))
w.Header().Set("Content-Disposition", fmt.Sprintf("%s; filename=\"%s\"; filename*=UTF-8''%s", disposition, asciiName, utf8Name))
name = urlFilename
} else {
name = getFilename(urlPath)
Expand Down

0 comments on commit 20aa771

Please sign in to comment.