-
Notifications
You must be signed in to change notification settings - Fork 7
/
map.go
45 lines (41 loc) · 1.15 KB
/
map.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
package pistol
import (
"io"
"regexp"
)
var internalWritersRegexMap = map[string] func(string, string) (func(w io.Writer) error, error) {
"text/*": NewChromaWriter,
"application/zip": NewArchiveLister,
"application/x-rar-compressed": NewArchiveLister,
"application/x-tar": NewArchiveLister,
"application/x-xz": NewArchiveLister,
"application/x-bzip2": NewArchiveLister,
"application/gzip": NewArchiveLister,
"application/x-lz4": NewArchiveLister,
"application/x-snappy-framed": NewArchiveLister,
"application/x-zstd": NewArchiveLister,
// TODO: Match brotli when libmagic supports it
}
var emptyWriter = func(w io.Writer) error {
return nil
}
func MatchInternalWriter(mimeType, filePath string) (func(w io.Writer) error, error) {
for regex, writerCreator := range internalWritersRegexMap {
match, err := regexp.MatchString(regex, mimeType)
if err != nil {
return emptyWriter, err
}
if match {
writer, err := writerCreator(mimeType, filePath)
if err != nil {
return emptyWriter, err
}
return writer, nil
}
}
writer, err := NewFallbackWriter(mimeType, filePath)
if err != nil {
return emptyWriter, err
}
return writer, err
}