-
Notifications
You must be signed in to change notification settings - Fork 56
/
page.html.go
144 lines (135 loc) · 4.78 KB
/
page.html.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
package tpl
import (
"../serverErrHandler"
"./util"
"html/template"
"path"
)
const pageTplStr = `
<!DOCTYPE html>
<html lang="">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="initial-scale=1, user-scalable=no"/>
<meta name="format-detection" content="telephone=no"/>
<meta name="renderer" content="webkit"/>
<meta name="wap-font-scale" content="no"/>
<title>{{.Path}}</title>
<link rel="stylesheet" type="text/css" href="{{.RootRelPath}}?asset=main.css"/>
</head>
<body class="{{if .IsRoot}}root-dir{{else}}sub-dir{{end}}">
{{$contextQueryString := .Context.QueryString}}
<ol class="path-list">
{{range .Paths}}
<li><a href="{{.Path}}{{$contextQueryString}}">{{fmtFilename .Name}}</a></li>
{{end}}
</ol>
{{if .CanMkdir}}
<div class="panel mkdir">
<form method="POST" action="{{.SubItemPrefix}}?mkdir">
<input type="text" autocomplete="off" name="name" class="name"/>
<input type="submit" value="mkdir" class="submit"/>
</form>
</div>
{{end}}
{{if .CanUpload}}
<div class="panel upload">
<form method="POST" action="{{.SubItemPrefix}}?upload" enctype="multipart/form-data">
<input type="file" name="file" multiple="multiple" class="file"/>
<input type="submit" value="Upload" class="submit"/>
</form>
</div>
{{end}}
{{if .CanArchive}}
<div class="archive">
<a href="{{.SubItemPrefix}}?tar" download="{{.ItemName}}.tar">.tar</a>
<a href="{{.SubItemPrefix}}?tgz" download="{{.ItemName}}.tar.gz">.tar.gz</a>
<a href="{{.SubItemPrefix}}?zip" download="{{.ItemName}}.zip">.zip</a>
</div>
{{end}}
{{if .SubItemsHtml}}
<div class="panel filter" id="panel-filter">
<div class="form">
<input type="text" accesskey="r" placeholder="filter..." name="filter-text" class="filter-text"/>
</div>
</div>
{{end}}
{{if .CanDelete}}
<script type="text/javascript">
function confirmDelete(el) {
var href = el.href;
var name = decodeURIComponent(href.substr(href.lastIndexOf('=') + 1));
return confirm('Delete?\n' + name);
}
</script>
{{end}}
<ul class="item-list{{if .HasDeletable}} has-deletable{{end}}">
<li class="header">{{$dirSort := .SortState.DirSort}}{{$sortKey := .SortState.Key}}
<span class="detail">
<a class="field dir" href="{{.SubItemPrefix}}{{.Context.QueryStringOfSort .SortState.NextDirSort}}">Dir{{if eq $dirSort -1}}↑{{else if eq $dirSort 1}}↓{{end}}</a>
<a class="field name" href="{{.SubItemPrefix}}{{.Context.QueryStringOfSort .SortState.NextNameSort}}">Name{{if eq $sortKey "n"}}↑{{else if eq $sortKey "N"}}↓{{end}}</a>
<a class="field type" href="{{.SubItemPrefix}}{{.Context.QueryStringOfSort .SortState.NextTypeSort}}">Type{{if eq $sortKey "e"}}↑{{else if eq $sortKey "E"}}↓{{end}}</a>
<a class="field size" href="{{.SubItemPrefix}}{{.Context.QueryStringOfSort .SortState.NextSizeSort}}">Size{{if eq $sortKey "s"}}↑{{else if eq $sortKey "S"}}↓{{end}}</a>
<a class="field time" href="{{.SubItemPrefix}}{{.Context.QueryStringOfSort .SortState.NextTimeSort}}">Time{{if eq $sortKey "t"}}↑{{else if eq $sortKey "T"}}↓{{end}}</a>
</span>
</li>
<li class="dir parent">
<a href="{{if .IsRoot}}./{{else}}../{{end}}{{$contextQueryString}}" class="detail">
<span class="field name">../</span>
<span class="field size"></span>
<span class="field time"></span>
</a>
</li>
{{range .SubItemsHtml}}
<li class="{{.Type}}">
<a href="{{.Url}}" class="detail">
<span class="field name">{{.DisplayName}}</span>
<span class="field size">{{.DisplaySize}}</span>
<span class="field time">{{.DisplayTime}}</span>
</a>
{{if .DeleteUrl}}<a href="{{.DeleteUrl}}" class="delete" onclick="return confirmDelete(this)">x</a>{{end}}
</li>
{{end}}
</ul>
{{if eq .Status 403}}
<div class="error">403 resource is forbidden</div>
{{else if eq .Status 404}}
<div class="error">404 resource not found</div>
{{else if eq .Status 500}}
<div class="error">500 potential issue occurred</div>
{{end}}
<script type="text/javascript" src="{{.RootRelPath}}?asset=main.js" defer="defer" async="async"></script>
</body>
</html>
`
var defaultPageTpl *template.Template
func init() {
pageTpl := template.New("page")
pageTpl = addFuncMap(pageTpl)
var err error
defaultPageTpl, err = pageTpl.Parse(pageTplStr)
if serverErrHandler.CheckError(err) {
defaultPageTpl = template.Must(pageTpl.Parse("Builtin Template Error"))
}
}
func LoadPageTpl(tplPath string) (*template.Template, error) {
if len(tplPath) == 0 {
return defaultPageTpl, nil
}
var err error
pageTpl := template.New(path.Base(tplPath))
pageTpl = addFuncMap(pageTpl)
pageTpl, err = pageTpl.ParseFiles(tplPath)
if err != nil {
pageTpl = defaultPageTpl
}
return pageTpl, err
}
func addFuncMap(tpl *template.Template) *template.Template {
return tpl.Funcs(template.FuncMap{
"fmtFilename": util.FormatFilename,
"fmtSize": util.FormatSize,
"fmtTime": util.FormatTime,
})
}