BlueGit

timfa bluegit

Files

bluegit / internal / server / media.go

media.go

feat: bied dezelfde acties als de UI aan via /api/v1
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
d89195e9 History
media.go 5.5 KiB Download Raw
1package server
2
3import (
4 "bytes"
5 "encoding/json"
6 "io"
7 "net/http"
8 "os"
9 "path/filepath"
10 "strconv"
11 "strings"
12 "time"
13
14 "gitlab.com/Timfa/bluegit/internal/access"
15 "gitlab.com/Timfa/bluegit/internal/i18n"
16 "gitlab.com/Timfa/bluegit/internal/logo"
17)
18
19func wantsModal(r *http.Request) bool {
20 return r.Header.Get("X-BlueGit") == "modal"
21}
22
23func writeModal(w http.ResponseWriter, ok bool, url, problem string, status int) {
24 if status == 0 {
25 status = http.StatusOK
26 }
27 w.Header().Set("Content-Type", "application/json; charset=utf-8")
28 w.WriteHeader(status)
29 _ = json.NewEncoder(w).Encode(map[string]any{"ok": ok, "url": url, "error": problem})
30}
31
32func (s *Server) finishCreate(w http.ResponseWriter, r *http.Request, path, notice string) {
33 if wantsModal(r) {
34 writeModal(w, true, path, "", http.StatusOK)
35 return
36 }
37 s.redirect(w, r, path, notice, "")
38}
39
40func (s *Server) failCreate(w http.ResponseWriter, r *http.Request, problem string) {
41 if wantsModal(r) {
42 writeModal(w, false, "", problem, http.StatusBadRequest)
43 return
44 }
45 s.redirect(w, r, "/new", "", problem)
46}
47
48func (s *Server) csrfValue(w http.ResponseWriter, r *http.Request) bool {
49 p := s.who(r)
50 if !p.OK || r.FormValue("csrf") == "" || r.FormValue("csrf") != p.Session.CSRF {
51 http.Error(w, s.tr(r, "err.badCSRF"), http.StatusBadRequest)
52 return false
53 }
54 return true
55}
56
57func (s *Server) saveLanguage(w http.ResponseWriter, r *http.Request) {
58 u, ok := s.loginRequired(w, r)
59 if !ok || !s.csrfOK(w, r) {
60 return
61 }
62 lang, err := s.opSetLanguage(r.Context(), u, r.FormValue("language"))
63 if err != nil {
64 s.redirect(w, r, "/settings", "", s.trErr(r, err))
65 return
66 }
67 s.redirect(w, r, "/settings", i18n.T(lang, "flash.language"), "")
68}
69
70func (s *Server) saveGroup(w http.ResponseWriter, r *http.Request) {
71 g, _, ok := s.guardGroup(w, r, access.RoleMaintainer)
72 if !ok || !s.csrfOK(w, r) {
73 return
74 }
75 if err := s.opUpdateGroup(r.Context(), s.who(r).User, g, r.FormValue("description"), r.FormValue("visibility"), r.FormValue("default_role")); err != nil {
76 s.redirect(w, r, "/groups/"+g.Path+"?tab=settings", "", s.commitProblem(r, err))
77 return
78 }
79 s.redirect(w, r, "/groups/"+g.Path+"?tab=settings", s.tr(r, "flash.saved"), "")
80}
81
82func (s *Server) toggleStar(w http.ResponseWriter, r *http.Request) {
83 u, repo, _, ok := s.guard(w, r, access.RoleReadonly)
84 if !ok || !s.csrfOK(w, r) {
85 return
86 }
87 if _, err := s.opToggleStar(r.Context(), u, repo); err != nil {
88 if !s.uiOK(w, r, repoURL(repo), err) {
89 return
90 }
91 }
92 back := r.FormValue("back")
93 if !strings.HasPrefix(back, "/") || strings.HasPrefix(back, "//") || strings.Contains(back, "://") {
94 back = repoURL(repo)
95 }
96 s.redirect(w, r, back, s.tr(r, "flash.star"), "")
97}
98
99func (s *Server) repoLogo(w http.ResponseWriter, r *http.Request) {
100 _, repo, _, ok := s.guard(w, r, access.RoleReadonly)
101 if !ok {
102 return
103 }
104 s.serveLogo(w, r, "repo", repo.ID, repo.LogoMIME)
105}
106
107func (s *Server) groupLogo(w http.ResponseWriter, r *http.Request) {
108 g, _, ok := s.guardGroup(w, r, access.RoleReadonly)
109 if !ok {
110 return
111 }
112 s.serveLogo(w, r, "group", g.ID, g.LogoMIME)
113}
114
115func (s *Server) uploadRepoLogo(w http.ResponseWriter, r *http.Request) {
116 _, repo, _, ok := s.guard(w, r, access.RoleMaintainer)
117 if !ok || !s.readUpload(w, r) {
118 return
119 }
120 if err := s.storeLogo(r, "repo", repo.ID); err != nil {
121 s.redirect(w, r, repoURL(repo)+"/-/settings", "", s.trErr(r, err))
122 return
123 }
124 s.redirect(w, r, repoURL(repo)+"/-/settings", s.tr(r, "flash.logo"), "")
125}
126
127func (s *Server) uploadGroupLogo(w http.ResponseWriter, r *http.Request) {
128 g, _, ok := s.guardGroup(w, r, access.RoleMaintainer)
129 if !ok || !s.readUpload(w, r) {
130 return
131 }
132 if err := s.storeLogo(r, "group", g.ID); err != nil {
133 s.redirect(w, r, "/groups/"+g.Path+"?tab=settings", "", s.trErr(r, err))
134 return
135 }
136 s.redirect(w, r, "/groups/"+g.Path+"?tab=settings", s.tr(r, "flash.logo"), "")
137}
138
139func (s *Server) readUpload(w http.ResponseWriter, r *http.Request) bool {
140 if err := r.ParseMultipartForm(logo.MaxUpload + 4096); err != nil {
141 http.Error(w, s.tr(r, "err.badForm"), http.StatusBadRequest)
142 return false
143 }
144 return s.csrfValue(w, r)
145}
146
147func (s *Server) storeLogo(r *http.Request, kind string, id int64) error {
148 f, hdr, err := r.FormFile("logo")
149 if err != nil {
150 return i18n.E("err.logoRead")
151 }
152 defer f.Close()
153 if hdr.Size > logo.MaxUpload {
154 return i18n.E("err.logoBig")
155 }
156 raw, err := io.ReadAll(io.LimitReader(f, logo.MaxUpload+1))
157 if err != nil {
158 return i18n.E("err.logoRead")
159 }
160 data, mime, err := logo.Normalize(raw)
161 if err != nil {
162 return err
163 }
164 path, err := s.logoPath(kind, id)
165 if err != nil {
166 return err
167 }
168 if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
169 return i18n.E("err.logoStore")
170 }
171 if err := os.WriteFile(path, data, 0o644); err != nil {
172 return i18n.E("err.logoStore")
173 }
174 return s.Store.SetLogoMIME(r.Context(), kind, id, mime)
175}
176
177func (s *Server) serveLogo(w http.ResponseWriter, r *http.Request, kind string, id int64, mime string) {
178 if mime == "" {
179 http.NotFound(w, r)
180 return
181 }
182 path, err := s.logoPath(kind, id)
183 if err != nil {
184 http.NotFound(w, r)
185 return
186 }
187 data, err := os.ReadFile(path)
188 if err != nil {
189 http.NotFound(w, r)
190 return
191 }
192 w.Header().Set("Content-Type", mime)
193 w.Header().Set("Cache-Control", "private, max-age=60")
194 http.ServeContent(w, r, "", time.Time{}, bytes.NewReader(data))
195}
196
197func (s *Server) logoPath(kind string, id int64) (string, error) {
198 if s.StateDir == "" {
199 return "", i18n.E("err.logoStore")
200 }
201 return filepath.Join(s.StateDir, "logos", kind+"-"+strconv.FormatInt(id, 10)), nil
202}