File tree 2 files changed +25
-1
lines changed
2 files changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -71,7 +71,7 @@ func (ct SniffedType) IsRepresentableAsText() bool {
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 IsBrowsableType 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,6 +116,17 @@ func DetectContentType(data []byte) SniffedType {
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
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ package typesniffer
6
6
import (
7
7
"bytes"
8
8
"encoding/base64"
9
+ "encoding/hex"
9
10
"strings"
10
11
"testing"
11
12
@@ -121,3 +122,15 @@ func TestDetectContentTypeFromReader(t *testing.T) {
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
+ }
You can’t perform that action at this time.
0 commit comments