-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver_ui.go
298 lines (241 loc) · 8.56 KB
/
server_ui.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright (C) 2020 David Vogel
//
// This file is part of Galago.
//
// Galago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// Galago is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Galago. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"archive/zip"
"compress/flate"
"fmt"
"html/template"
"io"
"net/http"
"path/filepath"
"regexp"
"strconv"
"time"
"github.com/coreos/go-semver/semver"
)
var templateFuncmap = template.FuncMap{
"filterAlbums": FilterAlbums,
"filterImages": FilterImages,
"filterNonEmpty": FilterNonEmpty,
"filterContainers": FilterContainers,
"imageToDataURI": ImageToDataURI,
"previousElement": PreviousElement,
"nextElement": NextElement,
"getPreviewImages": GetPreviewImages,
"getHomeElement": GetHomeElement,
}
var isAlphanumeric = regexp.MustCompile(`^[0-9A-Za-z]+$`).MatchString
type uiTemplate struct {
template *template.Template
filename string
}
func newUITemplate(filename string) *uiTemplate {
return &uiTemplate{filename: filename}
}
// Template returns the template that should be used for rendering.
func (t *uiTemplate) Template() (*template.Template, error) {
// Only load and parse templates once!
if t.template != nil {
return t.template, nil
}
tpl := template.New("").Funcs(templateFuncmap)
var err error
fp := filepath.Join(".", "ui", "templates", "*.*html")
if tpl, err = tpl.ParseGlob(fp); err != nil {
return nil, fmt.Errorf("Couldn't parse templates from %q: %w", fp, err)
}
fp = filepath.Join(".", "ui", "templates", "webcomponents", "*.html")
if tpl, err = tpl.ParseGlob(fp); err != nil {
return nil, fmt.Errorf("Couldn't parse templates from %q: %w", fp, err)
}
fp = filepath.Join(".", "ui", "templates", "pages", t.filename)
if tpl, err = tpl.ParseFiles(fp); err != nil {
return nil, fmt.Errorf("Couldn't parse templates from %q: %w", fp, err)
}
// Store template
t.template = tpl
return tpl, nil
}
func (t *uiTemplate) ServeHTTP(w http.ResponseWriter, r *http.Request) {
timeStart := time.Now()
//path := mux.Vars(r)["path"]
var tpl *template.Template
var err error
if tpl, err = t.Template(); err != nil {
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
d := struct {
RootElement *Album
Version *semver.Version
Path string
}{
RootElement: RootElement,
Version: version,
Path: r.URL.Path,
}
if err := tpl.ExecuteTemplate(w, "base.gohtml", d); err != nil {
err = fmt.Errorf("Error executing template %q: %w", "base.gohtml", err)
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.Tracef("(IP: %v): Served template %q for URL %q in %v µs", r.RemoteAddr, t.filename, r.URL.Path, time.Now().Sub(timeStart).Microseconds())
}
type uiImage struct{}
func (t *uiImage) ServeHTTP(w http.ResponseWriter, r *http.Request) {
timeStart := time.Now()
element, err := RootElement.Traverse(r.URL.Path)
if err != nil {
log.Error(err)
http.Error(w, err.Error(), http.StatusNotFound) // Assume the error is because the element was not found
return
}
image, ok := element.(Image)
if !ok {
err := fmt.Errorf("Element %v is not an image", element)
log.Error(err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
imageFile, size, mime, err := image.FileContent()
if err != nil {
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer imageFile.Close()
w.Header().Set("Content-Type", mime)
w.Header().Set("Content-Length", strconv.FormatInt(size, 10))
w.Header().Set("Cache-Control", "public, max-age=86400") // 1 Day
io.Copy(w, imageFile)
log.Tracef("(IP: %v): Served original image %v in %v µs", r.RemoteAddr, element.Name(), time.Now().Sub(timeStart).Microseconds())
}
type uiCachedImage struct{}
func (t *uiCachedImage) ServeHTTP(w http.ResponseWriter, r *http.Request) {
timeStart := time.Now()
hash := r.URL.Path
// Make sure only alphanumeric hashes can be queried
if !isAlphanumeric(hash) {
log.Errorf("Invalid request. Tried to query cache element with hash %q", hash)
http.Error(w, "The hash can only be alphanumeric", http.StatusBadRequest)
return
}
ce, err := cache.QueryCacheEntryHash(hash)
if err != nil {
log.Error(err)
http.Error(w, err.Error(), http.StatusNotFound)
return
}
f, size, mime, err := ce.ReducedImage()
if err != nil {
log.Error(err)
http.Error(w, err.Error(), http.StatusNotFound)
return
}
defer f.Close()
w.Header().Set("Content-Type", mime)
w.Header().Set("Content-Length", strconv.FormatInt(size, 10))
w.Header().Set("Cache-Control", "public, max-age=2419200") // 4 weeks
io.Copy(w, f)
log.Tracef("(IP: %v): Sent reduced image %q in %v µs", r.RemoteAddr, r.URL.Path, time.Now().Sub(timeStart).Microseconds())
}
type uiDownload struct{}
func (t *uiDownload) ServeHTTP(w http.ResponseWriter, r *http.Request) {
timeStart := time.Now()
element, err := RootElement.Traverse(r.URL.Path)
if err != nil {
log.Error(err)
http.Error(w, err.Error(), http.StatusNotFound)
return
}
// Handle download of single image files
if img, ok := element.(Image); ok {
re, size, mime, err := img.FileContent()
if err != nil {
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer re.Close()
w.Header().Set("Content-Disposition", "attachment; filename="+element.URLName())
w.Header().Set("Content-Length", strconv.FormatInt(size, 10))
w.Header().Set("Content-Type", mime)
io.Copy(w, re)
log.Tracef("(IP: %v): Sent archived image %q in %v µs", r.RemoteAddr, element.Name(), time.Now().Sub(timeStart).Microseconds())
return
}
// Handle download of containers. This will pack all images contained in element into an archive and stream it to the browser.
if element.IsContainer() {
w.Header().Set("Content-Disposition", "attachment; filename="+element.Name()+".zip")
w.Header().Set("Content-Type", "application/zip")
zw := zip.NewWriter(w)
zw.RegisterCompressor(zip.Deflate, func(out io.Writer) (io.WriteCloser, error) {
return flate.NewWriter(out, flate.NoCompression)
})
children, err := element.Children()
if err != nil {
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
for _, child := range children {
if cImg, ok := child.(Image); ok {
cw, err := zw.Create(child.URLName())
if err != nil {
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
cr, _, _, err := cImg.FileContent()
if err != nil {
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer cr.Close()
if _, err := io.Copy(cw, cr); err != nil {
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
if err := zw.Close(); err != nil {
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.Tracef("(IP: %v): Sent %v archived images of %q in %v µs", r.RemoteAddr, len(children), element.Name(), time.Now().Sub(timeStart).Microseconds())
return
}
log.Tracef("(IP: %v): Requested file is neither an image nor any other supported element", r.RemoteAddr)
http.Error(w, "Requested file is neither an image nor any other supported element.", http.StatusBadRequest)
}
func serverUIInit() {
router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(filepath.Join(".", "ui", "static")))))
//router.Handle("/login", auth.LoginHandler(storage.StorageSessions, storage.StorageUsers))
//router.Handle("/logout", auth.LogoutHandler(storage.StorageSessions))
router.PathPrefix("/image/").Handler(http.StripPrefix("/image/", &uiImage{}))
router.PathPrefix("/cached/").Handler(http.StripPrefix("/cached/", &uiCachedImage{}))
router.PathPrefix("/download/").Handler(http.StripPrefix("/download/", &uiDownload{}))
router.Handle("/", http.StripPrefix("/", newUITemplate("gallery.gohtml")))
router.PathPrefix("/gallery/").Handler(http.StripPrefix("/gallery/", newUITemplate("gallery.gohtml")))
router.PathPrefix("/image-viewer/").Handler(http.StripPrefix("/image-viewer/", newUITemplate("image-viewer.gohtml")))
}