oficina / mapa Public

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

Plain git — no account needed to clone.

Download

Download this file

1package mapa
2
3import (
4 "context"
5 "errors"
6 "net/http"
7 "strings"
8)
9
10// The scriptless floor (images §4.4). The Markdown screen's form gains a
11// file input and a second button, Upload and insert, which posts the
12// textarea's text, the base revision and the file to the form's own
13// action with ?upload=1. The file is uploaded as a pending image, and
14// the screen is shown again (the re-render a refused save already does)
15// with ![alt](/i/<id>) added at the end on a line of its own and the
16// base unchanged. Nothing is committed and no draft is written: Save
17// commits as before, and the commit attaches the image.
18
19// maxFormText is the text's own bound on a form: Go's for a parsed
20// form body.
21const maxFormText = 10 << 20
22
23// addedNote is the screen's notice after an upload.
24const addedNote = "Image added at the end of the page. Save to keep it."
25
26// isUpload is a form post from Upload and insert.
27func isUpload(request *http.Request) bool {
28 return request.URL.Query().Get("upload") == "1"
29}
30
31// insertUpload uploads the file and answers the text to show again,
32// the status, and the sentence to show: the notice, or the refusal. A
33// status of 0 means it has answered already (errUnread).
34func (app *app) insertUpload(writer http.ResponseWriter,
35 request *http.Request) (string, int, string) {
36 image, err := app.receiveImage(writer, request, maxFormText)
37 if errors.Is(err, errUnread) {
38 writer.Header().Set("Retry-After", "5")
39 ErrorPage(writer, request, http.StatusServiceUnavailable, "")
40 return "", 0, ""
41 }
42 // The text came before the file, so it is here even when the file
43 // was refused (Fable L5).
44 body := request.PostFormValue("body")
45 if err == nil {
46 // No space at either end, as the editor's field keeps it.
47 alt := strings.TrimSpace(CleanAlt(request.PostFormValue("alt")))
48 return withImage(body, image.Markdown(alt)), http.StatusOK,
49 addedNote
50 }
51 status, say, ok := imageRefusal(err, localeIn(request))
52 if !ok {
53 app.logger.Error("image failed", "path", request.URL.Path,
54 "err", err)
55 status, say = http.StatusInternalServerError,
56 "The image could not be stored. Try again."
57 }
58 return body, status, say
59}
60
61// withImage is body with line at the end, a block of its own.
62func withImage(body, line string) string {
63 body = strings.TrimRight(body, " \t\r\n")
64 if body == "" {
65 return line
66 }
67 return body + "\n\n" + line
68}
69
70// uploadedKey carries an upload's sentence to the day's re-render.
71type uploadedKey struct{}
72
73type uploadedNote struct {
74 say string
75 added bool
76}
77
78func withUploaded(request *http.Request, say string,
79 added bool) *http.Request {
80 return request.WithContext(context.WithValue(request.Context(),
81 uploadedKey{}, uploadedNote{say: say, added: added}))
82}
83
84// uploadedOf is the upload's sentence, if this request was one.
85func uploadedOf(request *http.Request) (uploadedNote, bool) {
86 note, ok := request.Context().Value(uploadedKey{}).(uploadedNote)
87 return note, ok
88}
89
90// uploadOnEdit is Upload and insert on a page's Markdown screen.
91func (app *app) uploadOnEdit(writer http.ResponseWriter,
92 request *http.Request) {
93 // The page first: nothing is stored for one the person may not open
94 // (Fable L6).
95 page, ok := app.visible(writer, request)
96 if !ok {
97 return
98 }
99 body, status, say := app.insertUpload(writer, request)
100 if status == 0 {
101 return
102 }
103 form := pageForm{Page: page, Body: body,
104 Revision: formInt(request, "revision"),
105 Version: formInt(request, "version"),
106 Action: pageHref(page.Public, ""), Label: "Save",
107 Drafts: pageHref(page.Public, "") + "/draft"}
108 if status == http.StatusOK {
109 form.Notice = say
110 } else {
111 form.Error = say
112 }
113 app.showForm(writer, request, status, "page_edit", form)
114}
115
116// uploadOnScratch is Upload and insert on New scratch page's form.
117func (app *app) uploadOnScratch(writer http.ResponseWriter,
118 request *http.Request) {
119 body, status, say := app.insertUpload(writer, request)
120 if status == 0 {
121 return
122 }
123 form := pageForm{Action: "/scratch", Body: body,
124 Label: "New scratch page", Page: PageView{Kind: KindScratch}}
125 if status == http.StatusOK {
126 form.Notice = say
127 } else {
128 form.Error = say
129 }
130 app.showForm(writer, request, status, "scratch_new", form)
131}
132
133// uploadOnDay is Upload and insert on a day's Markdown screen.
134func (app *app) uploadOnDay(writer http.ResponseWriter,
135 request *http.Request, viewer Viewer, date string) {
136 body, status, say := app.insertUpload(writer, request)
137 if status == 0 {
138 return
139 }
140 request.PostForm.Set("body", body)
141 request = withUploaded(request, say, status == http.StatusOK)
142 problem := ""
143 if status != http.StatusOK {
144 problem = say
145 }
146 app.showDay(writer, request, viewer, date, status, problem)
147}
148