-
Notifications
You must be signed in to change notification settings - Fork 0
/
directory-response-handler.go
158 lines (144 loc) · 4.22 KB
/
directory-response-handler.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package gfs
import (
"html/template"
"log"
"net/http"
"time"
)
const (
//language=html
LoginHtml = `
<h2>Login</h2>
<form action="/login" method="post">
<label for="usernameInput">Username</label>
<input name="username" id="usernameInput" type="text" required />
<label for="passwordInput">Password</label>
<input name="password" id="passwordInput" type="password" required />
<input type="hidden" name="redirectTo" value="{{.Path}}" />
<button type="submit">Login</button>
</form>`
//language=html
UploadHtml string = `<form enctype="multipart/form-data" name="uploadFilesForm" id="uploadFilesForm" action="/upload" method="post">
<input type="file" multiple="multiple" name="uploadfiles"/>
<input type="hidden" name="path" value="{{.Path}}"/>
<button type="submit" form="uploadFilesForm">Upload</button>
</form>
`
//language=html
DirectoryResponseHtml string = `<!DOCTYPE html>
<html>
<head>
<title>{{.Path}}</title>
</head>
<body>
<h1><a href="{{.Path}}">{{.Name}}</a> <small>last modified: {{.LastModificationTime}}</small></h1>
<hr />
<table>
<thead>
<tr>
<th>Name</th>
<th>Size</th>
<th>Last modified</th>
</tr>
</thead>
<tbody>
{{range .Entries}}
<tr>
<td><a href="{{.Path}}">{{.Name}}</a></td>
<td>
{{if .IsDirectory}}
<Dir>
{{else}}
{{.Size}}
{{end}}
</td>
<td>
{{.LastModificationTime}}
</td>
</tr>
{{else}}
<tr>
<td colspan="3">No entries</td>
</tr>
{{end}}
</tbody>
</table>
<hr />
{{if .Authorized}}
{{template "upload" .}}
{{if .HasUpdate}}
<p>A new update is available for download at <a href="{{.UpdateUrl}}">{{.UpdateUrl}}</a>"</p>
{{end}}
{{else}}
{{template "login" .}}
{{end}}
</body>
</html>`
)
type DirectoryEntry struct {
// The name of the file
Name string `json:"name" xml:"name"`
// The path to the file. Relative to the serve root
Path string `json:"path" xml:"path"`
// The size of the file. 0 if a directory
Size int64 `json:"size,omitempty" xml:"size,omitempty"`
// True is directory. Probably false if not
IsDirectory bool `json:"is_directory" xml:"is_directory"`
// The last time this file was modified
LastModificationTime time.Time `json:"last_modification_time" xml:"last_modification_time"`
}
// Simple stats about a directory
type DirectoryStats struct {
// The name of the directory
Name string `json:"name" xml:"name"`
// The path to the directory. Relative to the serve root
Path string `json:"path" xml:"path"`
// The last time this file was modified
LastModificationTime time.Time `json:"last_modification_time" xml:"last_modification_time"`
// The content directory available in this directory
Entries []DirectoryEntry `json:"entries" xml:"entries"`
// Indicates if the request is authorized
Authorized bool `json:"authorized" xml:"authorized"`
// Indicates if a new update is available for GFS
HasUpdate bool `json:"has_update" xml:"has_update"`
// Indicates the url the update can be downloaded at, if HasUpdate is true
UpdateUrl string `json:"update_url" xml:"update_url"`
}
type DirectoryResponseHandler struct {
responseHandler
htmlTemplate *template.Template
loginTemplate *template.Template
uploadTemplate *template.Template
}
func GetDirectoryResponseHandler() (h *DirectoryResponseHandler, err error) {
t := template.New("Directory Response Html Template")
loginTemplate := t.New("login")
if loginTemplate, err = loginTemplate.Parse(LoginHtml); err != nil {
return nil, err
}
uploadTemplate := t.New("upload")
if uploadTemplate, err = uploadTemplate.Parse(UploadHtml); err != nil {
return nil, err
}
t, err = t.Parse(DirectoryResponseHtml)
if err != nil {
return nil, err
}
h = &DirectoryResponseHandler{
htmlTemplate: t,
uploadTemplate: uploadTemplate,
loginTemplate: loginTemplate,
}
return h, nil
}
// Write a not found message to the response.
func (h *DirectoryResponseHandler) Handle(writer http.ResponseWriter, stats *DirectoryStats, format string) {
if hasUpdate != "" {
stats.HasUpdate = true
stats.UpdateUrl = hasUpdate
}
err := h.responseHandler.WriteResponse(writer, http.StatusOK, h.htmlTemplate, format, stats)
if err != nil {
log.Println("Something went wrong when responding", err.Error())
}
}