-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml.go
More file actions
93 lines (84 loc) · 2.32 KB
/
html.go
File metadata and controls
93 lines (84 loc) · 2.32 KB
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
package utils
import (
"net/url"
"os"
"fmt"
"strings"
"path/filepath"
)
func Index(path string, files []os.DirEntry, showHidden bool) string {
var sb strings.Builder
var tf, oo string
if showHidden {
tf = "false"
oo = "on"
}else {
tf = "true"
oo = "off"
}
pp := fmt.Sprintf(
`<!DOCTYPE html>
<html>
<head>
<meta name="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>%s</title>
<style type="text/css">
body {
white-space: normal;
word-break: break-word;
overflow-wrap: anywhere;
overflow-x: hidden;
}
li { margin: 0.75em 0; }
</style>
</head>
<body>
<h2>Directory listing for %s</h2>
<a href="?showHidden=%s"><button>Show Hidden Files</button></a> %s
<p></p>
<form method="post" enctype="multipart/form-data">
<input type="file" name="file", required="required" multiple=""> >> <button type="submit">Upload</button>
</form>
<hr>
`, path, path, tf, oo)
sb.WriteString(pp)
// parent directory
if path == "/" {
sb.WriteString("/")
} else {
sb.WriteString("<a href=\"")
p := filepath.Dir(strings.TrimSuffix(path, "/"))
if p != "/"{
p += "/"
}
sb.WriteString(p)
sb.WriteString("\">")
sb.WriteString("Parent Directory</a>")
}
sb.WriteString("<ul>\n")
for _, f := range files {
if f.IsDir(){
li := fmt.Sprintf(
`<li>
<a href="%s/"><strong>%s</strong></a>
</li>
`, url.PathEscape(f.Name()), f.Name() + "/")
sb.WriteString(li)
}
}
for _, f := range files {
if !f.IsDir(){
li := fmt.Sprintf(`
<li>
<a href="%s">%s</a>  -  <a href="%s">%s</a>
</li>
`, url.PathEscape(f.Name()), f.Name(), url.PathEscape(f.Name()) + "?download", "↓")
sb.WriteString(li)
}
}
sb.WriteString("</ul>\n")
sb.WriteString("<hr>")
sb.WriteString("</body>\n</html>")
return sb.String()
}