-
Notifications
You must be signed in to change notification settings - Fork 2
/
handlers_site.go
98 lines (83 loc) · 2.44 KB
/
handlers_site.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
package main
import (
"io"
"log"
"net/http"
"github.com/mdbot/wiki/config"
)
func ViewSiteConfigHandler(t *Templates) http.HandlerFunc {
return t.RenderViewSiteConfig
}
type SiteUpdater interface {
Update(site *config.Site, responsible string) error
}
func UpdateSiteConfigHandler(updater SiteUpdater) http.HandlerFunc {
fileBytes := func(request *http.Request, name string) ([]byte, error) {
file, _, err := request.FormFile(name)
if err != nil {
if err == http.ErrMissingFile {
return nil, nil
}
return nil, err
}
defer file.Close()
return io.ReadAll(file)
}
return func(writer http.ResponseWriter, request *http.Request) {
if err := request.ParseMultipartForm(1 << 30); err != nil {
log.Printf("Manage site: couldn't parse multipart data: %v", err)
writer.WriteHeader(http.StatusBadRequest)
return
}
siteName := request.FormValue("name")
favicon, err := fileBytes(request, "favicon")
if err != nil {
log.Printf("Manage site: couldn't read favicon file: %v", err)
writer.WriteHeader(http.StatusBadRequest)
return
}
mainLogo, err := fileBytes(request, "logo")
if err != nil {
log.Printf("Manage site: couldn't read logo file: %v", err)
writer.WriteHeader(http.StatusBadRequest)
return
}
darkLogo, err := fileBytes(request, "darklogo")
if err != nil {
log.Printf("Manage site: couldn't read dark logo file: %v", err)
writer.WriteHeader(http.StatusBadRequest)
return
}
username := "Anonymoose"
if user := getUserForRequest(request); user != nil {
username = user.Name
}
if err := updater.Update(&config.Site{
Name: siteName,
Favicon: favicon,
MainLogo: mainLogo,
DarkLogo: darkLogo,
}, username); err != nil {
log.Printf("Manage site: unable to save new config: %v", err)
writer.WriteHeader(http.StatusInternalServerError)
return
}
writer.Header().Add("location", "/wiki/site")
writer.WriteHeader(http.StatusSeeOther)
}
}
func ServeFavicon(siteConfig *config.Site) http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
_, _ = writer.Write(siteConfig.Favicon)
}
}
func ServeMainLogo(siteConfig *config.Site) http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
_, _ = writer.Write(siteConfig.MainLogo)
}
}
func ServeDarkLogo(siteConfig *config.Site) http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
_, _ = writer.Write(siteConfig.DarkLogo)
}
}