Skip to content

Commit 6f1de0a

Browse files
Add avif image file support (#32508)
Most modern browsers support it now ` Update ALLOWED_TYPES #96 ` https://gitea.com/gitea/docs/pulls/96 --------- Co-authored-by: silverwind <[email protected]>
1 parent 68731c0 commit 6f1de0a

File tree

7 files changed

+81
-25
lines changed

7 files changed

+81
-25
lines changed

Diff for: custom/conf/app.example.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -1912,7 +1912,7 @@ LEVEL = Info
19121912
;ENABLED = true
19131913
;;
19141914
;; Comma-separated list of allowed file extensions (`.zip`), mime types (`text/plain`) or wildcard type (`image/*`, `audio/*`, `video/*`). Empty value or `*/*` allows all types.
1915-
;ALLOWED_TYPES = .csv,.docx,.fodg,.fodp,.fods,.fodt,.gif,.gz,.jpeg,.jpg,.log,.md,.mov,.mp4,.odf,.odg,.odp,.ods,.odt,.patch,.pdf,.png,.pptx,.svg,.tgz,.txt,.webm,.xls,.xlsx,.zip
1915+
;ALLOWED_TYPES = .avif,.cpuprofile,.csv,.dmp,.docx,.fodg,.fodp,.fods,.fodt,.gif,.gz,.jpeg,.jpg,.json,.jsonc,.log,.md,.mov,.mp4,.odf,.odg,.odp,.ods,.odt,.patch,.pdf,.png,.pptx,.svg,.tgz,.txt,.webm,.webp,.xls,.xlsx,.zip
19161916
;;
19171917
;; Max size of each file. Defaults to 2048MB
19181918
;MAX_SIZE = 2048

Diff for: modules/httplib/serve.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func ServeSetHeaders(w http.ResponseWriter, opts *ServeHeaderOptions) {
4646
w.Header().Add(gzhttp.HeaderNoCompression, "1")
4747
}
4848

49-
contentType := typesniffer.ApplicationOctetStream
49+
contentType := typesniffer.MimeTypeApplicationOctetStream
5050
if opts.ContentType != "" {
5151
if opts.ContentTypeCharset != "" {
5252
contentType = opts.ContentType + "; charset=" + strings.ToLower(opts.ContentTypeCharset)
@@ -107,7 +107,7 @@ func setServeHeadersByFile(r *http.Request, w http.ResponseWriter, filePath stri
107107
} else if isPlain {
108108
opts.ContentType = "text/plain"
109109
} else {
110-
opts.ContentType = typesniffer.ApplicationOctetStream
110+
opts.ContentType = typesniffer.MimeTypeApplicationOctetStream
111111
}
112112
}
113113

Diff for: modules/setting/attachment.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,33 @@
33

44
package setting
55

6-
// Attachment settings
7-
var Attachment = struct {
6+
type AttachmentSettingType struct {
87
Storage *Storage
98
AllowedTypes string
109
MaxSize int64
1110
MaxFiles int
1211
Enabled bool
13-
}{
14-
Storage: &Storage{},
15-
AllowedTypes: ".cpuprofile,.csv,.dmp,.docx,.fodg,.fodp,.fods,.fodt,.gif,.gz,.jpeg,.jpg,.json,.jsonc,.log,.md,.mov,.mp4,.odf,.odg,.odp,.ods,.odt,.patch,.pdf,.png,.pptx,.svg,.tgz,.txt,.webm,.xls,.xlsx,.zip",
16-
MaxSize: 2048,
17-
MaxFiles: 5,
18-
Enabled: true,
1912
}
2013

14+
var Attachment AttachmentSettingType
15+
2116
func loadAttachmentFrom(rootCfg ConfigProvider) (err error) {
17+
Attachment = AttachmentSettingType{
18+
AllowedTypes: ".avif,.cpuprofile,.csv,.dmp,.docx,.fodg,.fodp,.fods,.fodt,.gif,.gz,.jpeg,.jpg,.json,.jsonc,.log,.md,.mov,.mp4,.odf,.odg,.odp,.ods,.odt,.patch,.pdf,.png,.pptx,.svg,.tgz,.txt,.webm,.webp,.xls,.xlsx,.zip",
19+
MaxSize: 2048,
20+
MaxFiles: 5,
21+
Enabled: true,
22+
}
2223
sec, _ := rootCfg.GetSection("attachment")
2324
if sec == nil {
2425
Attachment.Storage, err = getStorage(rootCfg, "attachments", "", nil)
2526
return err
2627
}
2728

28-
Attachment.AllowedTypes = sec.Key("ALLOWED_TYPES").MustString(".cpuprofile,.csv,.dmp,.docx,.fodg,.fodp,.fods,.fodt,.gif,.gz,.jpeg,.jpg,.json,.jsonc,.log,.md,.mov,.mp4,.odf,.odg,.odp,.ods,.odt,.patch,.pdf,.png,.pptx,.svg,.tgz,.txt,.webm,.xls,.xlsx,.zip")
29-
Attachment.MaxSize = sec.Key("MAX_SIZE").MustInt64(2048)
30-
Attachment.MaxFiles = sec.Key("MAX_FILES").MustInt(5)
31-
Attachment.Enabled = sec.Key("ENABLED").MustBool(true)
32-
29+
Attachment.AllowedTypes = sec.Key("ALLOWED_TYPES").MustString(Attachment.AllowedTypes)
30+
Attachment.MaxSize = sec.Key("MAX_SIZE").MustInt64(Attachment.MaxSize)
31+
Attachment.MaxFiles = sec.Key("MAX_FILES").MustInt(Attachment.MaxFiles)
32+
Attachment.Enabled = sec.Key("ENABLED").MustBool(Attachment.Enabled)
3333
Attachment.Storage, err = getStorage(rootCfg, "attachments", "", sec)
3434
return err
3535
}

Diff for: modules/typesniffer/typesniffer.go

+33-7
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ package typesniffer
55

66
import (
77
"bytes"
8+
"encoding/binary"
89
"fmt"
910
"io"
1011
"net/http"
1112
"regexp"
13+
"slices"
1214
"strings"
1315

1416
"code.gitea.io/gitea/modules/util"
@@ -18,10 +20,10 @@ import (
1820
const sniffLen = 1024
1921

2022
const (
21-
// SvgMimeType MIME type of SVG images.
22-
SvgMimeType = "image/svg+xml"
23-
// ApplicationOctetStream MIME type of binary files.
24-
ApplicationOctetStream = "application/octet-stream"
23+
MimeTypeImageSvg = "image/svg+xml"
24+
MimeTypeImageAvif = "image/avif"
25+
26+
MimeTypeApplicationOctetStream = "application/octet-stream"
2527
)
2628

2729
var (
@@ -47,7 +49,7 @@ func (ct SniffedType) IsImage() bool {
4749

4850
// IsSvgImage detects if data is an SVG image format
4951
func (ct SniffedType) IsSvgImage() bool {
50-
return strings.Contains(ct.contentType, SvgMimeType)
52+
return strings.Contains(ct.contentType, MimeTypeImageSvg)
5153
}
5254

5355
// IsPDF detects if data is a PDF format
@@ -81,6 +83,26 @@ func (ct SniffedType) GetMimeType() string {
8183
return strings.SplitN(ct.contentType, ";", 2)[0]
8284
}
8385

86+
// https://en.wikipedia.org/wiki/ISO_base_media_file_format#File_type_box
87+
func detectFileTypeBox(data []byte) (brands []string, found bool) {
88+
if len(data) < 12 {
89+
return nil, false
90+
}
91+
boxSize := int(binary.BigEndian.Uint32(data[:4]))
92+
if boxSize < 12 || boxSize > len(data) {
93+
return nil, false
94+
}
95+
tag := string(data[4:8])
96+
if tag != "ftyp" {
97+
return nil, false
98+
}
99+
brands = append(brands, string(data[8:12]))
100+
for i := 16; i+4 <= boxSize; i += 4 {
101+
brands = append(brands, string(data[i:i+4]))
102+
}
103+
return brands, true
104+
}
105+
84106
// DetectContentType extends http.DetectContentType with more content types. Defaults to text/unknown if input is empty.
85107
func DetectContentType(data []byte) SniffedType {
86108
if len(data) == 0 {
@@ -94,15 +116,14 @@ func DetectContentType(data []byte) SniffedType {
94116
}
95117

96118
// SVG is unsupported by http.DetectContentType, https://github.com/golang/go/issues/15888
97-
98119
detectByHTML := strings.Contains(ct, "text/plain") || strings.Contains(ct, "text/html")
99120
detectByXML := strings.Contains(ct, "text/xml")
100121
if detectByHTML || detectByXML {
101122
dataProcessed := svgComment.ReplaceAll(data, nil)
102123
dataProcessed = bytes.TrimSpace(dataProcessed)
103124
if detectByHTML && svgTagRegex.Match(dataProcessed) ||
104125
detectByXML && svgTagInXMLRegex.Match(dataProcessed) {
105-
ct = SvgMimeType
126+
ct = MimeTypeImageSvg
106127
}
107128
}
108129

@@ -116,6 +137,11 @@ func DetectContentType(data []byte) SniffedType {
116137
}
117138
}
118139

140+
fileTypeBrands, found := detectFileTypeBox(data)
141+
if found && slices.Contains(fileTypeBrands, "avif") {
142+
ct = MimeTypeImageAvif
143+
}
144+
119145
if ct == "application/ogg" {
120146
dataHead := data
121147
if len(dataHead) > 256 {

Diff for: modules/typesniffer/typesniffer_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,33 @@ func TestDetectContentTypeOgg(t *testing.T) {
134134
assert.NoError(t, err)
135135
assert.True(t, st.IsVideo())
136136
}
137+
138+
func TestDetectFileTypeBox(t *testing.T) {
139+
_, found := detectFileTypeBox([]byte("\x00\x00\xff\xffftypAAAA...."))
140+
assert.False(t, found)
141+
142+
brands, found := detectFileTypeBox([]byte("\x00\x00\x00\x0cftypAAAA"))
143+
assert.True(t, found)
144+
assert.Equal(t, []string{"AAAA"}, brands)
145+
146+
brands, found = detectFileTypeBox([]byte("\x00\x00\x00\x10ftypAAAA....BBBB"))
147+
assert.True(t, found)
148+
assert.Equal(t, []string{"AAAA"}, brands)
149+
150+
brands, found = detectFileTypeBox([]byte("\x00\x00\x00\x14ftypAAAA....BBBB"))
151+
assert.True(t, found)
152+
assert.Equal(t, []string{"AAAA", "BBBB"}, brands)
153+
154+
_, found = detectFileTypeBox([]byte("\x00\x00\x00\x14ftypAAAA....BBB"))
155+
assert.False(t, found)
156+
157+
brands, found = detectFileTypeBox([]byte("\x00\x00\x00\x13ftypAAAA....BBB"))
158+
assert.True(t, found)
159+
assert.Equal(t, []string{"AAAA"}, brands)
160+
}
161+
162+
func TestDetectContentTypeAvif(t *testing.T) {
163+
buf := []byte("\x00\x00\x00\x20ftypavif.......................")
164+
st := DetectContentType(buf)
165+
assert.Equal(t, MimeTypeImageAvif, st.contentType)
166+
}

Diff for: web_src/js/utils.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ test('encodeURLEncodedBase64, decodeURLEncodedBase64', () => {
118118
});
119119

120120
test('file detection', () => {
121-
for (const name of ['a.jpg', '/a.jpeg', '.file.png', '.webp', 'file.svg']) {
121+
for (const name of ['a.avif', 'a.jpg', '/a.jpeg', '.file.png', '.webp', 'file.svg']) {
122122
expect(isImageFile({name})).toBeTruthy();
123123
}
124124
for (const name of ['', 'a.jpg.x', '/path.png/x', 'webp']) {

Diff for: web_src/js/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export function sleep(ms: number): Promise<void> {
165165
}
166166

167167
export function isImageFile({name, type}: {name: string, type?: string}): boolean {
168-
return /\.(jpe?g|png|gif|webp|svg|heic)$/i.test(name || '') || type?.startsWith('image/');
168+
return /\.(avif|jpe?g|png|gif|webp|svg|heic)$/i.test(name || '') || type?.startsWith('image/');
169169
}
170170

171171
export function isVideoFile({name, type}: {name: string, type?: string}): boolean {

0 commit comments

Comments
 (0)