oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package chat
2
3import (
4 "io"
5 "mime"
6 "net/http"
7 "strconv"
8 "strings"
9
10 "amadan.net/oficina/equipo/internal/attach"
11 "amadan.net/oficina/equipo/internal/equipo/personapi"
12 "amadan.net/oficina/equipo/internal/wire"
13 "github.com/go-chi/chi/v5"
14)
15
16// ReadUpload reads one upload: the file's bytes as the body, its type in
17// Content-Type and its name in Content-Disposition (filename or
18// filename*). A body over the limit is ErrAttachmentTooLarge.
19func ReadUpload(r *http.Request) (name, declared string, data []byte, err error) {
20 if r.ContentLength > attach.MaxBytes {
21 return "", "", nil, ErrAttachmentTooLarge
22 }
23 data, err = io.ReadAll(io.LimitReader(r.Body, attach.MaxBytes+1))
24 if err != nil {
25 return "", "", nil, ErrInvalid
26 }
27 if len(data) > attach.MaxBytes {
28 return "", "", nil, ErrAttachmentTooLarge
29 }
30 if _, params, e := mime.ParseMediaType(r.Header.Get("Content-Disposition")); e == nil {
31 name = params["filename"]
32 }
33 return name, r.Header.Get("Content-Type"), data, nil
34}
35
36// upload is POST /api/v1/attachments.
37func (h *HTTP) upload(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) {
38 name, declared, data, err := ReadUpload(r)
39 if err == nil {
40 var out wire.MessageAttachment
41 if out, err = h.Store.Upload(r.Context(), p.Address, name, declared, data); err == nil {
42 writeJSON(w, http.StatusCreated, out)
43 return
44 }
45 }
46 h.fail(w, r, err)
47}
48
49// deleteUpload is DELETE /api/v1/attachments/{id}: the person removes an
50// upload they have not sent. 204 once it is gone.
51func (h *HTTP) deleteUpload(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) {
52 if err := h.Store.DeleteUnsent(r.Context(), p.Address, chi.URLParam(r, "id")); err != nil {
53 h.fail(w, r, err)
54 return
55 }
56 w.WriteHeader(http.StatusNoContent)
57}
58
59// attachment is GET /api/v1/attachments/{id} and its preview.
60func (h *HTTP) attachment(preview bool) handler {
61 return func(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) {
62 ServeAttachment(w, r, h.Store, p.Address, chi.URLParam(r, "id"), preview, func(err error) { h.fail(w, r, err) })
63 }
64}
65
66// ServeAttachment writes one of the person's attachments with its sniffed
67// type and nothing a browser could run: nosniff and a sandboxing CSP.
68// Images show inline; every other file downloads.
69func ServeAttachment(w http.ResponseWriter, r *http.Request, s *Store, person, id string, preview bool, fail func(error)) {
70 meta, data, err := s.AttachmentData(r.Context(), person, id, preview)
71 if err != nil {
72 fail(err)
73 return
74 }
75 serveFile(w, r, meta.MediaType, meta.Name, data)
76}
77
78// taskFile is GET /api/v1/tasks/{id}/files/{file}: an image a Research
79// step of the task kept, for anyone who reads the task, served as an
80// attachment is.
81func (h *HTTP) taskFile(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) {
82 ServeTaskFile(w, r, h.Store, p.Address, chi.URLParam(r, "id"), chi.URLParam(r, "file"), func(err error) { h.fail(w, r, err) })
83}
84
85// ServeTaskFile writes a task's kept image for person, under the same
86// read check and headers as the person API (the task page's images).
87func ServeTaskFile(w http.ResponseWriter, r *http.Request, s *Store, person, taskID, id string, fail func(error)) {
88 name, data, err := s.TaskFile(r.Context(), person, taskID, id)
89 if err != nil {
90 fail(err)
91 return
92 }
93 serveFile(w, r, attach.PNG, name, data)
94}
95
96// serveFile writes a stored file with nothing a browser could run.
97func serveFile(w http.ResponseWriter, r *http.Request, media, name string, data []byte) {
98 kind := media
99 if strings.HasPrefix(media, "text/") || media == attach.JSON {
100 media += "; charset=utf-8"
101 }
102 // An image shows inline; every other file downloads (Astra round 1:
103 // text accepted as plain text may be HTML).
104 disposition := "attachment"
105 if attach.IsImage(kind) {
106 disposition = "inline"
107 }
108 h := w.Header()
109 h.Set("Content-Type", media)
110 h.Set("Content-Length", strconv.Itoa(len(data)))
111 h.Set("Content-Disposition", mime.FormatMediaType(disposition, map[string]string{"filename": name}))
112 h.Set("X-Content-Type-Options", "nosniff")
113 h.Set("Content-Security-Policy", "default-src 'none'; img-src 'self'; style-src 'unsafe-inline'; sandbox")
114 // A stored file never changes; only its readers may cache it.
115 h.Set("Cache-Control", "private, max-age=86400")
116 w.WriteHeader(http.StatusOK)
117 if r.Method != http.MethodHead {
118 _, _ = w.Write(data)
119 }
120}
121