diff --git a/fixtures/sample.doc b/fixtures/sample.doc new file mode 100644 index 0000000..45c0345 Binary files /dev/null and b/fixtures/sample.doc differ diff --git a/match.go b/match.go index 9b6e376..ece105b 100644 --- a/match.go +++ b/match.go @@ -14,6 +14,21 @@ var Matchers = matchers.Matchers // NewMatcher is an alias to matchers.NewMatcher var NewMatcher = matchers.NewMatcher +// PossibleTypes returns the possible mime types of given bytes +func PossibleTypes(b []byte) ([]types.Type, error) { + if len(b) == 0 { + return nil, ErrEmptyBuffer + } + possibleTypes := []types.Type{} + for _, checker := range Matchers { + matchedType := checker(b) + if matchedType != types.Unknown && matchedType.Extension != "" { + possibleTypes = append(possibleTypes, matchedType) + } + } + return possibleTypes, nil +} + // Match infers the file type of a given buffer inspecting its magic numbers signature func Match(buf []byte) (types.Type, error) { length := len(buf) diff --git a/match_test.go b/match_test.go index 5dcbb5e..5e74b7f 100644 --- a/match_test.go +++ b/match_test.go @@ -185,3 +185,26 @@ func BenchmarkMatchPng(b *testing.B) { Match(pngBuffer) } } + +func TestPossibleTypes(t *testing.T) { + docBuffer, _ := ioutil.ReadFile("./fixtures/sample.doc") + fileBytes := [][]byte{docBuffer} + for _, fileByte := range fileBytes { + pts, err := PossibleTypes(fileByte) + if err != nil { + t.Fail() + t.Error(err) + } + var success bool + for _, typ := range pts { + t.Logf("possible mime-type: %s, ext: %s", typ.MIME.Value, typ.Extension) + if typ.Extension == "doc" { + success = true + } + } + if !success { + t.Fail() + t.Error("matched failed") + } + } +} diff --git a/matchers/document.go b/matchers/document.go index cc5ded2..ffc2679 100644 --- a/matchers/document.go +++ b/matchers/document.go @@ -1,7 +1,5 @@ package matchers -import "bytes" - var ( TypeDoc = newType("doc", "application/msword") TypeDocx = newType("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document") @@ -31,8 +29,7 @@ func Doc(buf []byte) bool { func Docx(buf []byte) bool { return len(buf) > 3 && buf[0] == 0x50 && buf[1] == 0x4B && - buf[2] == 0x03 && buf[3] == 0x04 && - bytes.Contains(buf[:256], []byte(TypeDocx.MIME.Value)) + buf[2] == 0x03 && buf[3] == 0x04 } func Xls(buf []byte) bool { @@ -46,8 +43,7 @@ func Xls(buf []byte) bool { func Xlsx(buf []byte) bool { return len(buf) > 3 && buf[0] == 0x50 && buf[1] == 0x4B && - buf[2] == 0x03 && buf[3] == 0x04 && - bytes.Contains(buf[:256], []byte(TypeXlsx.MIME.Value)) + buf[2] == 0x03 && buf[3] == 0x04 } func Ppt(buf []byte) bool { @@ -61,6 +57,5 @@ func Ppt(buf []byte) bool { func Pptx(buf []byte) bool { return len(buf) > 3 && buf[0] == 0x50 && buf[1] == 0x4B && - buf[2] == 0x07 && buf[3] == 0x08 && - bytes.Contains(buf[:256], []byte(TypePptx.MIME.Value)) + buf[2] == 0x07 && buf[3] == 0x08 }