Skip to content

Commit 7b4119c

Browse files
committed
refactor(tpl): use formatted data for page rendering
Add `.Html` to sub item info. Function invoking in template is slow. Generate formatted data before rendering. BREAKING CHANGE before: Iterate item's FileInfo in template ``` {{range .SubItems}} ... {{end}} ``` after: Iterate item's FileInfo in template ``` {{range .SubItems}}{{with .Info}} ... {{end}}{{end}} ```
1 parent dae32eb commit 7b4119c

File tree

11 files changed

+90
-52
lines changed

11 files changed

+90
-52
lines changed

src/serverHandler/perm.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ func hasUrlOrDirPrefix(urls []string, reqUrl string, dirs []string, reqDir strin
2121
return false
2222
}
2323

24-
func (h *handler) getCanUpload(item os.FileInfo, rawReqPath, reqFsPath string) bool {
25-
if item == nil || !item.IsDir() {
24+
func (h *handler) getCanUpload(info os.FileInfo, rawReqPath, reqFsPath string) bool {
25+
if info == nil || !info.IsDir() {
2626
return false
2727
}
2828

@@ -33,8 +33,8 @@ func (h *handler) getCanUpload(item os.FileInfo, rawReqPath, reqFsPath string) b
3333
return hasUrlOrDirPrefix(h.uploadUrls, rawReqPath, h.uploadDirs, reqFsPath)
3434
}
3535

36-
func (h *handler) getCanArchive(subItems []os.FileInfo, rawReqPath, reqFsPath string) bool {
37-
if len(subItems) == 0 {
36+
func (h *handler) getCanArchive(subInfos []os.FileInfo, rawReqPath, reqFsPath string) bool {
37+
if len(subInfos) == 0 {
3838
return false
3939
}
4040

src/serverHandler/responseData.go

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package serverHandler
22

33
import (
4+
tplutil "../tpl/util"
45
"../util"
6+
"html/template"
57
"net/http"
68
"os"
79
"path"
@@ -15,6 +17,18 @@ type pathEntry struct {
1517
Path string
1618
}
1719

20+
type subItemHtml struct {
21+
IsDir bool
22+
Name template.HTML
23+
Size template.HTML
24+
ModTime template.HTML
25+
}
26+
27+
type subItem struct {
28+
Info os.FileInfo
29+
Html subItemHtml
30+
}
31+
1832
type responseData struct {
1933
rawReqPath string
2034
handlerReqPath string
@@ -29,13 +43,14 @@ type responseData struct {
2943
File *os.File
3044
Item os.FileInfo
3145
ItemName string
32-
SubItems []os.FileInfo
46+
SubItems []*subItem
3347
SubItemPrefix string
34-
CanUpload bool
35-
CanArchive bool
36-
CanCors bool
37-
NeedAuth bool
38-
Errors []error
48+
49+
CanUpload bool
50+
CanArchive bool
51+
CanCors bool
52+
NeedAuth bool
53+
Errors []error
3954
}
4055

4156
func isSlash(c rune) bool {
@@ -185,12 +200,12 @@ func getSubItemPrefix(requestPath string, tailSlash bool) (subItemPrefix string)
185200
return
186201
}
187202

188-
func sortSubItems(subItems []os.FileInfo) {
203+
func sortSubInfos(subInfos []os.FileInfo) {
189204
sort.Slice(
190-
subItems,
205+
subInfos,
191206
func(prevIndex, nextIndex int) bool {
192-
prevItem := subItems[prevIndex]
193-
nextItem := subItems[nextIndex]
207+
prevItem := subInfos[prevIndex]
208+
nextItem := subInfos[nextIndex]
194209

195210
prevIsDir := prevItem.IsDir()
196211
nextIsDir := nextItem.IsDir()
@@ -204,16 +219,35 @@ func sortSubItems(subItems []os.FileInfo) {
204219
)
205220
}
206221

207-
func getItemName(item os.FileInfo, r *http.Request) (itemName string) {
208-
if item != nil {
209-
itemName = item.Name()
222+
func getItemName(info os.FileInfo, r *http.Request) (itemName string) {
223+
if info != nil {
224+
itemName = info.Name()
210225
}
211226
if len(itemName) == 0 || itemName == "." {
212227
itemName = strings.Replace(r.Host, ":", "_", -1)
213228
}
214229
return
215230
}
216231

232+
func getSubItems(subInfos []os.FileInfo) []*subItem {
233+
subItems := make([]*subItem, len(subInfos))
234+
235+
for i := 0; i < len(subInfos); i++ {
236+
info := subInfos[i]
237+
subItems[i] = &subItem{
238+
Info: info,
239+
Html: subItemHtml{
240+
IsDir: info.IsDir(),
241+
Name: tplutil.FormatFilename(info.Name()),
242+
Size: tplutil.FormatSize(info.Size()),
243+
ModTime: tplutil.FormatTime(info.ModTime()),
244+
},
245+
}
246+
}
247+
248+
return subItems
249+
}
250+
217251
func (h *handler) getResponseData(r *http.Request) (data *responseData) {
218252
requestUri := r.URL.Path
219253
tailSlash := requestUri[len(requestUri)-1] == '/'
@@ -248,21 +282,23 @@ func (h *handler) getResponseData(r *http.Request) (data *responseData) {
248282

249283
itemName := getItemName(item, r)
250284

251-
subItems, _readdirErrs := readdir(file, item)
285+
subInfos, _readdirErrs := readdir(file, item)
252286
errs = append(errs, _readdirErrs...)
253287
internalError = internalError || len(_readdirErrs) > 0
254288

255-
_mergeErrs := h.mergeAlias(rawReqPath, &subItems)
289+
_mergeErrs := h.mergeAlias(rawReqPath, &subInfos)
256290
errs = append(errs, _mergeErrs...)
257291
internalError = internalError || len(_mergeErrs) > 0
258292

259-
subItems = h.FilterItems(subItems)
260-
sortSubItems(subItems)
293+
subInfos = h.FilterItems(subInfos)
294+
sortSubInfos(subInfos)
295+
296+
subItems := getSubItems(subInfos)
261297

262298
subItemPrefix := getSubItemPrefix(reqPath, tailSlash)
263299

264300
canUpload := h.getCanUpload(item, rawReqPath, reqFsPath)
265-
canArchive := h.getCanArchive(subItems, rawReqPath, reqFsPath)
301+
canArchive := h.getCanArchive(subInfos, rawReqPath, reqFsPath)
266302
canCors := h.getCanCors(rawReqPath, reqFsPath)
267303
needAuth := h.getNeedAuth(rawReqPath, reqFsPath)
268304

src/tpl/page.html

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,15 @@
4545
<span class="time"></span>
4646
</a>
4747
</li>
48-
{{range .SubItems}}
49-
{{$isDir := .IsDir}}{{$subItemName := .Name}}
50-
<li class="{{if $isDir}}dir{{else}}file{{end}}">
51-
<a href="{{$subItemPrefix}}{{$subItemName}}{{if $isDir}}/{{end}}">
52-
<span class="name">{{fmtFilename $subItemName}}{{if $isDir}}/{{end}}</span>
53-
<span class="size">{{if not $isDir}}{{fmtSize .Size}}{{end}}</span>
54-
<span class="time">{{fmtTime .ModTime}}</span>
48+
{{range .SubItems}}{{with .Html}}
49+
<li class="{{if .IsDir}}dir{{else}}file{{end}}">
50+
<a href="{{$subItemPrefix}}{{.Name}}{{if .IsDir}}/{{end}}">
51+
<span class="name">{{.Name}}{{if .IsDir}}/{{end}}</span>
52+
<span class="size">{{if not .IsDir}}{{.Size}}{{end}}</span>
53+
<span class="time">{{.ModTime}}</span>
5554
</a>
5655
</li>
57-
{{end}}
56+
{{end}}{{end}}
5857
</ul>
5958

6059
{{range .Errors}}

src/tpl/page.html.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package tpl
22

33
import (
44
"../serverErrHandler"
5-
"../util"
5+
"./util"
66
"html/template"
77
"path"
88
)
@@ -50,16 +50,15 @@ const pageTplStr = `
5050
<span class="time"></span>
5151
</a>
5252
</li>
53-
{{range .SubItems}}
54-
{{$isDir := .IsDir}}{{$subItemName := .Name}}
55-
<li class="{{if $isDir}}dir{{else}}file{{end}}">
56-
<a href="{{$subItemPrefix}}{{$subItemName}}{{if $isDir}}/{{end}}">
57-
<span class="name">{{fmtFilename $subItemName}}{{if $isDir}}/{{end}}</span>
58-
<span class="size">{{if not $isDir}}{{fmtSize .Size}}{{end}}</span>
59-
<span class="time">{{fmtTime .ModTime}}</span>
53+
{{range .SubItems}}{{with .Html}}
54+
<li class="{{if .IsDir}}dir{{else}}file{{end}}">
55+
<a href="{{$subItemPrefix}}{{.Name}}{{if .IsDir}}/{{end}}">
56+
<span class="name">{{.Name}}{{if .IsDir}}/{{end}}</span>
57+
<span class="size">{{if not .IsDir}}{{.Size}}{{end}}</span>
58+
<span class="time">{{.ModTime}}</span>
6059
</a>
6160
</li>
62-
{{end}}
61+
{{end}}{{end}}
6362
</ul>
6463
{{range .Errors}}
6564
<div class="error">{{.}}</div>
@@ -102,6 +101,6 @@ func addFuncMap(tpl *template.Template) *template.Template {
102101
return tpl.Funcs(template.FuncMap{
103102
"fmtFilename": util.FormatFilename,
104103
"fmtSize": util.FormatSize,
105-
"fmtTime": util.FormatTimeMinute,
104+
"fmtTime": util.FormatTime,
106105
})
107106
}
File renamed without changes.
File renamed without changes.

src/util/formatSize.go renamed to src/tpl/util/formatSize.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package util
22

33
import (
4+
"html/template"
45
"strconv"
56
)
67

@@ -13,18 +14,18 @@ const (
1314
PB
1415
)
1516

16-
func fmtUnit(unitName string, unitValue int64, srcValue int64) string {
17+
func fmtUnit(unitName string, unitValue int64, srcValue int64) template.HTML {
1718
prefix := int(srcValue / unitValue)
1819
suffix := int(srcValue % unitValue * 100 / unitValue)
1920

2021
if suffix >= 55 {
2122
prefix++
2223
}
2324

24-
return strconv.Itoa(prefix) + unitName
25+
return template.HTML(strconv.Itoa(prefix) + unitName)
2526
}
2627

27-
func FormatSize(size int64) string {
28+
func FormatSize(size int64) template.HTML {
2829
switch {
2930
case size > PB:
3031
return fmtUnit("P", PB, size)
File renamed without changes.

src/tpl/util/formatTime.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package util
2+
3+
import (
4+
"html/template"
5+
"time"
6+
)
7+
8+
func FormatTime(t time.Time) template.HTML {
9+
return template.HTML(t.Format("2006-01-02 15:04"))
10+
}

src/util/formatTime_test.go renamed to src/tpl/util/formatTime_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@ import (
88

99
func TestFormatTimeMinute(t *testing.T) {
1010
time := time.Now()
11-
fmt.Println(FormatTimeMinute(time))
12-
fmt.Println(FormatTimeSecond(time))
11+
fmt.Println(FormatTime(time))
1312
}

src/util/formatTime.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
package util
22

3-
import (
4-
"time"
5-
)
6-
7-
func FormatTimeMinute(t time.Time) string {
8-
return t.Format("2006-01-02 15:04")
9-
}
3+
import "time"
104

115
func FormatTimeSecond(t time.Time) string {
126
return t.Format("2006-01-02 15:04:05")

0 commit comments

Comments
 (0)