forked from h2non/filetype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatcher_document.go
66 lines (57 loc) · 1.78 KB
/
matcher_document.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package filetype
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 = MapOfMatcherFunc{
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))
}