| 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 | "gitlab.com/Timfa/bluegit/internal/store" |
| 18 | ) |
| 19 | |
| 20 | func wantsModal(r *http.Request) bool { |
| 21 | return r.Header.Get("X-BlueGit") == "modal" |
| 22 | } |
| 23 | |
| 24 | func writeModal(w http.ResponseWriter, ok bool, url, problem string, status int) { |
| 25 | if status == 0 { |
| 26 | status = http.StatusOK |
| 27 | } |
| 28 | w.Header().Set("Content-Type", "application/json; charset=utf-8") |
| 29 | w.WriteHeader(status) |
| 30 | _ = json.NewEncoder(w).Encode(map[string]any{"ok": ok, "url": url, "error": problem}) |
| 31 | } |
| 32 | |
| 33 | func (s *Server) finishCreate(w http.ResponseWriter, r *http.Request, path, notice string) { |
| 34 | if wantsModal(r) { |
| 35 | writeModal(w, true, path, "", http.StatusOK) |
| 36 | return |
| 37 | } |
| 38 | s.redirect(w, r, path, notice, "") |
| 39 | } |
| 40 | |
| 41 | func (s *Server) failCreate(w http.ResponseWriter, r *http.Request, problem string) { |
| 42 | if wantsModal(r) { |
| 43 | writeModal(w, false, "", problem, http.StatusBadRequest) |
| 44 | return |
| 45 | } |
| 46 | s.redirect(w, r, "/new", "", problem) |
| 47 | } |
| 48 | |
| 49 | func (s *Server) csrfValue(w http.ResponseWriter, r *http.Request) bool { |
| 50 | p := s.who(r) |
| 51 | if !p.OK || r.FormValue("csrf") == "" || r.FormValue("csrf") != p.Session.CSRF { |
| 52 | http.Error(w, s.tr(r, "err.badCSRF"), http.StatusBadRequest) |
| 53 | return false |
| 54 | } |
| 55 | return true |
| 56 | } |
| 57 | |
| 58 | func (s *Server) saveProfile(w http.ResponseWriter, r *http.Request) { |
| 59 | u, ok := s.loginRequired(w, r) |
| 60 | if !ok || !s.csrfOK(w, r) { |
| 61 | return |
| 62 | } |
| 63 | if _, err := s.opSetProfile(r.Context(), u, r.FormValue("name"), r.FormValue("bio")); err != nil { |
| 64 | s.redirect(w, r, "/settings", "", s.trErr(r, err)) |
| 65 | return |
| 66 | } |
| 67 | s.redirect(w, r, "/settings", s.tr(r, "flash.profile"), "") |
| 68 | } |
| 69 | |
| 70 | func (s *Server) saveLanguage(w http.ResponseWriter, r *http.Request) { |
| 71 | u, ok := s.loginRequired(w, r) |
| 72 | if !ok || !s.csrfOK(w, r) { |
| 73 | return |
| 74 | } |
| 75 | lang, err := s.opSetLanguage(r.Context(), u, r.FormValue("language")) |
| 76 | if err != nil { |
| 77 | s.redirect(w, r, "/settings", "", s.trErr(r, err)) |
| 78 | return |
| 79 | } |
| 80 | s.redirect(w, r, "/settings", i18n.T(lang, "flash.language"), "") |
| 81 | } |
| 82 | |
| 83 | func (s *Server) saveGroup(w http.ResponseWriter, r *http.Request) { |
| 84 | g, _, ok := s.guardGroup(w, r, access.RoleMaintainer) |
| 85 | if !ok || !s.csrfOK(w, r) { |
| 86 | return |
| 87 | } |
| 88 | if err := s.opUpdateGroup(r.Context(), s.who(r).User, g, r.FormValue("description"), r.FormValue("visibility"), r.FormValue("default_role")); err != nil { |
| 89 | s.redirect(w, r, "/groups/"+g.Path+"?tab=settings", "", s.commitProblem(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 _, err := s.opToggleStar(r.Context(), u, repo); err != nil { |
| 101 | if !s.uiOK(w, r, repoURL(repo), err) { |
| 102 | return |
| 103 | } |
| 104 | } |
| 105 | back := r.FormValue("back") |
| 106 | if !strings.HasPrefix(back, "/") || strings.HasPrefix(back, "//") || strings.Contains(back, "://") { |
| 107 | back = repoURL(repo) |
| 108 | } |
| 109 | s.redirect(w, r, back, s.tr(r, "flash.star"), "") |
| 110 | } |
| 111 | |
| 112 | func (s *Server) repoLogo(w http.ResponseWriter, r *http.Request) { |
| 113 | // Repo-logo volgt de Releases-regel: wie de releases mag zien, ziet ook |
| 114 | // het logo (privé repo met publieke releases); verder zoals guard. |
| 115 | repo, ok := s.logoRepo(w, r) |
| 116 | if !ok { |
| 117 | return |
| 118 | } |
| 119 | s.serveLogo(w, r, "repo", repo.ID, repo.LogoMIME) |
| 120 | } |
| 121 | |
| 122 | func (s *Server) groupLogo(w http.ResponseWriter, r *http.Request) { |
| 123 | g, _, ok := s.guardGroup(w, r, access.RoleReadonly) |
| 124 | if !ok { |
| 125 | return |
| 126 | } |
| 127 | s.serveLogo(w, r, "group", g.ID, g.LogoMIME) |
| 128 | } |
| 129 | |
| 130 | func (s *Server) uploadRepoLogo(w http.ResponseWriter, r *http.Request) { |
| 131 | _, repo, _, ok := s.guard(w, r, access.RoleMaintainer) |
| 132 | if !ok || !s.readUpload(w, r) { |
| 133 | return |
| 134 | } |
| 135 | if err := s.storeLogo(r, "repo", repo.ID); err != nil { |
| 136 | s.redirect(w, r, repoURL(repo)+"/-/settings", "", s.trErr(r, err)) |
| 137 | return |
| 138 | } |
| 139 | s.redirect(w, r, repoURL(repo)+"/-/settings", s.tr(r, "flash.logo"), "") |
| 140 | } |
| 141 | |
| 142 | func (s *Server) uploadGroupLogo(w http.ResponseWriter, r *http.Request) { |
| 143 | g, _, ok := s.guardGroup(w, r, access.RoleMaintainer) |
| 144 | if !ok || !s.readUpload(w, r) { |
| 145 | return |
| 146 | } |
| 147 | if err := s.storeLogo(r, "group", g.ID); err != nil { |
| 148 | s.redirect(w, r, "/groups/"+g.Path+"?tab=settings", "", s.trErr(r, err)) |
| 149 | return |
| 150 | } |
| 151 | s.redirect(w, r, "/groups/"+g.Path+"?tab=settings", s.tr(r, "flash.logo"), "") |
| 152 | } |
| 153 | |
| 154 | func (s *Server) readUpload(w http.ResponseWriter, r *http.Request) bool { |
| 155 | if err := r.ParseMultipartForm(logo.MaxUpload + 4096); err != nil { |
| 156 | http.Error(w, s.tr(r, "err.badForm"), http.StatusBadRequest) |
| 157 | return false |
| 158 | } |
| 159 | return s.csrfValue(w, r) |
| 160 | } |
| 161 | |
| 162 | func (s *Server) storeLogo(r *http.Request, kind string, id int64) error { |
| 163 | f, hdr, err := r.FormFile("logo") |
| 164 | if err != nil { |
| 165 | return i18n.E("err.logoRead") |
| 166 | } |
| 167 | defer f.Close() |
| 168 | if hdr.Size > logo.MaxUpload { |
| 169 | return i18n.E("err.logoBig") |
| 170 | } |
| 171 | raw, err := io.ReadAll(io.LimitReader(f, logo.MaxUpload+1)) |
| 172 | if err != nil { |
| 173 | return i18n.E("err.logoRead") |
| 174 | } |
| 175 | data, mime, err := logo.Normalize(raw) |
| 176 | if err != nil { |
| 177 | return err |
| 178 | } |
| 179 | path, err := s.logoPath(kind, id) |
| 180 | if err != nil { |
| 181 | return err |
| 182 | } |
| 183 | if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 184 | return i18n.E("err.logoStore") |
| 185 | } |
| 186 | if err := os.WriteFile(path, data, 0o644); err != nil { |
| 187 | return i18n.E("err.logoStore") |
| 188 | } |
| 189 | return s.Store.SetLogoMIME(r.Context(), kind, id, mime) |
| 190 | } |
| 191 | |
| 192 | func (s *Server) serveLogo(w http.ResponseWriter, r *http.Request, kind string, id int64, mime string) { |
| 193 | if mime == "" { |
| 194 | http.NotFound(w, r) |
| 195 | return |
| 196 | } |
| 197 | path, err := s.logoPath(kind, id) |
| 198 | if err != nil { |
| 199 | http.NotFound(w, r) |
| 200 | return |
| 201 | } |
| 202 | data, err := os.ReadFile(path) |
| 203 | if err != nil { |
| 204 | http.NotFound(w, r) |
| 205 | return |
| 206 | } |
| 207 | w.Header().Set("Content-Type", mime) |
| 208 | w.Header().Set("Cache-Control", "private, max-age=60") |
| 209 | http.ServeContent(w, r, "", time.Time{}, bytes.NewReader(data)) |
| 210 | } |
| 211 | |
| 212 | func (s *Server) logoPath(kind string, id int64) (string, error) { |
| 213 | if s.StateDir == "" { |
| 214 | return "", i18n.E("err.logoStore") |
| 215 | } |
| 216 | return filepath.Join(s.StateDir, "logos", kind+"-"+strconv.FormatInt(id, 10)), nil |
| 217 | } |
| 218 | |
| 219 | func (s *Server) logoRepo(w http.ResponseWriter, r *http.Request) (store.Repo, bool) { |
| 220 | if repo, err := s.Store.RepoByPath(r.Context(), r.PathValue("owner"), r.PathValue("repo")); err == nil && repo.Visibility != "public" && !isCrawler(r) { |
| 221 | if _, role, err := s.webRole(r, repo); err == nil && releaseOnly(repo, role) { |
| 222 | return repo, true |
| 223 | } |
| 224 | } |
| 225 | _, repo, _, ok := s.guard(w, r, access.RoleReadonly) |
| 226 | return repo, ok |
| 227 | } |