-
Notifications
You must be signed in to change notification settings - Fork 0
/
compressible.go
47 lines (36 loc) · 1.08 KB
/
compressible.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package compressible
import (
"mime"
"regexp"
"github.com/GitbookIO/mimedb"
)
// Version is this package's version
const Version = "0.3.0"
var compressibleTypeRegExp = regexp.MustCompile(`(?i)^text\/|\+json$|\+text$|\+xml$`)
// Test checks whether the given contentType is compressible, using
// https://github.com/GitbookIO/mimedb as mime database.Recommand to autoload
// all extensions and their associated content type by
// `import _ "github.com/GitbookIO/mimedb/autoload"` first. All types that not
// in mimedb but have the scheme of "text/*", "*/*+json", "*/*+text" and
// "*/*+xml" are considered as compressible.
func Test(contentType string) bool {
dbMatched := false
exts, err := mime.ExtensionsByType(contentType)
if err != nil {
return false
}
for _, ext := range exts {
// all exts returned by mime.ExtensionsByType are always
// start with "."
if entry, ok := mimedb.DB[ext[1:]]; ok {
dbMatched = true
if entry.Compressible {
return true
}
}
}
if !dbMatched && compressibleTypeRegExp.MatchString(contentType) {
return true
}
return false
}