Files
pages/internal/server/text_window.go
Anya Tokugawa c1175d389f Implement Knowledge City platform, organizer and DAV integration
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.
2026-09-27 14:44:43 +09:00

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.ReadDocumentWindow(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),
})
}