Skip to content

Commit

Permalink
Add documentation for MIME type mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
szatyinadam committed May 10, 2021
1 parent 6c2f276 commit f346b3f
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 13 deletions.
9 changes: 9 additions & 0 deletions docs/content/doc/advanced/config-cheat-sheet.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,15 @@ MINIO_USE_SSL = false

And used by `[attachment]`, `[lfs]` and etc. as `STORAGE_TYPE`.

## MIME type mapping (`download.mimetype.mapping`)

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 `.`.

The following configuration set `Content-Type: application/javascript` header when downloading files with `.js` file extension.
```ini
.js=application/javascript
```

## Other (`other`)

- `SHOW_FOOTER_BRANDING`: **false**: Show Gitea branding in the footer.
Expand Down
5 changes: 0 additions & 5 deletions modules/base/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,6 @@ func IsAudioFile(data []byte) bool {
return strings.Contains(DetectContentType(data), "audio/")
}

// IsZipFile detects if data is a zip format
func IsZipFile(data []byte) bool {
return strings.Contains(DetectContentType(data), "application/zip")
}

// EntryIcon returns the octicon class for displaying files/directories
func EntryIcon(entry *git.TreeEntry) string {
switch {
Expand Down
6 changes: 0 additions & 6 deletions modules/base/tool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,6 @@ func TestIsAudioFile(t *testing.T) {
assert.False(t, IsAudioFile([]byte("plain text")))
}

func TestIsZipFile(t *testing.T) {
zip, _ := base64.StdEncoding.DecodeString("UEsDBAoAAAAAANG0d1IAAAAAAAAAAAAAAAAIABwAdGV4dC50eHRVVAkAA9pfWmDaX1pgdXgLAAEE9QEAAAQUAAAAUEsBAh4DCgAAAAAA0bR3UgAAAAAAAAAAAAAAAAgAGAAAAAAAAAAAAKSBAAAAAHRleHQudHh0VVQFAAPaX1pgdXgLAAEE9QEAAAQUAAAAUEsFBgAAAAABAAEATgAAAEIAAAAAAA==")
assert.True(t, IsZipFile(zip))
assert.False(t, IsZipFile([]byte("plain text")))
}

// TODO: Test EntryIcon

func TestSetupGiteaRoot(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion modules/setting/mime_type_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package setting

import "strings"

var (
// MimeTypeMap defines custom mime type mapping settings
MimeTypeMap = struct {
Expand All @@ -20,7 +22,7 @@ func newMimeTypeMap() {
keys := sec.Keys()
m := make(map[string]string, len(keys))
for _, key := range keys {
m[key.Name()] = key.Value()
m[strings.ToLower(key.Name())] = key.Value()
}
MimeTypeMap.Map = m
if len(keys) > 0 {
Expand Down
3 changes: 2 additions & 1 deletion routers/repo/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ func ServeData(ctx *context.Context, name string, size int64, reader io.Reader)
ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, name))
ctx.Resp.Header().Set("Access-Control-Expose-Headers", "Content-Disposition")
if setting.MimeTypeMap.Enabled {
if mimetype, ok := setting.MimeTypeMap.Map[filepath.Ext(name)]; ok {
fileExtension := strings.ToLower(filepath.Ext(name))
if mimetype, ok := setting.MimeTypeMap.Map[fileExtension]; ok {
ctx.Resp.Header().Set("Content-Type", mimetype)
}
}
Expand Down

0 comments on commit f346b3f

Please sign in to comment.