为双端群聊增加端到端加密附件
This commit is contained in:
1 parent
5988ad6ed6
commit
a95a007768
21 files changed
+405
-84
No files matched your search
@@ -19,17 +19,11 @@ func (a *API) uploadAttachment(w http.ResponseWriter, r *http.Request, _ string,
|
||||
if !a.limit(w, r, "attachment-upload:"+identity.Device.ID, 12) {
|
||||
return
|
||||
}
|
||||
body, err := io.ReadAll(http.MaxBytesReader(w, r.Body, delivery.AttachmentLimit))
|
||||
var tooLarge *http.MaxBytesError
|
||||
switch {
|
||||
case errors.As(err, &tooLarge):
|
||||
problem(w, 413, "attachment_too_large", "附件超过 8 MiB")
|
||||
return
|
||||
case err != nil || len(body) < 16:
|
||||
problem(w, 400, "invalid_attachment", "附件请求格式无效")
|
||||
body, ok := readAttachment(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
err = a.delivery.UploadAttachment(r.Context(), identity.Device.ID, recipient, id, body)
|
||||
err := a.delivery.UploadAttachment(r.Context(), identity.Device.ID, recipient, id, body)
|
||||
switch {
|
||||
case errors.Is(err, delivery.ErrUnavailable):
|
||||
problem(w, 404, "attachment_unavailable", "无法向指定设备发送附件")
|
||||
@@ -42,6 +36,20 @@ func (a *API) uploadAttachment(w http.ResponseWriter, r *http.Request, _ string,
|
||||
}
|
||||
}
|
||||
|
||||
func readAttachment(w http.ResponseWriter, r *http.Request) ([]byte, bool) {
|
||||
body, err := io.ReadAll(http.MaxBytesReader(w, r.Body, delivery.AttachmentLimit))
|
||||
var tooLarge *http.MaxBytesError
|
||||
switch {
|
||||
case errors.As(err, &tooLarge):
|
||||
problem(w, 413, "attachment_too_large", "附件超过 8 MiB")
|
||||
return nil, false
|
||||
case err != nil || len(body) < 16:
|
||||
problem(w, 400, "invalid_attachment", "附件请求格式无效")
|
||||
return nil, false
|
||||
}
|
||||
return body, true
|
||||
}
|
||||
|
||||
func (a *API) downloadAttachment(w http.ResponseWriter, r *http.Request, _ string, identity account.Identity) {
|
||||
id := r.PathValue("id")
|
||||
if !uuidPattern.MatchString(id) {
|
||||
@@ -58,8 +66,13 @@ func (a *API) downloadAttachment(w http.ResponseWriter, r *http.Request, _ strin
|
||||
case err != nil:
|
||||
unavailable(w, err)
|
||||
default:
|
||||
w.Header().Set("Content-Type", "application/octet-stream")
|
||||
w.Header().Set("Content-Disposition", "attachment")
|
||||
_, _ = w.Write(body)
|
||||
writeAttachment(w, body)
|
||||
}
|
||||
}
|
||||
|
||||
func writeAttachment(w http.ResponseWriter, body []byte) {
|
||||
w.Header().Set("Content-Type", "application/octet-stream")
|
||||
w.Header().Set("Content-Disposition", "attachment")
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
_, _ = w.Write(body)
|
||||
}
|
||||
Reference in new issue
Block a user