-
Notifications
You must be signed in to change notification settings - Fork 56
/
archive.go
194 lines (169 loc) · 4.48 KB
/
archive.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package serverHandler
import (
"../shimgo"
"../util"
"net/http"
"os"
"path"
"strings"
)
type archiveCallback func(f *os.File, fInfo os.FileInfo, relPath string) error
func matchSelection(info os.FileInfo, selections []string) (matchName, matchPrefix bool, childSelections []string) {
if len(selections) == 0 {
return true, false, nil
}
name := info.Name()
isNameEqual := getIsNameEqualFunc(info)
for _, selName := range selections {
if isNameEqual(selName, name) {
matchName = true
continue
}
slashIndex := strings.IndexByte(selName, '/')
if slashIndex <= 0 {
continue
}
selNamePart1 := selName[:slashIndex]
if isNameEqual(selNamePart1, name) {
childSel := selName[slashIndex+1:]
if len(childSel) > 0 {
matchPrefix = true
childSelections = append(childSelections, childSel)
}
continue
}
}
return
}
func (h *handler) visitTreeNode(
fsPath, rawReqPath, relPath string,
statNode bool,
childSelections []string,
archiveCallback archiveCallback,
) {
var fInfo os.FileInfo
var childInfos []os.FileInfo
// wrap func to run defer ASAP
err := func() error {
var f *os.File
var err error
if statNode {
f, err = os.Open(fsPath)
if f != nil {
defer f.Close()
}
if h.errHandler.LogError(err) {
if os.IsExist(err) {
return err
}
fInfo = createPlaceholderFileInfo(path.Base(fsPath), true) // prefix path for alias
} else {
fInfo, err = f.Stat()
if h.errHandler.LogError(err) {
return err
}
}
} else {
fInfo = createPlaceholderFileInfo(path.Base(fsPath), true)
}
if len(relPath) > 0 {
if err := archiveCallback(f, fInfo, relPath); err != nil {
return err
}
}
if f != nil && fInfo.IsDir() {
childInfos, err = f.Readdir(0)
if h.errHandler.LogError(err) {
return err
}
}
return nil
}()
if err != nil {
return
}
if fInfo.IsDir() {
childInfos, _, _ := h.mergeAlias(rawReqPath, fInfo, childInfos)
childInfos = h.FilterItems(childInfos)
// childInfo can be regular dir/file, or aliased item that shadows regular dir/file
for _, childInfo := range childInfos {
matchChildName, matchChildPrefix, childChildSelections := matchSelection(childInfo, childSelections)
if !matchChildName && !matchChildPrefix {
continue
}
childPath := "/" + childInfo.Name()
childFsPath := fsPath + childPath
childRawReqPath := util.CleanUrlPath(rawReqPath + childPath)
childRelPath := relPath + childPath
if childAlias, hasChildAlias := h.aliases.byUrlPath(childRawReqPath); hasChildAlias {
h.visitTreeNode(childAlias.fsPath(), childRawReqPath, childRelPath, true, childChildSelections, archiveCallback)
} else {
h.visitTreeNode(childFsPath, childRawReqPath, childRelPath, statNode, childChildSelections, archiveCallback)
}
}
}
}
func (h *handler) archive(
w http.ResponseWriter,
r *http.Request,
pageData *responseData,
selections []string,
fileSuffix string,
contentType string,
cbWriteFile archiveCallback,
) {
var itemName string
_, hasAlias := h.aliases.byUrlPath(pageData.rawReqPath)
if hasAlias {
itemName = path.Base(pageData.rawReqPath)
}
if len(itemName) == 0 || itemName == "/" {
itemName = pageData.ItemName
}
targetFilename := itemName + fileSuffix
writeArchiveHeader(w, contentType, targetFilename)
if !needResponseBody(r.Method) {
return
}
h.visitTreeNode(
path.Clean(h.root+pageData.handlerReqPath),
pageData.rawReqPath,
"",
pageData.Item != nil, // not empty root
selections,
func(f *os.File, fInfo os.FileInfo, relPath string) error {
go h.logArchive(targetFilename, relPath, r)
err := cbWriteFile(f, fInfo, relPath)
h.errHandler.LogError(err)
return err
},
)
}
func writeArchiveHeader(w http.ResponseWriter, contentType, filename string) {
filename = shimgo.Net_Url_PathEscape(filename)
header := w.Header()
header.Set("Content-Type", contentType)
header.Set("Content-Disposition", "attachment; filename*=UTF-8''"+filename)
header.Set("Cache-Control", "public, max-age=0")
w.WriteHeader(http.StatusOK)
}
func (h *handler) normalizeArchiveSelections(r *http.Request) ([]string, bool) {
if h.errHandler.LogError(r.ParseForm()) {
return nil, false
}
inputs := r.Form["name"]
if len(inputs) == 0 {
return nil, true
}
count := len(inputs)
selections := make([]string, count)
for i := 0; i < count; i++ {
var ok bool
selections[i], ok = getCleanDirFilePath(inputs[i])
if !ok {
h.logger.LogErrorString("archive: illegal path " + inputs[i])
return nil, false
}
}
return selections, true
}