@@ -6,19 +6,14 @@ package typesniffer
66import (
77 "bytes"
88 "encoding/binary"
9- "fmt"
10- "io"
119 "net/http"
1210 "regexp"
1311 "slices"
1412 "strings"
1513 "sync"
16-
17- "code.gitea.io/gitea/modules/util"
1814)
1915
20- // Use at most this many bytes to determine Content Type.
21- const sniffLen = 1024
16+ const SniffContentSize = 1024
2217
2318const (
2419 MimeTypeImageSvg = "image/svg+xml"
@@ -42,7 +37,7 @@ type SniffedType struct {
4237 contentType string
4338}
4439
45- // IsText detects if the content format is plain text.
40+ // IsText detects if the content format is text family, including text/ plain, text/html, text/css, etc .
4641func (ct SniffedType ) IsText () bool {
4742 return strings .Contains (ct .contentType , "text/" )
4843}
@@ -66,12 +61,12 @@ func (ct SniffedType) IsPDF() bool {
6661 return strings .Contains (ct .contentType , "application/pdf" )
6762}
6863
69- // IsVideo detects if data is an video format
64+ // IsVideo detects if data is a video format
7065func (ct SniffedType ) IsVideo () bool {
7166 return strings .Contains (ct .contentType , "video/" )
7267}
7368
74- // IsAudio detects if data is an video format
69+ // IsAudio detects if data is a video format
7570func (ct SniffedType ) IsAudio () bool {
7671 return strings .Contains (ct .contentType , "audio/" )
7772}
@@ -87,10 +82,6 @@ func (ct SniffedType) IsBrowsableBinaryType() bool {
8782 return ct .IsImage () || ct .IsSvgImage () || ct .IsPDF () || ct .IsVideo () || ct .IsAudio ()
8883}
8984
90- func (ct SniffedType ) IsApplicationOctetStream () bool {
91- return ct .contentType == "application/octet-stream"
92- }
93-
9485// GetMimeType returns the mime type
9586func (ct SniffedType ) GetMimeType () string {
9687 return strings .SplitN (ct .contentType , ";" , 2 )[0 ]
@@ -116,16 +107,16 @@ func detectFileTypeBox(data []byte) (brands []string, found bool) {
116107 return brands , true
117108}
118109
119- // DetectContentType extends http.DetectContentType with more content types. Defaults to text/unknown if input is empty.
110+ // DetectContentType extends http.DetectContentType with more content types. Defaults to text/plain if input is empty.
120111func DetectContentType (data []byte ) SniffedType {
121112 if len (data ) == 0 {
122- return SniffedType {"text/unknown " }
113+ return SniffedType {"text/plain " }
123114 }
124115
125116 ct := http .DetectContentType (data )
126117
127- if len (data ) > sniffLen {
128- data = data [:sniffLen ]
118+ if len (data ) > SniffContentSize {
119+ data = data [:SniffContentSize ]
129120 }
130121
131122 vars := globalVars ()
@@ -143,7 +134,7 @@ func DetectContentType(data []byte) SniffedType {
143134
144135 if strings .HasPrefix (ct , "audio/" ) && bytes .HasPrefix (data , []byte ("ID3" )) {
145136 // The MP3 detection is quite inaccurate, any content with "ID3" prefix will result in "audio/mpeg".
146- // So remove the "ID3" prefix and detect again, if result is text, then it must be text content.
137+ // So remove the "ID3" prefix and detect again, then if the result is " text", it must be text content.
147138 // This works especially because audio files contain many unprintable/invalid characters like `0x00`
148139 ct2 := http .DetectContentType (data [3 :])
149140 if strings .HasPrefix (ct2 , "text/" ) {
@@ -169,15 +160,3 @@ func DetectContentType(data []byte) SniffedType {
169160 }
170161 return SniffedType {ct }
171162}
172-
173- // DetectContentTypeFromReader guesses the content type contained in the reader.
174- func DetectContentTypeFromReader (r io.Reader ) (SniffedType , error ) {
175- buf := make ([]byte , sniffLen )
176- n , err := util .ReadAtMost (r , buf )
177- if err != nil {
178- return SniffedType {}, fmt .Errorf ("DetectContentTypeFromReader io error: %w" , err )
179- }
180- buf = buf [:n ]
181-
182- return DetectContentType (buf ), nil
183- }
0 commit comments