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