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) {
if action := r.FormValue("editoraction"); action != "" {
if action == "loadupdate" {
switch action {
case "loadupdate":
parsedURL, err := url.Parse(r.FormValue("url"))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
@ -34,8 +35,7 @@ func serveEditorPost(w http.ResponseWriter, r *http.Request) {
"UpdatePostContent": mf.Properties.Content[0],
},
})
return
} else if action == "updatepost" {
case "updatepost":
urlValue := r.FormValue("url")
content := r.FormValue("content")
mf := map[string]interface{}{
@ -58,18 +58,24 @@ func serveEditorPost(w http.ResponseWriter, r *http.Request) {
return
}
req.Header.Set(contentType, contentTypeJSON)
editorMicropubPost(w, req)
return
editorMicropubPost(w, req, false)
case "upload":
editorMicropubPost(w, r, true)
default:
http.Error(w, "unknown editoraction", http.StatusBadRequest)
}
http.Error(w, "unknown editoraction", http.StatusBadRequest)
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()
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()
if location := result.Header.Get("Location"); location != "" {
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
}
}
w.Header().Add("Location", location)
w.WriteHeader(http.StatusCreated)
http.Redirect(w, r, location, http.StatusCreated)
}
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 class="fw" type="submit" value="{{ string .Blog.Lang "delete" }}">
</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>
{{ end }}

View File

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