Skip to content

Commit 9939f13

Browse files
committed
perf(serverHandler): prevent assemble string in template
BREAKING CHANGE: Getting HTML formatted sub items in template changed. Before: ``` {{range .SubItemsHtml}} <li class="{{if .IsDir}}dir{{else}}file{{end}}"> <a href="{{$subItemPrefix}}{{.Link}}{{if .IsDir}}/{{end}}"> <span class="name">{{.Name}}{{if .IsDir}}/{{end}}</span> <span class="size">{{if not .IsDir}}{{.Size}}{{end}}</span> <span class="time">{{.ModTime}}</span> </a> </li> {{end}} ``` After: ``` {{range .SubItemsHtml}} <li class="{{.Type}}"> <a href="{{.Url}}"> <span class="name">{{.DisplayName}}</span> <span class="size">{{.DisplaySize}}</span> <span class="time">{{.DisplayTime}}</span> </a> </li> {{end}} ```
1 parent 240efbf commit 9939f13

File tree

4 files changed

+40
-22
lines changed

4 files changed

+40
-22
lines changed

src/serverHandler/page.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,42 @@ package serverHandler
22

33
import (
44
tplutil "../tpl/util"
5+
"html/template"
56
"net/http"
67
)
78

9+
const TypeDir = template.HTML("dir")
10+
const TypeFile = template.HTML("file")
11+
812
func updateSubItemsHtml(data *responseData) {
913
length := len(data.SubItems)
1014

1115
data.SubItemsHtml = make([]*itemHtml, length)
1216

13-
for i := 0; i < length; i++ {
14-
info := data.SubItems[i]
17+
for i, info := range data.SubItems {
1518
name := info.Name()
19+
displayName := tplutil.FormatFilename(name)
20+
21+
var typ template.HTML
22+
var url string
23+
var readableSize template.HTML
24+
25+
if info.IsDir() {
26+
typ = TypeDir
27+
url = data.SubItemPrefix + name + "/"
28+
displayName += "/"
29+
} else {
30+
typ = TypeFile
31+
url = data.SubItemPrefix + name
32+
readableSize = tplutil.FormatSize(info.Size())
33+
}
1634

1735
data.SubItemsHtml[i] = &itemHtml{
18-
IsDir: info.IsDir(),
19-
Link: name,
20-
Name: tplutil.FormatFilename(name),
21-
Size: tplutil.FormatSize(info.Size()),
22-
ModTime: tplutil.FormatTime(info.ModTime()),
36+
Type: typ,
37+
Url: url,
38+
DisplayName: displayName,
39+
DisplaySize: readableSize,
40+
DisplayTime: tplutil.FormatTime(info.ModTime()),
2341
}
2442
}
2543
}

src/serverHandler/responseData.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ type pathEntry struct {
1717
}
1818

1919
type itemHtml struct {
20-
IsDir bool
21-
Link string
22-
Name template.HTML
23-
Size template.HTML
24-
ModTime template.HTML
20+
Type template.HTML
21+
Url string
22+
DisplayName template.HTML
23+
DisplaySize template.HTML
24+
DisplayTime template.HTML
2525
}
2626

2727
type responseData struct {

src/tpl/page.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@
4646
</a>
4747
</li>
4848
{{range .SubItemsHtml}}
49-
<li class="{{if .IsDir}}dir{{else}}file{{end}}">
50-
<a href="{{$subItemPrefix}}{{.Link}}{{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>
49+
<li class="{{.Type}}">
50+
<a href="{{.Url}}">
51+
<span class="name">{{.DisplayName}}</span>
52+
<span class="size">{{.DisplaySize}}</span>
53+
<span class="time">{{.DisplayTime}}</span>
5454
</a>
5555
</li>
5656
{{end}}

src/tpl/page.html.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ const pageTplStr = `
5151
</a>
5252
</li>
5353
{{range .SubItemsHtml}}
54-
<li class="{{if .IsDir}}dir{{else}}file{{end}}">
55-
<a href="{{$subItemPrefix}}{{.Link}}{{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>
54+
<li class="{{.Type}}">
55+
<a href="{{.Url}}">
56+
<span class="name">{{.DisplayName}}</span>
57+
<span class="size">{{.DisplaySize}}</span>
58+
<span class="time">{{.DisplayTime}}</span>
5959
</a>
6060
</li>
6161
{{end}}

0 commit comments

Comments
 (0)