diff options
author | silverwind <me@silverwind.io> | 2022-07-29 17:26:55 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-29 17:26:55 +0200 |
commit | eaf653f843cb887bc2f4ff664010b4e969dd5446 (patch) | |
tree | b331347455827acc03aacb2e9f8aa110ab78ba0f /modules | |
parent | 7fe77f0167b264c7a8bcfab05385594b6c8141c8 (diff) | |
download | gitea-eaf653f843cb887bc2f4ff664010b4e969dd5446.tar.gz gitea-eaf653f843cb887bc2f4ff664010b4e969dd5446.zip |
Rework raw file http header logic (#20484)
- Always respect the user's configured mime type map
- Allow more types like image/pdf/video/audio to serve with correct content-type
- Shorten cache duration of raw files to 5 minutes, matching GitHub
- Don't set `content-disposition: attachment`, let the browser decide whether it wants to download or display a file directly
- Implement rfc5987 for filenames, remove previous hack. Confirmed it working in Safari.
- Make PDF attachment work in Safari by removing `sandbox` attribute.
This change will make a lot more file types open directly in browser now. Logic should generally be more readable than before with less `if` nesting and such.
Replaces: https://github.com/go-gitea/gitea/pull/20460
Replaces: https://github.com/go-gitea/gitea/pull/20455
Fixes: https://github.com/go-gitea/gitea/issues/20404
Diffstat (limited to 'modules')
-rw-r--r-- | modules/typesniffer/typesniffer.go | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/modules/typesniffer/typesniffer.go b/modules/typesniffer/typesniffer.go index b6a6646d50..e50928e8c2 100644 --- a/modules/typesniffer/typesniffer.go +++ b/modules/typesniffer/typesniffer.go @@ -70,6 +70,16 @@ func (ct SniffedType) IsRepresentableAsText() bool { return ct.IsText() || ct.IsSvgImage() } +// IsBrowsableType returns whether a non-text type can be displayed in a browser +func (ct SniffedType) IsBrowsableBinaryType() bool { + return ct.IsImage() || ct.IsSvgImage() || ct.IsPDF() || ct.IsVideo() || ct.IsAudio() +} + +// GetMimeType returns the mime type +func (ct SniffedType) GetMimeType() string { + return strings.SplitN(ct.contentType, ";", 2)[0] +} + // DetectContentType extends http.DetectContentType with more content types. Defaults to text/unknown if input is empty. func DetectContentType(data []byte) SniffedType { if len(data) == 0 { |