| 1 | package server |
| 2 | |
| 3 | import ( |
| 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 | |
| 19 | func wantsModal(r *http.Request) bool { |
| 20 | return r.Header.Get("X-BlueGit") == "modal" |
| 21 | } |
| 22 | |
| 23 | func 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 | |
| 32 | func (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 | |
| 40 | func (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 | |
| 48 | func (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 | |
| 57 | func (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 := i18n.Parse(r.FormValue("language")) |
| 63 | if err := s.Store.SetLanguage(r.Context(), u.ID, string(lang)); 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 | |
| 70 | func (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 | vis := r.FormValue("visibility") |
| 76 | if vis != "public" { |
| 77 | vis = "private" |
| 78 | } |
| 79 | role := g.DefaultRole |
| 80 | if raw := strings.TrimSpace(r.FormValue("default_role")); raw != "" { |
| 81 | parsed, err := access.ParseRole(raw) |
| 82 | if err != nil { |
| 83 | s.redirect(w, r, "/groups/"+g.Path+"?tab=settings", "", s.trErr(r, err)) |
| 84 | return |
| 85 | } |
| 86 | role = parsed |
| 87 | } |
| 88 | if err := s.Store.UpdateGroup(r.Context(), g.ID, r.FormValue("description"), vis, role); err != nil { |
| 89 | s.redirect(w, r, "/groups/"+g.Path+"?tab=settings", "", s.trErr(r, err)) |
| 90 | return |
| 91 | } |
| 92 | s.redirect(w, r, "/groups/"+g.Path+"?tab=settings", s.tr(r, "flash.saved"), "") |
| 93 | } |
| 94 | |
| 95 | func (s *Server) toggleStar(w http.ResponseWriter, r *http.Request) { |
| 96 | u, repo, _, ok := s.guard(w, r, access.RoleReadonly) |
| 97 | if !ok || !s.csrfOK(w, r) { |
| 98 | return |
| 99 | } |
| 100 | if u.ID == 0 { |
| 101 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 102 | return |
| 103 | } |
| 104 | _, mine, err := s.Store.StarInfo(r.Context(), u.ID, []int64{repo.ID}) |
| 105 | if err != nil { |
| 106 | s.redirect(w, r, repoURL(repo), "", s.trErr(r, err)) |
| 107 | return |
| 108 | } |
| 109 | if err := s.Store.SetStar(r.Context(), u.ID, repo.ID, !mine[repo.ID]); err != nil { |
| 110 | s.redirect(w, r, repoURL(repo), "", s.trErr(r, err)) |
| 111 | return |
| 112 | } |
| 113 | back := r.FormValue("back") |
| 114 | if !strings.HasPrefix(back, "/") || strings.HasPrefix(back, "//") || strings.Contains(back, "://") { |
| 115 | back = repoURL(repo) |
| 116 | } |
| 117 | s.redirect(w, r, back, s.tr(r, "flash.star"), "") |
| 118 | } |
| 119 | |
| 120 | func (s *Server) repoLogo(w http.ResponseWriter, r *http.Request) { |
| 121 | _, repo, _, ok := s.guard(w, r, access.RoleReadonly) |
| 122 | if !ok { |
| 123 | return |
| 124 | } |
| 125 | s.serveLogo(w, r, "repo", repo.ID, repo.LogoMIME) |
| 126 | } |
| 127 | |
| 128 | func (s *Server) groupLogo(w http.ResponseWriter, r *http.Request) { |
| 129 | g, _, ok := s.guardGroup(w, r, access.RoleReadonly) |
| 130 | if !ok { |
| 131 | return |
| 132 | } |
| 133 | s.serveLogo(w, r, "group", g.ID, g.LogoMIME) |
| 134 | } |
| 135 | |
| 136 | func (s *Server) uploadRepoLogo(w http.ResponseWriter, r *http.Request) { |
| 137 | _, repo, _, ok := s.guard(w, r, access.RoleMaintainer) |
| 138 | if !ok || !s.readUpload(w, r) { |
| 139 | return |
| 140 | } |
| 141 | if err := s.storeLogo(r, "repo", repo.ID); err != nil { |
| 142 | s.redirect(w, r, repoURL(repo)+"/-/settings", "", s.trErr(r, err)) |
| 143 | return |
| 144 | } |
| 145 | s.redirect(w, r, repoURL(repo)+"/-/settings", s.tr(r, "flash.logo"), "") |
| 146 | } |
| 147 | |
| 148 | func (s *Server) uploadGroupLogo(w http.ResponseWriter, r *http.Request) { |
| 149 | g, _, ok := s.guardGroup(w, r, access.RoleMaintainer) |
| 150 | if !ok || !s.readUpload(w, r) { |
| 151 | return |
| 152 | } |
| 153 | if err := s.storeLogo(r, "group", g.ID); err != nil { |
| 154 | s.redirect(w, r, "/groups/"+g.Path+"?tab=settings", "", s.trErr(r, err)) |
| 155 | return |
| 156 | } |
| 157 | s.redirect(w, r, "/groups/"+g.Path+"?tab=settings", s.tr(r, "flash.logo"), "") |
| 158 | } |
| 159 | |
| 160 | func (s *Server) readUpload(w http.ResponseWriter, r *http.Request) bool { |
| 161 | if err := r.ParseMultipartForm(logo.MaxUpload + 4096); err != nil { |
| 162 | http.Error(w, s.tr(r, "err.badForm"), http.StatusBadRequest) |
| 163 | return false |
| 164 | } |
| 165 | return s.csrfValue(w, r) |
| 166 | } |
| 167 | |
| 168 | func (s *Server) storeLogo(r *http.Request, kind string, id int64) error { |
| 169 | f, hdr, err := r.FormFile("logo") |
| 170 | if err != nil { |
| 171 | return i18n.E("err.logoRead") |
| 172 | } |
| 173 | defer f.Close() |
| 174 | if hdr.Size > logo.MaxUpload { |
| 175 | return i18n.E("err.logoBig") |
| 176 | } |
| 177 | raw, err := io.ReadAll(io.LimitReader(f, logo.MaxUpload+1)) |
| 178 | if err != nil { |
| 179 | return i18n.E("err.logoRead") |
| 180 | } |
| 181 | data, mime, err := logo.Normalize(raw) |
| 182 | if err != nil { |
| 183 | return err |
| 184 | } |
| 185 | path, err := s.logoPath(kind, id) |
| 186 | if err != nil { |
| 187 | return err |
| 188 | } |
| 189 | if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 190 | return i18n.E("err.logoStore") |
| 191 | } |
| 192 | if err := os.WriteFile(path, data, 0o644); err != nil { |
| 193 | return i18n.E("err.logoStore") |
| 194 | } |
| 195 | return s.Store.SetLogoMIME(r.Context(), kind, id, mime) |
| 196 | } |
| 197 | |
| 198 | func (s *Server) serveLogo(w http.ResponseWriter, r *http.Request, kind string, id int64, mime string) { |
| 199 | if mime == "" { |
| 200 | http.NotFound(w, r) |
| 201 | return |
| 202 | } |
| 203 | path, err := s.logoPath(kind, id) |
| 204 | if err != nil { |
| 205 | http.NotFound(w, r) |
| 206 | return |
| 207 | } |
| 208 | data, err := os.ReadFile(path) |
| 209 | if err != nil { |
| 210 | http.NotFound(w, r) |
| 211 | return |
| 212 | } |
| 213 | w.Header().Set("Content-Type", mime) |
| 214 | w.Header().Set("Cache-Control", "private, max-age=60") |
| 215 | http.ServeContent(w, r, "", time.Time{}, bytes.NewReader(data)) |
| 216 | } |
| 217 | |
| 218 | func (s *Server) logoPath(kind string, id int64) (string, error) { |
| 219 | if s.StateDir == "" { |
| 220 | return "", i18n.E("err.logoStore") |
| 221 | } |
| 222 | return filepath.Join(s.StateDir, "logos", kind+"-"+strconv.FormatInt(id, 10)), nil |
| 223 | } |