Files
davidandClaude Opus 5.5 6d3eb18b5e Phase 14, parts 5 and 6: seats, and moving down to Free
Seats: inviting past the seats paid for offers the owner to add one
(Stripe prorates it); admins are told to ask the owner; Free has two.
Moving down: past the plan's seats, the owner and one other (the owner's
pick, or the first to join) edit and the rest read; storage is measured
hourly, and past the plan's, photo uploads and the SEO crawl stop.
Nothing is deleted. TEST.md section 18.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 13:34:22 -07:00

116 lines
2.8 KiB
Go

package web
import (
"bytes"
"errors"
"io"
"net/http"
"strconv"
"strings"
"supermarket/internal/users"
)
// Profile photos, kept in the file storage (photos/<user>-<random>.<ext>)
// and shown to people signed in.
const maxPhoto = 2 << 20
var photoTypes = map[string]string{"image/png": "png", "image/jpeg": "jpg", "image/webp": "webp", "image/gif": "gif"}
func (s *Server) accountPhoto(w http.ResponseWriter, r *http.Request) {
v := s.me(w, r)
if v == nil {
return
}
if s.Files == nil {
http.NotFound(w, r)
return
}
u, err := users.ByID(s.DB, v.User.ID)
if err != nil {
s.serverError(w, err)
return
}
fail := func(msg string) {
s.renderAccount(w, r, v, &accountData{Section: "profile", Problem: msg}, http.StatusUnprocessableEntity)
}
key := ""
if r.URL.Query().Get("remove") == "" {
if err := s.storageFull(); err != nil {
fail(sentence(err.Error()) + ".")
return
}
r.Body = http.MaxBytesReader(w, r.Body, maxPhoto+64<<10)
f, _, err := r.FormFile("photo")
if err != nil {
fail("Choose a picture of 2 MB or less (PNG, JPEG, WebP or GIF).")
return
}
defer f.Close()
data, err := io.ReadAll(io.LimitReader(f, maxPhoto+1))
if err != nil || len(data) > maxPhoto || len(data) == 0 {
fail("Choose a picture of 2 MB or less (PNG, JPEG, WebP or GIF).")
return
}
ext, ok := photoTypes[http.DetectContentType(data)]
if !ok {
fail("That isn't a PNG, JPEG, WebP or GIF picture.")
return
}
token, err := users.NewToken()
if err != nil {
s.serverError(w, err)
return
}
key = "photos/" + strconv.FormatInt(u.ID, 10) + "-" + token[:12] + "." + ext
if err := s.Files.Put(r.Context(), key, bytes.NewReader(data), int64(len(data))); err != nil {
s.serverError(w, err)
return
}
}
old := u.Photo
u.Photo = key
if err := users.SaveProfile(s.DB, u); err != nil {
s.serverError(w, err)
return
}
if old != "" {
if err := s.Files.Delete(r.Context(), old); err != nil {
s.Log.Warn("account: removing the old photo", "file", old, "err", err)
}
}
redirect(w, r, "/settings/account", "saved")
}
// userPhoto shows someone's photo to people signed in.
func (s *Server) userPhoto(w http.ResponseWriter, r *http.Request) {
if s.me(w, r) == nil {
return
}
id, _ := strconv.ParseInt(r.PathValue("id"), 10, 64)
u, err := users.ByID(s.DB, id)
if errors.Is(err, users.ErrNotFound) || (err == nil && (u.Photo == "" || s.Files == nil)) {
http.NotFound(w, r)
return
}
if err != nil {
s.serverError(w, err)
return
}
rc, err := s.Files.Get(r.Context(), u.Photo)
if err != nil {
http.NotFound(w, r)
return
}
defer rc.Close()
ext := u.Photo[strings.LastIndex(u.Photo, ".")+1:]
for t, e := range photoTypes {
if e == ext {
w.Header().Set("Content-Type", t)
}
}
w.Header().Set("Cache-Control", "private, max-age=3600")
_, _ = io.Copy(w, rc)
}