1package matchers
2
3import "gopkg.in/h2non/filetype.v1/types"
4
5// Internal shortcut to NewType
6var newType = types.NewType
7
8// Matcher function interface as type alias
9type Matcher func([]byte) bool
10
11// Type interface to store pairs of type with its matcher function
12type Map map[types.Type]Matcher
13
14// Type specific matcher function interface
15type TypeMatcher func([]byte) types.Type
16
17// Store registered file type matchers
18var Matchers = make(map[types.Type]TypeMatcher)
19
20// Create and register a new type matcher function
21func NewMatcher(kind types.Type, fn Matcher) TypeMatcher {
22	matcher := func(buf []byte) types.Type {
23		if fn(buf) {
24			return kind
25		}
26		return types.Unknown
27	}
28
29	Matchers[kind] = matcher
30	return matcher
31}
32
33func register(matchers ...Map) {
34	for _, m := range matchers {
35		for kind, matcher := range m {
36			NewMatcher(kind, matcher)
37		}
38	}
39}
40
41func init() {
42	// Arguments order is intentional
43	register(Image, Video, Audio, Font, Document, Archive)
44}
45