Add file upload to editor

This commit is contained in:
Jan-Lukas Else 2020-12-13 11:28:46 +01:00
parent 54a1f699b3
commit 796470c067
4 changed files with 24 additions and 12 deletions

View File

@ -16,7 +16,8 @@ func serveEditor(w http.ResponseWriter, r *http.Request) {
func serveEditorPost(w http.ResponseWriter, r *http.Request) { func serveEditorPost(w http.ResponseWriter, r *http.Request) {
if action := r.FormValue("editoraction"); action != "" { if action := r.FormValue("editoraction"); action != "" {
if action == "loadupdate" { switch action {
case "loadupdate":
parsedURL, err := url.Parse(r.FormValue("url")) parsedURL, err := url.Parse(r.FormValue("url"))
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
@ -34,8 +35,7 @@ func serveEditorPost(w http.ResponseWriter, r *http.Request) {
"UpdatePostContent": mf.Properties.Content[0], "UpdatePostContent": mf.Properties.Content[0],
}, },
}) })
return case "updatepost":
} else if action == "updatepost" {
urlValue := r.FormValue("url") urlValue := r.FormValue("url")
content := r.FormValue("content") content := r.FormValue("content")
mf := map[string]interface{}{ mf := map[string]interface{}{
@ -58,18 +58,24 @@ func serveEditorPost(w http.ResponseWriter, r *http.Request) {
return return
} }
req.Header.Set(contentType, contentTypeJSON) req.Header.Set(contentType, contentTypeJSON)
editorMicropubPost(w, req) editorMicropubPost(w, req, false)
return case "upload":
editorMicropubPost(w, r, true)
default:
http.Error(w, "unknown editoraction", http.StatusBadRequest)
} }
http.Error(w, "unknown editoraction", http.StatusBadRequest)
return return
} }
editorMicropubPost(w, r) editorMicropubPost(w, r, false)
} }
func editorMicropubPost(w http.ResponseWriter, r *http.Request) { func editorMicropubPost(w http.ResponseWriter, r *http.Request, media bool) {
recorder := httptest.NewRecorder() recorder := httptest.NewRecorder()
addAllScopes(http.HandlerFunc(serveMicropubPost)).ServeHTTP(recorder, r) if media {
addAllScopes(http.HandlerFunc(serveMicropubMedia)).ServeHTTP(recorder, r)
} else {
addAllScopes(http.HandlerFunc(serveMicropubPost)).ServeHTTP(recorder, r)
}
result := recorder.Result() result := recorder.Result()
if location := result.Header.Get("Location"); location != "" { if location := result.Header.Get("Location"); location != "" {
http.Redirect(w, r, result.Header.Get("Location"), http.StatusFound) http.Redirect(w, r, result.Header.Get("Location"), http.StatusFound)

View File

@ -77,8 +77,7 @@ func serveMicropubMedia(w http.ResponseWriter, r *http.Request) {
location = compressedLocation location = compressedLocation
} }
} }
w.Header().Add("Location", location) http.Redirect(w, r, location, http.StatusCreated)
w.WriteHeader(http.StatusCreated)
} }
func (mediaConf *configMicropubMedia) uploadToBunny(filename string, file multipart.File) (location string, err error) { func (mediaConf *configMicropubMedia) uploadToBunny(filename string, file multipart.File) (location string, err error) {

View File

@ -29,6 +29,12 @@
<input type="url" name="url" placeholder="URL"> <input type="url" name="url" placeholder="URL">
<input class="fw" type="submit" value="{{ string .Blog.Lang "delete" }}"> <input class="fw" type="submit" value="{{ string .Blog.Lang "delete" }}">
</form> </form>
<h2>{{ string .Blog.Lang "upload" }}</h2>
<form class="fw-form p" method="post" enctype="multipart/form-data">
<input type="hidden" name="editoraction" value="upload">
<input class="fw" type="file" name="file">
<input class="fw" type="submit" value="{{ string .Blog.Lang "upload" }}">
</form>
</main> </main>
{{ end }} {{ end }}

View File

@ -25,4 +25,5 @@ interactionslabel: "Have you published a response to this? Paste the URL here."
search: "Search" search: "Search"
editor: "Editor" editor: "Editor"
create: "Create" create: "Create"
update: "Update" update: "Update"
upload: "Upload"