-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzipfile.go
94 lines (79 loc) · 2.38 KB
/
zipfile.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package giawarc
import (
"archive/zip"
"bytes"
"io"
"io/ioutil"
"regexp"
"strings"
)
var zip_types map[string]*regexp.Regexp
func init() {
zip_types = map[string]*regexp.Regexp {
"application/vnd.oasis.opendocument.text": regexp.MustCompile(`^content\.xml$`),
"application/vnd.oasis.opendocument.spreadsheet": regexp.MustCompile(`^content\.xml$`),
"application/vnd.oasis.opendocument.presentation": regexp.MustCompile(`^content\.xml$`),
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": regexp.MustCompile(`^word/document\.xml$`),
"application/vnd.openxmlformats-officedocument.presentationml.presentation": regexp.MustCompile(`^ppt/slides/slide.*$`),
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": regexp.MustCompile(`^xl/sharedStrings\.xml$`),
"application/epub+zip": regexp.MustCompile(`^.*ml$`),
}
}
func IsZip(content_type, uri string) (string, bool) {
if strings.HasSuffix(uri, "odt") {
return "application/vnd.oasis.opendocument.text", true
}
if strings.HasSuffix(uri, "ods") {
return "application/vnd.oasis.opendocument.spreadsheet", true
}
if strings.HasSuffix(uri, "odp") {
return "application/vnd.oasis.opendocument.presentation", true
}
if strings.HasSuffix(uri, "docx") {
return "application/vnd.openxmlformats-officedocument.wordprocessingml.document", true
}
if strings.HasSuffix(uri, "pptx") {
return "application/vnd.openxmlformats-officedocument.presentationml.presentation", true
}
if strings.HasSuffix(uri, "xslx") {
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", true
}
if strings.HasSuffix(uri, "epub") {
return "application/epub+zip", true
}
_, ok := zip_types[content_type]
if ok {
return content_type, true
}
return content_type, false
}
func ReadZipPayload(content_type string, body io.Reader) (buf io.Reader, err error){
zipdata, err := ioutil.ReadAll(body)
if err != nil {
return
}
zip, err := zip.NewReader(bytes.NewReader(zipdata), int64(len(zipdata)))
if err != nil {
return
}
data := make([]byte, 0, 16384)
fre := zip_types[content_type]
for _, f := range zip.File {
if ! fre.MatchString(f.Name) {
continue
}
fp, err := f.Open()
if err != nil {
continue
}
contents, err := ioutil.ReadAll(fp)
if err != nil {
fp.Close()
continue
}
fp.Close()
data = append(data, contents...)
}
buf = bytes.NewBuffer(data)
return
}