Skip to content

Commit d86d123

Browse files
Add mimetype mapping settings (#15133)
* Fix APK's Content-Type header * Fix case sensitive comparison * Add custom mime type mapping for downloadable files * Add documentation for MIME type mapping * Rename download.mimetype.mapping configuration to repository.mimetype_mapping Co-authored-by: zeripath <[email protected]>
1 parent 2f65c6b commit d86d123

File tree

5 files changed

+58
-1
lines changed

5 files changed

+58
-1
lines changed

custom/conf/app.example.ini

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,15 @@ PATH =
903903
;; - approved: only sign when merging an approved pr to a protected branch
904904
;MERGES = pubkey, twofa, basesigned, commitssigned
905905

906+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
907+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
908+
;[repository.mimetype_mapping]
909+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
910+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
911+
;;
912+
;; Custom MIME type mapping for downloadable files
913+
;.apk=application/vnd.android.package-archive
914+
906915
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
907916
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
908917
;[project]
@@ -912,7 +921,6 @@ PATH =
912921
;PROJECT_BOARD_BASIC_KANBAN_TYPE = To Do, In Progress, Done
913922
;PROJECT_BOARD_BUG_TRIAGE_TYPE = Needs Triage, High Priority, Low Priority, Closed
914923

915-
916924
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
917925
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
918926
;[cors]

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
143143

144144
- `LOCAL_COPY_PATH`: **tmp/local-repo**: Path for temporary local repository copies. Defaults to `tmp/local-repo`
145145

146+
## Repository - MIME type mapping (`repository.mimetype_mapping`)
147+
148+
Configuration for set the expected MIME type based on file extensions of downloadable files. Configuration presents in key-value pairs and file extensions starts with leading `.`.
149+
150+
The following configuration set `Content-Type: application/vnd.android.package-archive` header when downloading files with `.apk` file extension.
151+
```ini
152+
.apk=application/vnd.android.package-archive
153+
```
154+
146155
## CORS (`cors`)
147156

148157
- `ENABLED`: **false**: enable cors headers (disabled by default)

modules/setting/mime_type_map.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
import "strings"
8+
9+
var (
10+
// MimeTypeMap defines custom mime type mapping settings
11+
MimeTypeMap = struct {
12+
Enabled bool
13+
Map map[string]string
14+
}{
15+
Enabled: false,
16+
Map: map[string]string{},
17+
}
18+
)
19+
20+
func newMimeTypeMap() {
21+
sec := Cfg.Section("repository.mimetype_mapping")
22+
keys := sec.Keys()
23+
m := make(map[string]string, len(keys))
24+
for _, key := range keys {
25+
m[strings.ToLower(key.Name())] = key.Value()
26+
}
27+
MimeTypeMap.Map = m
28+
if len(keys) > 0 {
29+
MimeTypeMap.Enabled = true
30+
}
31+
}

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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"io"
1111
"path"
12+
"path/filepath"
1213
"strings"
1314

1415
"code.gitea.io/gitea/modules/base"
@@ -18,6 +19,7 @@ import (
1819
"code.gitea.io/gitea/modules/httpcache"
1920
"code.gitea.io/gitea/modules/lfs"
2021
"code.gitea.io/gitea/modules/log"
22+
"code.gitea.io/gitea/modules/setting"
2123
)
2224

2325
// ServeData download file from io.Reader
@@ -61,6 +63,12 @@ func ServeData(ctx *context.Context, name string, size int64, reader io.Reader)
6163
} else {
6264
ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, name))
6365
ctx.Resp.Header().Set("Access-Control-Expose-Headers", "Content-Disposition")
66+
if setting.MimeTypeMap.Enabled {
67+
fileExtension := strings.ToLower(filepath.Ext(name))
68+
if mimetype, ok := setting.MimeTypeMap.Map[fileExtension]; ok {
69+
ctx.Resp.Header().Set("Content-Type", mimetype)
70+
}
71+
}
6472
}
6573

6674
_, err = ctx.Resp.Write(buf)

0 commit comments

Comments
 (0)