-
Notifications
You must be signed in to change notification settings - Fork 56
/
page.go
95 lines (81 loc) · 2.25 KB
/
page.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
package serverHandler
import (
"../acceptHeaders"
"../i18n"
tplutil "../tpl/util"
"../util"
"html/template"
"io"
"net/http"
)
const TypeDir = template.HTML("dir")
const TypeFile = template.HTML("file")
func updateSubItemsHtml(data *responseData) {
length := len(data.SubItems)
data.SubItemsHtml = make([]*itemHtml, length)
dirQueryString := data.Context.QueryString()
for i, info := range data.SubItems {
name := info.Name()
urlEscapedName := tplutil.FormatFileUrl(name)
var displayName template.HTML
var typ template.HTML
var url string
var readableSize template.HTML
if info.IsDir() {
displayName = tplutil.FormatFilename(name) + "/"
typ = TypeDir
url = data.SubItemPrefix + urlEscapedName + "/" + dirQueryString
} else {
displayName = tplutil.FormatFilename(name)
typ = TypeFile
url = data.SubItemPrefix + urlEscapedName
readableSize = tplutil.FormatSize(info.Size())
}
var deleteUrl string
if data.CanDelete && !isVirtual(info) {
deleteUrl = name
}
data.SubItemsHtml[i] = &itemHtml{
Type: typ,
Url: url,
DisplayName: displayName,
DisplaySize: readableSize,
DisplayTime: tplutil.FormatTime(info.ModTime()),
DeleteUrl: deleteUrl,
}
}
}
func updateTranslation(r *http.Request, data *responseData) {
accepts := acceptHeaders.ParseAccepts(util.AsciiToLowerCase(r.Header.Get("Accept-Language")))
index, _, ok := accepts.GetPreferredValue(i18n.LanguageTags)
if !ok {
index = 0
}
data.Lang = i18n.LanguageTags[index]
data.Trans = i18n.Dictionaries[index].Trans
}
func (h *handler) page(w http.ResponseWriter, r *http.Request, data *responseData) {
header := w.Header()
header.Set("X-Content-Type-Options", "nosniff")
header.Set("Content-Type", "text/html; charset=utf-8")
header.Set("Cache-Control", "public, max-age=0")
updateTranslation(r, data)
if !needResponseBody(r.Method) {
w.WriteHeader(data.Status)
return
}
var bodyW io.Writer
if compressW, encoding, useCompressW := getCompressWriter(w, r); useCompressW {
defer compressW.Close()
header.Set("Content-Encoding", encoding)
bodyW = compressW
} else {
bodyW = w
}
w.WriteHeader(data.Status)
updateSubItemsHtml(data)
err := h.theme.RenderPage(bodyW, data)
if err != nil {
go h.errHandler.LogError(err)
}
}