package httpapi import ( "errors" "net/http" "xhchat/server/internal/account" "xhchat/server/internal/groups" ) func (a *API) uploadGroupAttachment(w http.ResponseWriter, r *http.Request, _ string, actor account.Identity) { group, id, epoch := r.PathValue("id"), r.PathValue("attachment"), r.URL.Query().Get("epoch") if !uuidPattern.MatchString(group) || !uuidPattern.MatchString(id) || !uuidPattern.MatchString(epoch) || r.Header.Get("Content-Type") != "application/octet-stream" { problem(w, 400, "invalid_attachment", "群附件请求格式无效") return } if !a.limit(w, r, "attachment-upload:"+actor.Device.ID, 12) { return } body, ok := readAttachment(w, r) if !ok { return } if err := a.groups.UploadAttachment(r.Context(), group, actor.User.ID, actor.Device.ID, epoch, id, body); err != nil { groupError(w, err) return } w.WriteHeader(204) } func (a *API) downloadGroupAttachment(w http.ResponseWriter, r *http.Request, _ string, actor account.Identity) { group, id := r.PathValue("id"), r.PathValue("attachment") if !uuidPattern.MatchString(group) || !uuidPattern.MatchString(id) { problem(w, 404, "attachment_unavailable", "群附件不可用") return } if !a.limit(w, r, "attachment-download:"+actor.Device.ID, 30) { return } body, err := a.groups.DownloadAttachment(r.Context(), group, actor.User.ID, actor.Device.ID, id) switch { case errors.Is(err, groups.ErrUnavailable): problem(w, 404, "attachment_unavailable", "群附件不可用或已过期") case err != nil: unavailable(w, err) default: writeAttachment(w, body) } }