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