Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add method PossibleTypes and fix ms-office matchers' rules #39

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added fixtures/sample.doc
Binary file not shown.
15 changes: 15 additions & 0 deletions match.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}
11 changes: 3 additions & 8 deletions matchers/document.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package matchers

import "bytes"

var (
TypeDoc = newType("doc", "application/msword")
TypeDocx = newType("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
}