GoBlog/editor.go

116 lines
3.1 KiB
Go
Raw Normal View History

package main
import (
"bytes"
2021-01-21 16:59:47 +00:00
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"net/url"
"git.jlel.se/jlelse/GoBlog/pkgs/contenttype"
)
const editorPath = "/editor"
func (a *goBlog) serveEditor(w http.ResponseWriter, r *http.Request) {
blog := r.Context().Value(blogContextKey).(string)
a.render(w, r, templateEditor, &renderData{
BlogString: blog,
Data: map[string]interface{}{
"Drafts": a.db.getDrafts(blog),
},
})
}
func (a *goBlog) serveEditorPost(w http.ResponseWriter, r *http.Request) {
blog := r.Context().Value(blogContextKey).(string)
if action := r.FormValue("editoraction"); action != "" {
switch action {
case "loaddelete":
a.render(w, r, templateEditor, &renderData{
BlogString: blog,
Data: map[string]interface{}{
"DeleteURL": r.FormValue("url"),
"Drafts": a.db.getDrafts(blog),
},
})
case "loadupdate":
parsedURL, err := url.Parse(r.FormValue("url"))
if err != nil {
a.serveError(w, r, err.Error(), http.StatusBadRequest)
return
}
post, err := a.db.getPost(parsedURL.Path)
if err != nil {
a.serveError(w, r, err.Error(), http.StatusBadRequest)
return
}
a.render(w, r, templateEditor, &renderData{
BlogString: blog,
Data: map[string]interface{}{
"UpdatePostURL": parsedURL.String(),
2021-06-23 17:20:50 +00:00
"UpdatePostContent": a.postToMfItem(post).Properties.Content[0],
"Drafts": a.db.getDrafts(blog),
},
})
case "updatepost":
jsonBytes, err := json.Marshal(map[string]interface{}{
"action": actionUpdate,
"url": r.FormValue("url"),
"replace": map[string][]string{
"content": {
r.FormValue("content"),
2021-01-17 11:53:07 +00:00
},
},
})
if err != nil {
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
return
}
req, err := http.NewRequest(http.MethodPost, "", bytes.NewReader(jsonBytes))
if err != nil {
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
return
}
req.Header.Set(contentType, contenttype.JSON)
a.editorMicropubPost(w, req, false)
case "upload":
a.editorMicropubPost(w, r, true)
2021-06-23 17:20:50 +00:00
case "viewdraft":
parsedURL, err := url.Parse(r.FormValue("url"))
if err != nil {
a.serveError(w, r, err.Error(), http.StatusBadRequest)
return
}
http.Redirect(w, r, parsedURL.Path, http.StatusFound)
return
default:
a.serveError(w, r, "Unknown editoraction", http.StatusBadRequest)
}
return
}
a.editorMicropubPost(w, r, false)
}
func (a *goBlog) editorMicropubPost(w http.ResponseWriter, r *http.Request, media bool) {
recorder := httptest.NewRecorder()
2020-12-13 10:28:46 +00:00
if media {
addAllScopes(http.HandlerFunc(a.serveMicropubMedia)).ServeHTTP(recorder, r)
2020-12-13 10:28:46 +00:00
} else {
addAllScopes(http.HandlerFunc(a.serveMicropubPost)).ServeHTTP(recorder, r)
2020-12-13 10:28:46 +00:00
}
result := recorder.Result()
if location := result.Header.Get("Location"); location != "" {
2021-01-15 20:56:46 +00:00
http.Redirect(w, r, location, http.StatusFound)
return
}
if result.StatusCode >= 200 && result.StatusCode <= 400 {
http.Redirect(w, r, editorPath, http.StatusFound)
return
}
w.WriteHeader(result.StatusCode)
_, _ = io.Copy(w, result.Body)
_ = result.Body.Close()
}