GoBlog/editor.go

272 lines
7.5 KiB
Go
Raw Normal View History

package main
import (
"context"
2021-08-01 17:27:21 +00:00
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"time"
2023-04-15 16:46:12 +00:00
"github.com/carlmjohnson/requests"
"go.goblog.app/app/pkgs/bufferpool"
"go.goblog.app/app/pkgs/contenttype"
"go.goblog.app/app/pkgs/htmlbuilder"
2021-07-31 12:19:37 +00:00
"gopkg.in/yaml.v3"
ws "nhooyr.io/websocket"
)
const editorPath = "/editor"
func (a *goBlog) serveEditor(w http.ResponseWriter, r *http.Request) {
a.render(w, r, a.renderEditor, &renderData{
Data: &editorRenderData{
presetParams: parsePresetPostParamsFromQuery(r),
},
})
}
2021-11-01 17:39:08 +00:00
func (a *goBlog) serveEditorPreview(w http.ResponseWriter, r *http.Request) {
blog, _ := a.getBlog(r)
2022-02-18 15:35:53 +00:00
c, err := ws.Accept(w, r, &ws.AcceptOptions{CompressionMode: ws.CompressionContextTakeover})
2021-11-01 17:39:08 +00:00
if err != nil {
return
}
c.SetReadLimit(1 << 20) // 1MB
defer c.Close(ws.StatusNormalClosure, "")
ctx, cancel := context.WithTimeout(r.Context(), time.Hour*6)
defer cancel()
2021-11-01 17:39:08 +00:00
for {
// Retrieve content
2022-02-11 23:48:59 +00:00
mt, message, err := c.Reader(ctx)
2021-11-01 17:39:08 +00:00
if err != nil {
break
}
if mt != ws.MessageText {
2021-11-10 17:17:25 +00:00
continue
}
2021-11-01 17:39:08 +00:00
// Create preview
2022-02-11 18:39:07 +00:00
w, err := c.Writer(ctx, ws.MessageText)
2021-11-01 17:39:08 +00:00
if err != nil {
2022-02-11 18:39:07 +00:00
break
2021-11-01 17:39:08 +00:00
}
2022-02-11 23:48:59 +00:00
a.createMarkdownPreview(w, blog, message)
if err = w.Close(); err != nil {
2021-11-01 17:39:08 +00:00
break
}
}
}
2022-02-11 23:48:59 +00:00
func (a *goBlog) createMarkdownPreview(w io.Writer, blog string, markdown io.Reader) {
md, err := io.ReadAll(markdown)
if err != nil {
_, _ = io.WriteString(w, err.Error())
return
}
p := &post{
Blog: blog,
2022-02-11 23:48:59 +00:00
Content: string(md),
}
if err = a.extractParamsFromContent(p); err != nil {
_, _ = io.WriteString(w, err.Error())
return
}
if err := a.checkPost(p, true); err != nil {
2022-02-11 18:39:07 +00:00
_, _ = io.WriteString(w, err.Error())
return
}
if t := p.Title(); t != "" {
p.RenderedTitle = a.renderMdTitle(t)
}
// Render post (using post's blog config)
hb := htmlbuilder.NewHtmlBuilder(w)
2022-12-14 15:03:54 +00:00
a.renderEditorPreview(hb, a.getBlogFromPost(p), p)
2021-11-01 17:39:08 +00:00
}
func (a *goBlog) serveEditorPost(w http.ResponseWriter, r *http.Request) {
2023-04-15 16:46:12 +00:00
switch action := r.FormValue("editoraction"); action {
case "loadupdate":
post, err := a.getPost(r.FormValue("path"))
if err != nil {
a.serveError(w, r, err.Error(), http.StatusBadRequest)
return
}
2023-04-15 16:46:12 +00:00
a.render(w, r, a.renderEditor, &renderData{
Data: &editorRenderData{
presetParams: parsePresetPostParamsFromQuery(r),
updatePostUrl: a.fullPostURL(post),
updatePostContent: a.postToMfItem(post).Properties.Content[0],
},
})
case "createpost", "updatepost":
reqBody := map[string]any{}
if action == "updatepost" {
reqBody["action"] = actionUpdate
reqBody["url"] = r.FormValue("url")
reqBody["replace"] = map[string][]string{"content": {r.FormValue("content")}}
} else {
reqBody["type"] = []string{"h-entry"}
reqBody["properties"] = map[string][]string{"content": {r.FormValue("content")}}
}
req, _ := requests.URL("").BodyJSON(reqBody).Request(r.Context())
a.editorMicropubPost(w, req, false)
case "upload":
a.editorMicropubPost(w, r, true)
case "delete", "undelete":
req, _ := requests.URL("").Method(http.MethodPost).ContentType(contenttype.WWWForm).Param("action", action).Param("url", r.FormValue("url")).Request(r.Context())
a.editorMicropubPost(w, req, false)
case "tts":
parsedURL, err := url.Parse(r.FormValue("url"))
if err != nil {
a.serveError(w, r, err.Error(), http.StatusBadRequest)
return
}
post, err := a.getPost(parsedURL.Path)
if err != nil {
a.serveError(w, r, err.Error(), http.StatusBadRequest)
return
}
if err = a.createPostTTSAudio(post); err != nil {
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, post.Path, http.StatusFound)
case "helpgpx":
file, _, err := r.FormFile("file")
if err != nil {
a.serveError(w, r, err.Error(), http.StatusBadRequest)
return
}
gpx, err := io.ReadAll(a.min.Get().Reader(contenttype.XML, file))
if err != nil {
a.serveError(w, r, err.Error(), http.StatusBadRequest)
return
}
w.Header().Set(contentType, contenttype.TextUTF8)
_ = yaml.NewEncoder(w).Encode(map[string]string{
"gpx": string(gpx),
})
default:
a.serveError(w, r, "Unknown or missing editoraction", http.StatusBadRequest)
}
}
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()
}
2021-07-31 12:19:37 +00:00
func (*goBlog) editorPostTemplate(blog string, bc *configBlog, presetParams map[string][]string) string {
builder := bufferpool.Get()
defer bufferpool.Put(builder)
marsh := func(param string, preset bool, i any) {
if _, presetPresent := presetParams[param]; !preset && presetPresent {
return
}
2022-03-16 07:28:03 +00:00
_ = yaml.NewEncoder(builder).Encode(map[string]any{
2021-07-31 12:19:37 +00:00
param: i,
})
}
builder.WriteString("---\n")
marsh("blog", false, blog)
marsh("section", false, bc.DefaultSection)
marsh("status", false, statusDraft)
marsh("visibility", false, visibilityPublic)
marsh("priority", false, 0)
marsh("slug", false, "")
marsh("title", false, "")
2021-07-31 12:19:37 +00:00
for _, t := range bc.Taxonomies {
marsh(t.Name, false, []string{""})
}
for key, param := range presetParams {
marsh(key, true, param)
2021-07-31 12:19:37 +00:00
}
builder.WriteString("---\n")
return builder.String()
}
func (a *goBlog) editorPostDesc(bc *configBlog) string {
2021-08-01 17:27:21 +00:00
t := a.ts.GetTemplateStringVariant(bc.Lang, "editorpostdesc")
paramBuilder, statusBuilder, visibilityBuilder := bufferpool.Get(), bufferpool.Get(), bufferpool.Get()
defer bufferpool.Put(paramBuilder, statusBuilder, visibilityBuilder)
2021-07-31 12:19:37 +00:00
for i, param := range []string{
2021-12-11 18:43:40 +00:00
"published",
"updated",
2021-07-31 12:19:37 +00:00
"summary",
"translationkey",
"original",
a.cfg.Micropub.AudioParam,
a.cfg.Micropub.BookmarkParam,
a.cfg.Micropub.LikeParam,
a.cfg.Micropub.LikeTitleParam,
a.cfg.Micropub.LikeContextParam,
2021-07-31 12:19:37 +00:00
a.cfg.Micropub.LocationParam,
a.cfg.Micropub.PhotoParam,
a.cfg.Micropub.PhotoDescriptionParam,
a.cfg.Micropub.ReplyParam,
a.cfg.Micropub.ReplyTitleParam,
a.cfg.Micropub.ReplyContextParam,
gpxParameter,
2021-07-31 12:19:37 +00:00
} {
if param == "" {
continue
}
if i > 0 {
2021-08-01 17:27:21 +00:00
paramBuilder.WriteString(", ")
2021-07-31 12:19:37 +00:00
}
2021-08-01 17:27:21 +00:00
paramBuilder.WriteByte('`')
paramBuilder.WriteString(param)
paramBuilder.WriteByte('`')
2021-07-31 12:19:37 +00:00
}
2021-08-01 17:27:21 +00:00
for i, status := range []postStatus{
statusPublished, statusDraft, statusScheduled,
2021-08-01 17:27:21 +00:00
} {
if i > 0 {
statusBuilder.WriteString(", ")
}
statusBuilder.WriteByte('`')
statusBuilder.WriteString(string(status))
statusBuilder.WriteByte('`')
}
for i, visibility := range []postVisibility{
visibilityPublic, visibilityUnlisted, visibilityPrivate,
} {
if i > 0 {
visibilityBuilder.WriteString(", ")
}
visibilityBuilder.WriteByte('`')
visibilityBuilder.WriteString(string(visibility))
visibilityBuilder.WriteByte('`')
}
return fmt.Sprintf(t, paramBuilder.String(), "status", "visibility", statusBuilder.String(), visibilityBuilder.String())
2021-07-31 12:19:37 +00:00
}
func parsePresetPostParamsFromQuery(r *http.Request) map[string][]string {
m := map[string][]string{}
for key, param := range r.URL.Query() {
if strings.HasPrefix(key, "p:") {
paramKey := strings.TrimPrefix(key, "p:")
m[paramKey] = append(m[paramKey], param...)
}
}
return m
}