| 1 |
package matchers |
| 2 |
|
| 3 |
import ( |
| 4 |
"github.com/h2non/filetype/types" |
| 5 |
) |
| 6 |
|
| 7 |
|
| 8 |
var newType = types.NewType |
| 9 |
|
| 10 |
|
| 11 |
type Matcher func([]byte) bool |
| 12 |
|
| 13 |
|
| 14 |
type Map map[types.Type]Matcher |
| 15 |
|
| 16 |
|
| 17 |
type TypeMatcher func([]byte) types.Type |
| 18 |
|
| 19 |
|
| 20 |
var Matchers = make(map[types.Type]TypeMatcher) |
| 21 |
var MatcherKeys []types.Type |
| 22 |
|
| 23 |
|
| 24 |
func NewMatcher(kind types.Type, fn Matcher) TypeMatcher { |
| 25 |
matcher := func(buf []byte) types.Type { |
| 26 |
if fn(buf) { |
| 27 |
return kind |
| 28 |
} |
| 29 |
return types.Unknown |
| 30 |
} |
| 31 |
|
| 32 |
Matchers[kind] = matcher |
| 33 |
|
| 34 |
MatcherKeys = append([]types.Type{kind}, MatcherKeys...) |
| 35 |
return matcher |
| 36 |
} |
| 37 |
|
| 38 |
func register(matchers ...Map) { |
| 39 |
MatcherKeys = MatcherKeys[:0] |
| 40 |
for _, m := range matchers { |
| 41 |
for kind, matcher := range m { |
| 42 |
NewMatcher(kind, matcher) |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
func init() { |
| 48 |
|
| 49 |
|
| 50 |
register(Archive, Document, Font, Audio, Video, Image, Application) |
| 51 |
} |