Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
112 lines
2.3 KiB
Go
112 lines
2.3 KiB
Go
package server
|
|
|
|
import (
|
|
"html/template"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"pageswiki/internal/store"
|
|
)
|
|
|
|
func (s *Server) handlePreview(
|
|
w http.ResponseWriter,
|
|
r *http.Request,
|
|
) {
|
|
|
|
path :=
|
|
strings.TrimPrefix(
|
|
r.URL.Path,
|
|
"/v/preview",
|
|
)
|
|
|
|
if path == "" {
|
|
path = "/"
|
|
}
|
|
|
|
if !strings.HasPrefix(path, "/") {
|
|
path = "/" + path
|
|
}
|
|
|
|
objectUUID, err :=
|
|
s.store.ResolvePathInDomain(
|
|
s.domainUUIDForRequest(r),
|
|
path,
|
|
)
|
|
|
|
if err == nil {
|
|
windowedView, selected, windowErr := s.windowedContentViewForRequest(r, objectUUID, path)
|
|
if windowErr != nil {
|
|
http.Error(w, windowErr.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
if selected {
|
|
s.renderPreviewDocument(w, r, path, windowedView, "")
|
|
return
|
|
}
|
|
|
|
latest, err :=
|
|
s.latestRevisionForRequest(
|
|
r,
|
|
objectUUID,
|
|
)
|
|
|
|
if err == nil {
|
|
view := previewContentView(latest, path)
|
|
view.RawURL = rawProjectionURL("rev:" + latest.UUID)
|
|
view.RevisionUUID = latest.UUID
|
|
if view.Kind == "pdf" {
|
|
view.PDFPages = s.pdfPageCount(latest.DocumentHash)
|
|
}
|
|
rendered := ""
|
|
if view.Kind == "html" && strings.TrimSpace(latest.Body) != "" {
|
|
rendered = s.safeHTMLProjection(r, latest.Body, path, true)
|
|
}
|
|
s.renderPreviewDocument(w, r, path, view, rendered)
|
|
return
|
|
}
|
|
}
|
|
|
|
entries, err :=
|
|
s.store.ListBrowserEntriesInDomain(
|
|
s.domainUUIDForRequest(r),
|
|
path,
|
|
)
|
|
|
|
if err != nil {
|
|
http.Error(w, err.Error(), 500)
|
|
return
|
|
}
|
|
visibleEntries := entries[:0]
|
|
for _, entry := range entries {
|
|
if s.requestCanReadPath(r, entry.Path) {
|
|
visibleEntries = append(visibleEntries, entry)
|
|
}
|
|
}
|
|
entries = visibleEntries
|
|
|
|
s.renderWithContext(
|
|
w,
|
|
r,
|
|
"preview.gohtml",
|
|
map[string]any{
|
|
"Path": path,
|
|
"IsListing": true,
|
|
"Entries": entries,
|
|
},
|
|
)
|
|
}
|
|
|
|
// previewContentView deliberately shares the Viewer adapter registry. Preview
|
|
// must never infer that an arbitrary Revision body is trusted HTML.
|
|
func previewContentView(revision *store.Revision, semanticPath string) contentView {
|
|
return revisionContentView(revision, semanticPath)
|
|
}
|
|
|
|
func (s *Server) renderPreviewDocument(w http.ResponseWriter, r *http.Request, path string, view contentView, rendered string) {
|
|
s.renderWithContext(w, r, "preview.gohtml", map[string]any{
|
|
"Path": path,
|
|
"ContentView": view,
|
|
"RenderedHTML": template.HTML(rendered),
|
|
})
|
|
}
|