Skip to content

Commit 6c2f276

Browse files
committed
Add custom mime type mapping for downloadable files
1 parent a7b42e6 commit 6c2f276

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

custom/conf/app.example.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,3 +2037,8 @@ PATH =
20372037
;;
20382038
;; Minio enabled ssl only available when STORAGE_TYPE is `minio`
20392039
;MINIO_USE_SSL = false
2040+
2041+
; Custom mime type mapping for downloadable files
2042+
;[download.mimetype.mapping]
2043+
;.apk=application/vnd.android.package-archive
2044+
;.js=application/javascript

modules/setting/mime_type_map.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package setting
6+
7+
var (
8+
// MimeTypeMap defines custom mime type mapping settings
9+
MimeTypeMap = struct {
10+
Enabled bool
11+
Map map[string]string
12+
}{
13+
Enabled: false,
14+
Map: map[string]string{},
15+
}
16+
)
17+
18+
func newMimeTypeMap() {
19+
sec := Cfg.Section("download.mimetype.mapping")
20+
keys := sec.Keys()
21+
m := make(map[string]string, len(keys))
22+
for _, key := range keys {
23+
m[key.Name()] = key.Value()
24+
}
25+
MimeTypeMap.Map = m
26+
if len(keys) > 0 {
27+
MimeTypeMap.Enabled = true
28+
}
29+
}

modules/setting/setting.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,4 +1177,5 @@ func NewServices() {
11771177
newTaskService()
11781178
NewQueueService()
11791179
newProject()
1180+
newMimeTypeMap()
11801181
}

routers/repo/download.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"code.gitea.io/gitea/modules/httpcache"
2020
"code.gitea.io/gitea/modules/lfs"
2121
"code.gitea.io/gitea/modules/log"
22+
"code.gitea.io/gitea/modules/setting"
2223
)
2324

2425
// ServeData download file from io.Reader
@@ -62,8 +63,10 @@ func ServeData(ctx *context.Context, name string, size int64, reader io.Reader)
6263
} else {
6364
ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, name))
6465
ctx.Resp.Header().Set("Access-Control-Expose-Headers", "Content-Disposition")
65-
if strings.EqualFold(".apk", filepath.Ext(name)) && base.IsZipFile(buf) {
66-
ctx.Resp.Header().Set("Content-Type", "application/vnd.android.package-archive")
66+
if setting.MimeTypeMap.Enabled {
67+
if mimetype, ok := setting.MimeTypeMap.Map[filepath.Ext(name)]; ok {
68+
ctx.Resp.Header().Set("Content-Type", mimetype)
69+
}
6770
}
6871
}
6972

0 commit comments

Comments
 (0)