summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJimmy Praet <jimmy.praet@telenet.be>2021-06-30 23:31:54 +0200
committerGitHub <noreply@github.com>2021-06-30 17:31:54 -0400
commitce286f9d9c00ceb24fb4eab4cab56c0b0678765a (patch)
treee8ef0385b108332f63bb744e45fe2ad62bb5a3cf
parent99799832835aae6a8641112cb71eb87baef32afa (diff)
downloadgitea-ce286f9d9c00ceb24fb4eab4cab56c0b0678765a.tar.gz
gitea-ce286f9d9c00ceb24fb4eab4cab56c0b0678765a.zip
Support custom mime type mapping for text files (#16304)
* Support custom mime type mapping for text files * Apply suggested change to routers/common/repo.go Co-authored-by: KN4CK3R <admin@oldschoolhack.me> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
-rw-r--r--integrations/download_test.go28
-rw-r--r--integrations/gitea-repositories-meta/user2/repo2.git/objects/10/32bbf17fbc0d9c95bb5418dabe8f8c992787002
-rw-r--r--integrations/gitea-repositories-meta/user2/repo2.git/objects/26/f842bcad37fa40a1bb34cbb5ee219ee35d863dbin0 -> 75 bytes
-rw-r--r--integrations/gitea-repositories-meta/user2/repo2.git/objects/ba/1aed4e2ea2443d76cec241b96be4ec990852ecbin0 -> 117 bytes
-rw-r--r--integrations/gitea-repositories-meta/user2/repo2.git/refs/heads/master2
-rw-r--r--routers/common/repo.go20
6 files changed, 43 insertions, 9 deletions
diff --git a/integrations/download_test.go b/integrations/download_test.go
index 305155e9ac..38de75f476 100644
--- a/integrations/download_test.go
+++ b/integrations/download_test.go
@@ -8,6 +8,7 @@ import (
"net/http"
"testing"
+ "code.gitea.io/gitea/modules/setting"
"github.com/stretchr/testify/assert"
)
@@ -62,3 +63,30 @@ func TestDownloadByIDMediaForSVGUsesSecureHeaders(t *testing.T) {
assert.Equal(t, "image/svg+xml", resp.HeaderMap.Get("Content-Type"))
assert.Equal(t, "nosniff", resp.HeaderMap.Get("X-Content-Type-Options"))
}
+
+func TestDownloadRawTextFileWithoutMimeTypeMapping(t *testing.T) {
+ defer prepareTestEnv(t)()
+
+ session := loginUser(t, "user2")
+
+ req := NewRequest(t, "GET", "/user2/repo2/raw/branch/master/test.xml")
+ resp := session.MakeRequest(t, req, http.StatusOK)
+
+ assert.Equal(t, "text/plain; charset=utf-8", resp.HeaderMap.Get("Content-Type"))
+}
+
+func TestDownloadRawTextFileWithMimeTypeMapping(t *testing.T) {
+ defer prepareTestEnv(t)()
+ setting.MimeTypeMap.Map[".xml"] = "text/xml"
+ setting.MimeTypeMap.Enabled = true
+
+ session := loginUser(t, "user2")
+
+ req := NewRequest(t, "GET", "/user2/repo2/raw/branch/master/test.xml")
+ resp := session.MakeRequest(t, req, http.StatusOK)
+
+ assert.Equal(t, "text/xml; charset=utf-8", resp.HeaderMap.Get("Content-Type"))
+
+ delete(setting.MimeTypeMap.Map, ".xml")
+ setting.MimeTypeMap.Enabled = false
+}
diff --git a/integrations/gitea-repositories-meta/user2/repo2.git/objects/10/32bbf17fbc0d9c95bb5418dabe8f8c99278700 b/integrations/gitea-repositories-meta/user2/repo2.git/objects/10/32bbf17fbc0d9c95bb5418dabe8f8c99278700
new file mode 100644
index 0000000000..736e40878e
--- /dev/null
+++ b/integrations/gitea-repositories-meta/user2/repo2.git/objects/10/32bbf17fbc0d9c95bb5418dabe8f8c99278700
@@ -0,0 +1,2 @@
+xK
+0Eg %":u􊕦J|p˭Q~% 9لG6G ͦw(E4}*{)`YƆleMJOܚ>%^ݿL!]N[v#E6U~/0 ZU'gpJ5 \ No newline at end of file
diff --git a/integrations/gitea-repositories-meta/user2/repo2.git/objects/26/f842bcad37fa40a1bb34cbb5ee219ee35d863d b/integrations/gitea-repositories-meta/user2/repo2.git/objects/26/f842bcad37fa40a1bb34cbb5ee219ee35d863d
new file mode 100644
index 0000000000..c3e7e778c5
--- /dev/null
+++ b/integrations/gitea-repositories-meta/user2/repo2.git/objects/26/f842bcad37fa40a1bb34cbb5ee219ee35d863d
Binary files differ
diff --git a/integrations/gitea-repositories-meta/user2/repo2.git/objects/ba/1aed4e2ea2443d76cec241b96be4ec990852ec b/integrations/gitea-repositories-meta/user2/repo2.git/objects/ba/1aed4e2ea2443d76cec241b96be4ec990852ec
new file mode 100644
index 0000000000..add9a3af0d
--- /dev/null
+++ b/integrations/gitea-repositories-meta/user2/repo2.git/objects/ba/1aed4e2ea2443d76cec241b96be4ec990852ec
Binary files differ
diff --git a/integrations/gitea-repositories-meta/user2/repo2.git/refs/heads/master b/integrations/gitea-repositories-meta/user2/repo2.git/refs/heads/master
index 10967a9b8a..334d09ca02 100644
--- a/integrations/gitea-repositories-meta/user2/repo2.git/refs/heads/master
+++ b/integrations/gitea-repositories-meta/user2/repo2.git/refs/heads/master
@@ -1 +1 @@
-205ac761f3326a7ebe416e8673760016450b5cec
+1032bbf17fbc0d9c95bb5418dabe8f8c99278700
diff --git a/routers/common/repo.go b/routers/common/repo.go
index 22403da097..8d33fb07fb 100644
--- a/routers/common/repo.go
+++ b/routers/common/repo.go
@@ -64,16 +64,26 @@ func ServeData(ctx *context.Context, name string, size int64, reader io.Reader)
st := typesniffer.DetectContentType(buf)
+ mappedMimeType := ""
+ if setting.MimeTypeMap.Enabled {
+ fileExtension := strings.ToLower(filepath.Ext(name))
+ mappedMimeType = setting.MimeTypeMap.Map[fileExtension]
+ }
if st.IsText() || ctx.QueryBool("render") {
cs, err := charset.DetectEncoding(buf)
if err != nil {
log.Error("Detect raw file %s charset failed: %v, using by default utf-8", name, err)
cs = "utf-8"
}
- ctx.Resp.Header().Set("Content-Type", "text/plain; charset="+strings.ToLower(cs))
+ if mappedMimeType == "" {
+ mappedMimeType = "text/plain"
+ }
+ ctx.Resp.Header().Set("Content-Type", mappedMimeType+"; charset="+strings.ToLower(cs))
} else {
ctx.Resp.Header().Set("Access-Control-Expose-Headers", "Content-Disposition")
-
+ if mappedMimeType != "" {
+ ctx.Resp.Header().Set("Content-Type", mappedMimeType)
+ }
if (st.IsImage() || st.IsPDF()) && (setting.UI.SVG.Enabled || !st.IsSvgImage()) {
ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf(`inline; filename="%s"`, name))
if st.IsSvgImage() {
@@ -83,12 +93,6 @@ func ServeData(ctx *context.Context, name string, size int64, reader io.Reader)
}
} else {
ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, name))
- if setting.MimeTypeMap.Enabled {
- fileExtension := strings.ToLower(filepath.Ext(name))
- if mimetype, ok := setting.MimeTypeMap.Map[fileExtension]; ok {
- ctx.Resp.Header().Set("Content-Type", mimetype)
- }
- }
}
}