Files
Anya Tokugawa 02fb77f72b
Build and publish container images / images (push) Successful in 9m2s
Release container images / release (push) Successful in 21s
feat(slides): add sandboxed Reveal.js viewer and editor preview
2026-11-08 22:10:52 +09:00

69 lines
2.1 KiB
Go

package server
import (
"net/http"
"strings"
"unicode/utf8"
"pageswiki/internal/grammaratur"
)
const maxSlideSourceBytes = 256 << 10
func (s *Server) handleSlides(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet && r.Method != http.MethodHead {
w.Header().Set("Allow", "GET, HEAD")
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
domain := s.domainUUIDForRequest(r)
if !s.systemExtensionEnabled(domain, "viewer.slides") {
http.NotFound(w, r)
return
}
path := semanticViewPath(r.URL.Path)
if !s.requestCanReadPath(r, path) {
http.NotFound(w, r)
return
}
object, err := s.store.ResolvePathInDomain(domain, path)
if err != nil {
http.NotFound(w, r)
return
}
revision, err := s.store.GetLatestRevisionMetadataForLanguagesContext(r.Context(), object, s.revisionLanguageCandidates(r))
if err != nil || revision == nil {
http.NotFound(w, r)
return
}
media := strings.ToLower(strings.TrimSpace(strings.Split(revision.ContentType, ";")[0]))
if media != "text/html" && media != "text/markdown" && media != "text/x-markdown" {
http.Error(w, "slides require HTML or Markdown", http.StatusUnsupportedMediaType)
return
}
size, err := s.store.DocumentSizeContext(r.Context(), revision.DocumentHash)
if err != nil {
http.NotFound(w, r)
return
}
if size > maxSlideSourceBytes {
http.Error(w, "slides source exceeds 256 KiB", http.StatusRequestEntityTooLarge)
return
}
window, err := s.store.ReadDocumentWindowContext(r.Context(), revision.DocumentHash, 0, maxSlideSourceBytes)
if err != nil || window.Truncated || !utf8.Valid(window.Data) {
http.Error(w, "invalid slides source", http.StatusUnsupportedMediaType)
return
}
source := string(window.Data)
if media != "text/html" {
source = grammaratur.MarkdownAuthoringHTML(source)
}
source = s.safeHTMLProjection(r, source, path, false)
w.Header().Set("Cache-Control", "private, no-store")
if r.Method == http.MethodHead {
return
}
s.renderWithContext(w, r, "slides.gohtml", map[string]any{"Title": path, "Path": path, "SlideSource": source})
}