-
Notifications
You must be signed in to change notification settings - Fork 10
/
web.go
131 lines (122 loc) · 3.85 KB
/
web.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
package main
import (
_ "embed"
"fmt"
"html"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/spf13/afero"
)
var (
//go:embed favicon.ico
favIcn []byte
disTag = map[bool]string{
true: "",
false: "DISABLED",
}
charset = map[bool]string{
true: "UTF-8",
false: "ISO-8859-1",
}
padding = map[bool]string{
true: "2px",
false: "0px",
}
)
func htErr(w http.ResponseWriter, msg string, err error) {
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Cache-Control", *cacheCtl)
fmt.Fprintln(w, msg, ":", err)
log.Printf("error: %v : %v", msg, err)
}
func header(w http.ResponseWriter, uDir, sort, extraCSS string, modern bool) {
eDir := html.EscapeString(uDir)
w.Header().Set("Content-Type", "text/html")
w.Header().Set("Cache-Control", *cacheCtl)
w.Write([]byte(`<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML LANG="en">
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=` + charset[modern] + `">
<META HTTP-EQUIV="Content-Language" CONTENT="en-US">
<META HTTP-EQUIV="google" CONTENT="notranslate">
<META HTTP-EQUIV="charset" CONTENT="` + charset[modern] + `">
<META HTTP-EQUIV="encoding" CONTENT="` + charset[modern] + `">
<META NAME="viewport" CONTENT="width=device-width">
<META NAME="description" CONTENT="` + *siteDesc + `">
<LINK REL="icon" TYPE="image/x-icon" HREF="/favicon.ico">
<LINK REL="shortcut icon" HREF="/favicon.ico?">
<TITLE>` + *siteName + ` : ` + eDir + `</TITLE>
<STYLE TYPE="text/css"><!--
A:link {text-decoration: none; color:#0000CE; }
A:visited {text-decoration: none; color:#0000CE; }
A:active {text-decoration: none; color:#FF0000; }
A:hover {text-decoration: none; background-color: #FF8000; color: #FFFFFF; }
html, body, table { margin:0px; padding:0px; border:none; }
td, th { font-family: Tahoma, Arial, Geneva, sans-serif; font-size:13px; margin:0px; padding:` + padding[modern] + `; border:none; }
input { font-family: Tahoma, Arial, Geneva, sans-serif; font-size:13px; }
.thov tr:hover { background-color: #FF8000; color: #FFFFFF; }
.tbr { border-width: 1px; border-style: solid solid solid solid; border-color: #AAAAAA #555555 #555555 #AAAAAA; }
.nb { border-style:none; background-color: #EEEEEE; }
` + extraCSS + `
--></STYLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<FORM ACTION="` + wfmPfx + `" METHOD="POST" ENCTYPE="multipart/form-data">
<INPUT TYPE="hidden" NAME="dir" VALUE="` + eDir + `">
<INPUT TYPE="hidden" NAME="sort" VALUE="` + sort + `">
`))
}
func footer(w http.ResponseWriter) {
w.Write([]byte("\n</FORM></BODY></HTML>\n"))
}
func redirect(w http.ResponseWriter, uUrl string) {
w.Header().Set("Location", uUrl)
w.Header().Set("Content-Type", "text/html")
w.Header().Set("Cache-Control", *cacheCtl)
w.WriteHeader(302)
u := html.EscapeString(uUrl)
w.Write([]byte(`<HTML>
<HEAD>
<META HTTP-EQUIV="refresh" CONTENT="0; URL=` + u + `">
</HEAD>
<BODY>
If you see this, your browser did not redirect. <A HREF="` + u + `">Click here...</A>
</BODY>
</HTML>
`))
}
func upDnDir(uDir, uBn string, wfs afero.Fs) string {
o := strings.Builder{}
o.WriteString("<OPTION VALUE=\"/\">/ - Root</OPTION>\n")
p := "/"
i := 0
for _, n := range strings.Split(uDir, string(os.PathSeparator))[1:] {
p = p + n + "/"
opt := ""
if p == uDir+"/" {
opt = "DISABLED"
}
i++
o.WriteString("<OPTION " + opt + " VALUE=\"" +
html.EscapeString(filepath.Clean(p+"/"+uBn)) + "\">" +
emit(" ", i) + " L " +
html.EscapeString(n) + "</OPTION>\n")
}
d, err := afero.ReadDir(wfs, uDir)
if err != nil {
return o.String()
}
for _, n := range d {
if !n.IsDir() || strings.HasPrefix(n.Name(), ".") {
continue
}
o.WriteString("<OPTION VALUE=\"" +
html.EscapeString(uDir+"/"+n.Name()+"/"+uBn) + "\">" +
emit(" ", i) + " L " +
html.EscapeString(n.Name()) + "</OPTION>\n")
}
return o.String()
}