111 lines
3.9 KiB
Go
111 lines
3.9 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strings"
|
|
"unicode/utf8"
|
|
|
|
"pageswiki/internal/grammaratur"
|
|
)
|
|
|
|
// Converts authoring input only. It never expands macros or writes a Revision.
|
|
func (s *Server) handleEditorConvert(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
w.Header().Set("Cache-Control", "private, no-store")
|
|
if r.Method != http.MethodPost {
|
|
w.Header().Set("Allow", "POST")
|
|
http.Error(w, "method not allowed", 405)
|
|
return
|
|
}
|
|
var input struct {
|
|
HTML string `json:"html"`
|
|
}
|
|
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 2<<20)).Decode(&input); err != nil {
|
|
http.Error(w, "invalid input", 400)
|
|
return
|
|
}
|
|
markdown, err := grammaratur.HTMLToMarkdownAuthoring(input.HTML)
|
|
if err != nil {
|
|
http.Error(w, "conversion failed", 422)
|
|
return
|
|
}
|
|
_ = json.NewEncoder(w).Encode(map[string]string{"markdown": markdown})
|
|
}
|
|
|
|
// Restore loads a working buffer only; it never mutates an old Revision.
|
|
func (s *Server) handleEditorState(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
w.Header().Set("Cache-Control", "private, no-store")
|
|
w.Header().Set("Vary", "Authorization, Cookie")
|
|
if r.Method != http.MethodGet {
|
|
w.Header().Set("Allow", "GET")
|
|
http.Error(w, "method not allowed", 405)
|
|
return
|
|
}
|
|
domain := s.domainUUIDForRequest(r)
|
|
principal, authErr := s.store.PrincipalForSession(domain, s.authTokenFromRequest(r))
|
|
if mode, _ := s.store.GetConfigInDomain(domain, "authorization_mode"); mode == "enforcing" && authErr != nil {
|
|
http.Error(w, "authentication required", 401)
|
|
return
|
|
}
|
|
object := r.URL.Query().Get("uuid")
|
|
if object == "" || !s.store.ObjectBoundInDomain(domain, object) || s.authorizeObjectForRequest(r, "read", object) != nil {
|
|
http.Error(w, "forbidden", 403)
|
|
return
|
|
}
|
|
rev, err := s.store.GetRevisionMetadataForExactLanguage(object, r.URL.Query().Get("lang"))
|
|
if err != nil {
|
|
http.Error(w, "invalid language or unavailable revision", 400)
|
|
return
|
|
}
|
|
head := rev.UUID
|
|
if id := r.URL.Query().Get("revision"); id != "" {
|
|
metadata, e := s.store.GetRevisionMetadata(id)
|
|
if e != nil || metadata.ObjectUUID != object {
|
|
http.Error(w, "revision not found", 404)
|
|
return
|
|
}
|
|
rev = metadata
|
|
}
|
|
owner := ""
|
|
if principal != nil {
|
|
owner = principal.UUID
|
|
}
|
|
out := map[string]any{"revision_uuid": rev.UUID, "head_revision_uuid": head, "lang": rev.Language, "content_type": rev.ContentType, "filename": rev.Filename, "principal_uuid": owner}
|
|
if r.URL.Query().Get("metadata") != "1" {
|
|
csv := selectProjectionAdapter(contentDescriptor{MediaType: strings.ToLower(strings.TrimSpace(strings.Split(rev.ContentType, ";")[0])), Filename: rev.Filename}).Kind == "csv"
|
|
if csv {
|
|
size, e := s.store.DocumentSizeContext(r.Context(), rev.DocumentHash)
|
|
if e != nil || size > maxCSVMacroBytes {
|
|
http.Error(w, "CSV editor size limit exceeded", 413)
|
|
return
|
|
}
|
|
window, e := s.store.ReadDocumentWindowContext(r.Context(), rev.DocumentHash, 0, maxCSVMacroBytes)
|
|
if e != nil || window.Truncated || !utf8.Valid(window.Data) || strings.ContainsRune(string(window.Data), 0) {
|
|
http.Error(w, "CSV source unavailable or not UTF-8", 415)
|
|
return
|
|
}
|
|
rev.Body = string(window.Data)
|
|
} else if rev.UUID != "" {
|
|
rev, err = s.store.GetRevision(rev.UUID)
|
|
}
|
|
if err != nil {
|
|
http.Error(w, "revision unavailable", 503)
|
|
return
|
|
}
|
|
markdown := rev.Body
|
|
if strings.HasPrefix(rev.ContentType, "text/html") {
|
|
markdown, err = grammaratur.HTMLToMarkdownAuthoring(rev.Body)
|
|
}
|
|
if err != nil {
|
|
http.Error(w, "conversion unavailable", 503)
|
|
return
|
|
}
|
|
out["body"], out["markdown"] = rev.Body, markdown
|
|
kind := revisionContentView(rev, "").Kind
|
|
out["editable"] = rev.UUID == "" || kind == "html" || kind == "code" || (csv && utf8.ValidString(rev.Body) && !strings.ContainsRune(rev.Body, 0))
|
|
}
|
|
_ = json.NewEncoder(w).Encode(out)
|
|
}
|