mirror of https://github.com/jlelse/GoBlog
Add file upload to editor
This commit is contained in:
parent
54a1f699b3
commit
796470c067
22
editor.go
22
editor.go
|
@ -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)
|
||||
}
|
||||
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()
|
||||
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)
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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 }}
|
||||
|
||||
|
|
|
@ -26,3 +26,4 @@ search: "Search"
|
|||
editor: "Editor"
|
||||
create: "Create"
|
||||
update: "Update"
|
||||
upload: "Upload"
|
Loading…
Reference in New Issue