mirror of
https://github.com/h2non/filetype.git
synced 2025-12-24 11:52:08 +08:00
fix: tests and document types matchers
This commit is contained in:
@@ -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`
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
66
matchers/document.go
Normal file
66
matchers/document.go
Normal file
@@ -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))
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user