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.
53 lines
1.9 KiB
Go
53 lines
1.9 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
func (s *Server) handleTextWindow(w http.ResponseWriter, r *http.Request) {
|
|
address := strings.TrimPrefix(r.URL.Path, "/v/text/")
|
|
if !strings.HasPrefix(address, "rev:") {
|
|
http.Error(w, "bounded text paging requires an immutable rev: address", http.StatusBadRequest)
|
|
return
|
|
}
|
|
revision, err := s.store.GetRevisionMetadata(strings.TrimPrefix(address, "rev:"))
|
|
if err != nil {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
if !s.store.ObjectBoundInDomain(s.domainUUIDForRequest(r), revision.ObjectUUID) {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
offset, _ := strconv.ParseInt(r.URL.Query().Get("offset"), 10, 64)
|
|
limit, _ := strconv.ParseInt(r.URL.Query().Get("limit"), 10, 64)
|
|
window, err := s.store.ReadDocumentWindowContext(r.Context(), revision.DocumentHash, offset, limit)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
data := window.Data
|
|
actualOffset := window.Offset
|
|
for len(data) > 0 && !utf8.RuneStart(data[0]) {
|
|
data, actualOffset = data[1:], actualOffset+1
|
|
}
|
|
rawLength := len(data)
|
|
data = []byte(strings.ToValidUTF8(string(data), "\uFFFD"))
|
|
nextOffset := actualOffset + int64(rawLength)
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
w.Header().Set("Cache-Control", "private, max-age=31536000, immutable")
|
|
w.Header().Set("Vary", "Authorization, Cookie")
|
|
w.Header().Set("ETag", `"`+revision.DocumentHash+`-`+strconv.FormatInt(actualOffset, 10)+`-`+strconv.FormatInt(int64(len(data)), 10)+`"`)
|
|
_ = json.NewEncoder(w).Encode(map[string]any{
|
|
"revision_uuid": revision.UUID, "document_hash": revision.DocumentHash,
|
|
"content_type": revision.ContentType, "total_bytes": window.TotalBytes,
|
|
"offset": actualOffset, "next_offset": nextOffset,
|
|
"line_start": nil, "line_end": nil,
|
|
"truncated": nextOffset < window.TotalBytes, "text": string(data),
|
|
})
|
|
}
|