Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts. Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation. Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
174 lines
5.7 KiB
Go
174 lines
5.7 KiB
Go
package server
|
|
|
|
import (
|
|
"mime"
|
|
"net/http"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"pageswiki/internal/store"
|
|
)
|
|
|
|
func (s *Server) windowedContentViewForRequest(r *http.Request, objectUUID, semanticPath string) (contentView, bool, error) {
|
|
metadata, err := s.latestRevisionMetadataForRequest(r, objectUUID)
|
|
if err != nil || metadata.UUID == "" {
|
|
return contentView{}, false, err
|
|
}
|
|
size, err := s.store.DocumentSizeContext(r.Context(), metadata.DocumentHash)
|
|
if err != nil {
|
|
return contentView{}, false, nil
|
|
}
|
|
mediaType := strings.ToLower(strings.TrimSpace(strings.Split(metadata.ContentType, ";")[0]))
|
|
adapter := selectProjectionAdapter(contentDescriptor{MediaType: mediaType, Filename: metadata.Filename, SizeBytes: size})
|
|
if adapter.ID == "pdf" || (adapter.ID == "archive" && !strings.EqualFold(filepath.Ext(metadata.Filename), ".zip")) {
|
|
view := revisionContentView(metadata, semanticPath)
|
|
view.SizeBytes = size
|
|
view.RawURL = rawProjectionURL("rev:" + metadata.UUID)
|
|
s.applyMetadataProjection(&view, semanticPath)
|
|
return view, true, nil
|
|
}
|
|
if size <= inlineTextWindowThreshold || !adapter.WindowableText {
|
|
return contentView{}, false, nil
|
|
}
|
|
window, err := s.store.ReadDocumentWindowContext(r.Context(), metadata.DocumentHash, 0, 128<<10)
|
|
if err != nil {
|
|
return contentView{}, false, err
|
|
}
|
|
view := revisionWindowedContentView(metadata, semanticPath, size, window.Data, window.NextOffset)
|
|
view.RawURL = rawProjectionURL("rev:" + metadata.UUID)
|
|
return view, true, nil
|
|
}
|
|
|
|
type contentView struct {
|
|
Kind string
|
|
AdapterID string
|
|
Delivery projectionDelivery
|
|
ContentType string
|
|
Filename string
|
|
Text string
|
|
RawURL string
|
|
Language string
|
|
SizeBytes int64
|
|
Windowed bool
|
|
ArchiveEntries []archiveEntryView
|
|
ArchiveTruncated bool
|
|
MetadataStatus string
|
|
MetadataURL string
|
|
Metadata []metadataView
|
|
ParseError string
|
|
EmailHTML bool
|
|
PDFPages int
|
|
CSVRows [][]string
|
|
CSVTruncated bool
|
|
ExcalidrawValid bool
|
|
ExcalidrawElements int
|
|
ObjectUUID string
|
|
RevisionUUID string
|
|
DocumentHash string
|
|
WindowURL string
|
|
NextOffset int64
|
|
JSONURL string
|
|
}
|
|
|
|
func revisionWindowedContentView(revision *store.Revision, semanticPath string, totalBytes int64, initial []byte, nextOffset int64) contentView {
|
|
contentType := revision.ContentType
|
|
mediaType, _, err := mime.ParseMediaType(contentType)
|
|
if err != nil {
|
|
mediaType, contentType = "application/octet-stream", "application/octet-stream"
|
|
}
|
|
filename := strings.TrimSpace(revision.Filename)
|
|
if filename == "" {
|
|
filename = filepath.Base(semanticPath)
|
|
}
|
|
if mediaType == "application/octet-stream" {
|
|
detected := detectRevisionText(initial, filename)
|
|
if detected.ContentType != "" {
|
|
contentType = detected.ContentType
|
|
mediaType, _, _ = mime.ParseMediaType(contentType)
|
|
}
|
|
}
|
|
adapter := selectProjectionAdapter(contentDescriptor{MediaType: strings.ToLower(mediaType), Filename: filename, SizeBytes: totalBytes})
|
|
text, _, ok := decodeTextBytes(initial)
|
|
if !ok {
|
|
text = string(initial)
|
|
}
|
|
view := contentView{Kind: adapter.Kind, AdapterID: adapter.ID, Delivery: adapter.Delivery, ContentType: contentType, Filename: filename, Text: text, Language: codeLanguage(contentType, filename, text), SizeBytes: totalBytes, Windowed: true, ObjectUUID: revision.ObjectUUID, RevisionUUID: revision.UUID, DocumentHash: revision.DocumentHash, WindowURL: "/v/text/rev:" + revision.UUID, NextOffset: nextOffset}
|
|
if adapter.ID == "json" {
|
|
view.JSONURL = "/v/json/rev:" + revision.UUID
|
|
view.Text = ""
|
|
}
|
|
if adapter.ID == "csv" {
|
|
parseCSVView(&view, initial)
|
|
view.CSVTruncated = view.CSVTruncated || nextOffset < totalBytes
|
|
}
|
|
if adapter.ID == "excalidraw" {
|
|
// Large scenes keep bounded source delivery rather than parsing an
|
|
// incomplete or unbounded graph during navigation.
|
|
view.Kind = "code"
|
|
view.Language = "json"
|
|
}
|
|
return view
|
|
}
|
|
|
|
func revisionContentView(revision *store.Revision, semanticPath string) contentView {
|
|
contentType := revision.ContentType
|
|
mediaType, _, err := mime.ParseMediaType(contentType)
|
|
if err != nil {
|
|
mediaType = "application/octet-stream"
|
|
contentType = mediaType
|
|
}
|
|
mediaType = strings.ToLower(mediaType)
|
|
filename := strings.TrimSpace(revision.Filename)
|
|
if filename == "" {
|
|
filename = filepath.Base(semanticPath)
|
|
}
|
|
view := contentView{
|
|
ContentType: contentType,
|
|
Filename: filename,
|
|
SizeBytes: int64(len(revision.Body)),
|
|
ObjectUUID: revision.ObjectUUID,
|
|
RevisionUUID: revision.UUID,
|
|
DocumentHash: revision.DocumentHash,
|
|
}
|
|
if mediaType == "application/octet-stream" {
|
|
detected := detectRevisionText([]byte(revision.Body), filename)
|
|
if detected.ContentType != "" {
|
|
contentType = detected.ContentType
|
|
mediaType, _, _ = mime.ParseMediaType(contentType)
|
|
view.ContentType = contentType
|
|
}
|
|
if detected.IsText {
|
|
view.Text = detected.Text
|
|
}
|
|
}
|
|
adapter := selectProjectionAdapter(contentDescriptor{
|
|
MediaType: mediaType,
|
|
Filename: filename,
|
|
SizeBytes: view.SizeBytes,
|
|
})
|
|
view.Kind = adapter.Kind
|
|
view.AdapterID = adapter.ID
|
|
view.Delivery = adapter.Delivery
|
|
view.Windowed = adapter.WindowableText &&
|
|
view.SizeBytes > inlineTextWindowThreshold
|
|
if adapter.ID == "json" {
|
|
view.JSONURL = "/v/json/rev:" + revision.UUID
|
|
}
|
|
|
|
if adapter.NeedsText {
|
|
if view.Text == "" {
|
|
if decoded, _, ok := decodeTextBytes([]byte(revision.Body)); ok {
|
|
view.Text = decoded
|
|
} else {
|
|
view.Text = revision.Body
|
|
}
|
|
}
|
|
view.Language = codeLanguage(contentType, filename, view.Text)
|
|
}
|
|
structuredContentView(&view, []byte(revision.Body), semanticPath)
|
|
if adapter.ID == "eml" {
|
|
view.EmailHTML = extractEmailHTML([]byte(revision.Body)) != ""
|
|
}
|
|
return view
|
|
}
|