-
Notifications
You must be signed in to change notification settings - Fork 56
/
archive.go
160 lines (139 loc) · 3.83 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
package serverHandler
import (
"../shimgo"
"../util"
"net/http"
"os"
"path"
"strings"
)
type filterCallback func([]os.FileInfo) []os.FileInfo
type archiveCallback func(f *os.File, fInfo os.FileInfo, relPath string) error
func (h *handler) visitFs(
initFsPath, rawRequestPath, relPath string,
filterCallback filterCallback,
archiveCallback archiveCallback,
) {
alias, hasAlias := h.aliases.byUrlPath(rawRequestPath)
var fsPath string
if hasAlias {
fsPath = alias.fsPath
} else {
fsPath = initFsPath
}
var fInfo os.FileInfo
var childInfos []os.FileInfo
err := func() error {
f, err := os.Open(fsPath)
if f != nil {
defer f.Close()
}
h.errHandler.LogError(err)
if err != nil {
if os.IsExist(err) {
return err
}
fInfo = newFakeFileInfo(path.Base(fsPath), true) // prefix path for alias
} else {
fInfo, err = f.Stat()
if h.errHandler.LogError(err) {
return err
}
}
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
}
childInfos = filterCallback(childInfos)
}
return nil
}()
if err != nil {
return
}
if fInfo.IsDir() {
childAliases := map[string]string{}
for _, alias := range h.aliases {
if alias.isChildOf(rawRequestPath) {
childAliases[alias.urlPath] = alias.fsPath
continue
}
if alias.isSuccessorOf(rawRequestPath) {
succPath := alias.urlPath[len(rawRequestPath):]
if succPath[0] == '/' {
succPath = succPath[1:]
}
childName := succPath[:strings.Index(succPath, "/")]
childUrlPath := util.CleanUrlPath(rawRequestPath + "/" + childName)
childFsPath := fsPath + "/" + childName
childAliases[childUrlPath] = childFsPath
continue
}
}
for _, childInfo := range childInfos {
childPath := "/" + childInfo.Name()
childFsPath := fsPath + childPath
childRawRequestPath := util.CleanUrlPath(rawRequestPath + childPath)
childRelPath := relPath + childPath
if childAliasedFsPath, hasChildAlias := childAliases[childRawRequestPath]; hasChildAlias {
h.visitFs(childAliasedFsPath, childRawRequestPath, childRelPath, filterCallback, archiveCallback)
delete(childAliases, childRawRequestPath)
} else {
h.visitFs(childFsPath, childRawRequestPath, childRelPath, filterCallback, archiveCallback)
}
}
for childRawRequestPath, childAliasedFsPath := range childAliases {
childRelPath := relPath + "/" + path.Base(childRawRequestPath)
h.visitFs(childAliasedFsPath, childRawRequestPath, childRelPath, filterCallback, archiveCallback)
}
}
}
func (h *handler) archive(
w http.ResponseWriter,
r *http.Request,
pageData *responseData,
fileSuffix string,
contentType string,
filterCallback filterCallback,
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.visitFs(
path.Clean(h.root+pageData.handlerReqPath),
pageData.rawReqPath,
"",
filterCallback,
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)
}