forked from h2non/filetype
-
Notifications
You must be signed in to change notification settings - Fork 1
/
match.go
86 lines (69 loc) · 1.99 KB
/
match.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
75
76
77
78
79
80
81
82
83
84
85
86
package filetype
import (
"io"
"os"
"gopkg.in/h2non/filetype.v1/matchers"
"gopkg.in/h2non/filetype.v1/types"
)
// Matchers is an alias to matchers.Matchers
var Matchers = matchers.Matchers
// NewMatcher is an alias to matchers.NewMatcher
var NewMatcher = matchers.NewMatcher
// Match infers the file type of a given buffer inspecting its magic numbers signature
func Match(buf []byte) (types.Type, error) {
length := len(buf)
if length == 0 {
return types.Unknown, ErrEmptyBuffer
}
for _, checker := range Matchers {
match := checker(buf)
if match != types.Unknown && match.Extension != "" {
return match, nil
}
}
return types.Unknown, nil
}
// Get is an alias to Match()
func Get(buf []byte) (types.Type, error) {
return Match(buf)
}
// MatchFile infers a file type for a file
func MatchFile(filepath string) (types.Type, error) {
file, err := os.Open(filepath)
if err != nil {
return types.Unknown, err
}
defer file.Close()
return MatchReader(file)
}
// MatchReader is convenient wrapper to Match() any Reader
func MatchReader(reader io.Reader) (types.Type, error) {
buffer := make([]byte, 512)
_, err := reader.Read(buffer)
if err != nil && err != io.EOF {
return types.Unknown, err
}
return Match(buffer)
}
// AddMatcher registers a new matcher type
func AddMatcher(fileType types.Type, matcher matchers.Matcher) matchers.TypeMatcher {
return matchers.NewMatcher(fileType, matcher)
}
// Matches checks if the given buffer matches with some supported file type
func Matches(buf []byte) bool {
kind, _ := Match(buf)
return kind != types.Unknown
}
// MatchMap performs a file matching againts a map of match functions
func MatchMap(buf []byte, matchers matchers.Map) types.Type {
for kind, matcher := range matchers {
if matcher(buf) {
return kind
}
}
return types.Unknown
}
// MatchesMap is an alias to Matches() but using matching againts a map of match functions
func MatchesMap(buf []byte, matchers matchers.Map) bool {
return MatchMap(buf, matchers) != types.Unknown
}