forked from carl-mastrangelo/pixur
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.go
92 lines (76 loc) · 1.8 KB
/
index.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
package handlers
import (
"html/template"
"net/http"
"pixur.org/pixur/api"
ptpl "pixur.org/pixur/fe/tpl"
)
type indexData struct {
*paneData
Pic []*api.PicAndThumbnail
NextID, PrevID string
CanUpload bool
}
var indexTpl = parseTpl(ptpl.Base, ptpl.Pane, ptpl.Index)
func parseTpl(tpls ...string) *template.Template {
t := template.New("NamesAreADumbIdeaForTemplates").Option("missingkey=error")
for i := len(tpls) - 1; i >= 0; i-- {
template.Must(t.Parse(tpls[i]))
}
return t
}
type indexHandler struct {
c api.PixurServiceClient
pt *paths
}
func (h *indexHandler) static(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if err := r.ParseForm(); err != nil {
httpReadError(ctx, w, &HTTPErr{
Message: err.Error(),
Code: http.StatusBadRequest,
})
return
}
id := r.FormValue(h.pt.pr.IndexPic())
_, isPrev := r.Form[h.pt.pr.IndexPrev()]
req := &api.FindIndexPicsRequest{
StartPicId: id,
Ascending: isPrev,
}
if canViewIndex := maybeHasCap(ctx, api.Capability_PIC_INDEX); !canViewIndex {
http.Redirect(w, r, h.pt.Login().String(), http.StatusSeeOther)
return
}
res, err := h.c.FindIndexPics(ctx, req)
if err != nil {
httpReadError(ctx, w, err)
return
}
var prevID string
var nextID string
if !isPrev {
nextID = res.NextPicId
prevID = res.PrevPicId
} else {
nextID = res.PrevPicId
prevID = res.NextPicId
}
if isPrev {
for i := 0; i < len(res.Pic)/2; i++ {
res.Pic[i], res.Pic[len(res.Pic)-i-1] = res.Pic[len(res.Pic)-i-1], res.Pic[i]
}
}
canupload := hasCap(ctx, api.Capability_PIC_CREATE)
data := indexData{
paneData: newPaneData(ctx, "Index", h.pt),
Pic: res.Pic,
NextID: nextID,
PrevID: prevID,
CanUpload: canupload,
}
if err := indexTpl.Execute(w, data); err != nil {
httpCleanupError(w, err)
return
}
}