-
Notifications
You must be signed in to change notification settings - Fork 0
/
classifier.go
74 lines (65 loc) · 1.54 KB
/
classifier.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package cbsr
import (
"mime"
"github.com/renthraysk/encoding"
)
type Classifier interface {
ContentTypeFromExt(ext string) string
ContentEncodingFromExt(ext string) (encoding.Encoding, bool)
}
type defaultClassifier struct{}
func (defaultClassifier) ContentTypeFromExt(ext string) string {
switch ext {
case ".avif":
return "image/avif"
case ".css":
return "text/css; charset=utf-8"
case ".gif":
return "image/gif"
case ".htm", ".html":
return "text/html; charset=utf-8"
case ".jpg", ".jpeg":
return "image/jpeg"
case ".js", ".mjs":
return "text/javascript; charset=utf-8"
case ".json":
return "application/json; charset=utf-8"
case ".pdf":
return "application/pdf"
case ".png":
return "image/png"
case ".svg":
return "image/svg+xml; charset=utf-8"
case ".txt":
return "text/plain; charset=utf-8"
case ".wasm":
return "application/wasm"
case ".webp":
return "image/webp"
case ".xml":
return "application/xml; charset=utf-8"
}
return ""
}
func (defaultClassifier) ContentEncodingFromExt(ext string) (encoding.Encoding, bool) {
switch ext {
case ".br":
return encoding.Brotli, true
case ".gz":
return encoding.Gzip, true
}
return encoding.Identity, false
}
type mimeClassifier struct{}
func (mimeClassifier) ContentTypeFromExt(ext string) string {
return mime.TypeByExtension(ext)
}
func (mimeClassifier) ContentEncodingFromExt(ext string) (encoding.Encoding, bool) {
switch ext {
case ".br":
return encoding.Brotli, true
case ".gz":
return encoding.Gzip, true
}
return encoding.Identity, false
}