loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Detect ogg mime-type as audio or video (#26494)

"ogg" is just a "container" format for audio and video.

Golang's `DetectContentType` only reports "application/ogg" for
potential ogg files.

Actually it could do more "guess" to see whether it is a audio file or a
video file.

authored by

wxiaoguang and committed by
GitHub
ced34bab c91a7e8d

+25 -1
+12 -1
modules/typesniffer/typesniffer.go
··· 71 71 return ct.IsText() || ct.IsSvgImage() 72 72 } 73 73 74 - // IsBrowsableType returns whether a non-text type can be displayed in a browser 74 + // IsBrowsableBinaryType returns whether a non-text type can be displayed in a browser 75 75 func (ct SniffedType) IsBrowsableBinaryType() bool { 76 76 return ct.IsImage() || ct.IsSvgImage() || ct.IsPDF() || ct.IsVideo() || ct.IsAudio() 77 77 } ··· 116 116 } 117 117 } 118 118 119 + if ct == "application/ogg" { 120 + dataHead := data 121 + if len(dataHead) > 256 { 122 + dataHead = dataHead[:256] // only need to do a quick check for the file header 123 + } 124 + if bytes.Contains(dataHead, []byte("theora")) || bytes.Contains(dataHead, []byte("dirac")) { 125 + ct = "video/ogg" // ogg is only used for some video formats, and it's not popular 126 + } else { 127 + ct = "audio/ogg" // for most cases, it is used as an audio container 128 + } 129 + } 119 130 return SniffedType{ct} 120 131 } 121 132
+13
modules/typesniffer/typesniffer_test.go
··· 6 6 import ( 7 7 "bytes" 8 8 "encoding/base64" 9 + "encoding/hex" 9 10 "strings" 10 11 "testing" 11 12 ··· 121 122 assert.NoError(t, err) 122 123 assert.True(t, st.IsAudio()) 123 124 } 125 + 126 + func TestDetectContentTypeOgg(t *testing.T) { 127 + oggAudio, _ := hex.DecodeString("4f67675300020000000000000000352f0000000000007dc39163011e01766f72626973000000000244ac0000000000000071020000000000b8014f6767530000") 128 + st, err := DetectContentTypeFromReader(bytes.NewReader(oggAudio)) 129 + assert.NoError(t, err) 130 + assert.True(t, st.IsAudio()) 131 + 132 + oggVideo, _ := hex.DecodeString("4f676753000200000000000000007d9747ef000000009b59daf3012a807468656f7261030201001e00110001e000010e00020000001e00000001000001000001") 133 + st, err = DetectContentTypeFromReader(bytes.NewReader(oggVideo)) 134 + assert.NoError(t, err) 135 + assert.True(t, st.IsVideo()) 136 + }