diff --git a/README.md b/README.md index ee9cff4..f75d2a9 100644 --- a/README.md +++ b/README.md @@ -239,6 +239,15 @@ func main() { - **rpm** - `application/x-rpm` - **elf** - `application/x-executable` +#### Documents + +- **doc** - `application/msword` +- **docx** - `application/vnd.openxmlformats-officedocument.wordprocessingml.document` +- **xls** - `application/vnd.ms-excel` +- **xlsx** - `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet` +- **ppt** - `application/vnd.ms-powerpoint` +- **pptx** - `application/vnd.openxmlformats-officedocument.presentationml.presentation` + #### Font - **woff** - `application/font-woff` diff --git a/kind_test.go b/kind_test.go index ec3d903..7a2478f 100644 --- a/kind_test.go +++ b/kind_test.go @@ -34,7 +34,7 @@ func TestIsKind(t *testing.T) { for _, test := range cases { if IsImage(test.buf) != test.match { - t.Fatalf("Invalid match: %s", test.match) + t.Fatalf("Invalid match: %t", test.match) } } } diff --git a/match_test.go b/match_test.go index b0e542f..5dcbb5e 100644 --- a/match_test.go +++ b/match_test.go @@ -27,7 +27,7 @@ func TestMatch(t *testing.T) { } if match.Extension != test.ext { - t.Fatalf("Invalid image type: %s", match.Extension) + t.Fatalf("Invalid image type: %s != %s", match.Extension, test.ext) } } } @@ -48,7 +48,7 @@ func TestMatchFile(t *testing.T) { for _, test := range cases { kind, _ := MatchFile("./fixtures/sample." + test.ext) if kind.Extension != test.ext { - t.Fatalf("Invalid image type: %s", kind.Extension) + t.Fatalf("Invalid image type: %s != %s", kind.Extension, test.ext) } } } diff --git a/matchers/document.go b/matchers/document.go new file mode 100644 index 0000000..cc5ded2 --- /dev/null +++ b/matchers/document.go @@ -0,0 +1,66 @@ +package matchers + +import "bytes" + +var ( + TypeDoc = newType("doc", "application/msword") + TypeDocx = newType("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document") + TypeXls = newType("xls", "application/vnd.ms-excel") + TypeXlsx = newType("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") + TypePpt = newType("ppt", "application/vnd.ms-powerpoint") + TypePptx = newType("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation") +) + +var Document = Map{ + TypeDoc: Doc, + TypeDocx: Docx, + TypeXls: Xls, + TypeXlsx: Xlsx, + TypePpt: Ppt, + TypePptx: Pptx, +} + +func Doc(buf []byte) bool { + return len(buf) > 7 && + buf[0] == 0xD0 && buf[1] == 0xCF && + buf[2] == 0x11 && buf[3] == 0xE0 && + buf[4] == 0xA1 && buf[5] == 0xB1 && + buf[6] == 0x1A && buf[7] == 0xE1 +} + +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)) +} + +func Xls(buf []byte) bool { + return len(buf) > 7 && + buf[0] == 0xD0 && buf[1] == 0xCF && + buf[2] == 0x11 && buf[3] == 0xE0 && + buf[4] == 0xA1 && buf[5] == 0xB1 && + buf[6] == 0x1A && buf[7] == 0xE1 +} + +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)) +} + +func Ppt(buf []byte) bool { + return len(buf) > 7 && + buf[0] == 0xD0 && buf[1] == 0xCF && + buf[2] == 0x11 && buf[3] == 0xE0 && + buf[4] == 0xA1 && buf[5] == 0xB1 && + buf[6] == 0x1A && buf[7] == 0xE1 +} + +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)) +} diff --git a/matchers/font.go b/matchers/font.go index a4bb91f..f391716 100644 --- a/matchers/font.go +++ b/matchers/font.go @@ -5,12 +5,6 @@ var ( TypeWoff2 = newType("woff2", "application/font-woff") TypeTtf = newType("ttf", "application/font-sfnt") TypeOtf = newType("otf", "application/font-sfnt") - TypeDoc = newType("doc", "application/msword") - TypeDocx = newType("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document") - TypeXls = newType("xls", "application/vnd.ms-excel") - TypeXlsx = newType("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") - TypePpt = newType("ppt", "application/vnd.ms-powerpoint") - TypePptx = newType("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation") ) var Font = Map{ @@ -18,12 +12,6 @@ var Font = Map{ TypeWoff2: Woff2, TypeTtf: Ttf, TypeOtf: Otf, - TypeDoc: Doc, - TypeDocx: Docx, - TypeXls: Xls, - TypeXlsx: Xlsx, - TypePpt: Ppt, - TypePptx: Pptx, } func Woff(buf []byte) bool { @@ -55,45 +43,3 @@ func Otf(buf []byte) bool { buf[2] == 0x54 && buf[3] == 0x4F && buf[4] == 0x00 } - -func Doc(buf []byte) bool { - return len(buf) > 7 && - buf[0] == 0xD0 && buf[1] == 0xCF && - buf[2] == 0x11 && buf[3] == 0xE0 && - buf[4] == 0xA1 && buf[5] == 0xB1 && - buf[6] == 0x1A && buf[7] == 0xE1 -} - -func Docx(buf []byte) bool { - return len(buf) > 3 && - buf[0] == 0x50 && buf[1] == 0x4B && - buf[2] == 0x03 && buf[3] == 0x04 -} - -func Xls(buf []byte) bool { - return len(buf) > 7 && - buf[0] == 0xD0 && buf[1] == 0xCF && - buf[2] == 0x11 && buf[3] == 0xE0 && - buf[4] == 0xA1 && buf[5] == 0xB1 && - buf[6] == 0x1A && buf[7] == 0xE1 -} - -func Xlsx(buf []byte) bool { - return len(buf) > 3 && - buf[0] == 0x50 && buf[1] == 0x4B && - buf[2] == 0x03 && buf[3] == 0x04 -} - -func Ppt(buf []byte) bool { - return len(buf) > 7 && - buf[0] == 0xD0 && buf[1] == 0xCF && - buf[2] == 0x11 && buf[3] == 0xE0 && - buf[4] == 0xA1 && buf[5] == 0xB1 && - buf[6] == 0x1A && buf[7] == 0xE1 -} - -func Pptx(buf []byte) bool { - return len(buf) > 3 && - buf[0] == 0x50 && buf[1] == 0x4B && - buf[2] == 0x07 && buf[3] == 0x08 -} diff --git a/matchers/matchers.go b/matchers/matchers.go index c697391..4525c02 100644 --- a/matchers/matchers.go +++ b/matchers/matchers.go @@ -40,5 +40,5 @@ func register(matchers ...Map) { func init() { // Arguments order is intentional - register(Image, Video, Audio, Font, Archive) + register(Image, Video, Audio, Font, Document, Archive) }