Files
pages/internal/server/raw_view.go
T
Anya Tokugawa d5d21b7841
Build and publish container images / images (push) Successful in 9m34s
Release container images / release (push) Successful in 36s
Refactor architecture into eight components and consolidate reliability work
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.
2026-11-01 15:23:31 +09:00

212 lines
5.9 KiB
Go

package server
import (
"bytes"
"mime"
"net/http"
"strings"
"time"
"pageswiki/internal/store"
)
func (s *Server) handleRawView(
w http.ResponseWriter,
r *http.Request,
) {
if r.URL.Query().Get("preview") == "1" && s.previewSlots != nil {
select {
case s.previewSlots <- struct{}{}:
defer func() { <-s.previewSlots }()
case <-r.Context().Done():
return
}
}
address := strings.TrimPrefix(
r.URL.Path,
"/v/raw/",
)
if strings.HasPrefix(address, "asset:") {
if !s.canManageAssets(r) {
http.NotFound(w, r)
return
}
asset, err := s.store.GetAsset(s.domainUUIDForRequest(r), address)
if err != nil {
http.NotFound(w, r)
return
}
s.serveAssetHead(w, r, asset)
return
}
var (
rev *store.Revision
err error
viewPath string
)
switch {
case strings.HasPrefix(address, "path:"):
path := strings.TrimPrefix(address, "path:")
viewPath = path
var objectUUID string
objectUUID, err = s.store.ResolvePathInDomain(
s.domainUUIDForRequest(r),
path,
)
if err == nil {
rev, err = s.latestRevisionForRequest(r, objectUUID)
}
case strings.HasPrefix(address, "uuid:"):
objectUUID := strings.TrimPrefix(address, "uuid:")
if !s.store.ObjectBoundInDomain(s.domainUUIDForRequest(r), objectUUID) {
http.NotFound(w, r)
return
}
rev, err = s.latestRevisionForRequest(r, objectUUID)
case strings.HasPrefix(address, "rev:"):
revisionUUID := strings.TrimPrefix(address, "rev:")
rev, err = s.store.GetRevision(revisionUUID)
if err == nil && !s.store.ObjectBoundInDomain(s.domainUUIDForRequest(r), rev.ObjectUUID) {
http.NotFound(w, r)
return
}
default:
http.NotFound(w, r)
return
}
if err != nil {
http.NotFound(w, r)
return
}
etag := `"` + rev.DocumentHash + `"`
w.Header().Set("ETag", etag)
setDocumentIdentityHeaders(w.Header(), rev.ObjectUUID, rev.UUID, rev.DocumentHash)
if strings.HasPrefix(address, "rev:") {
w.Header().Set("Cache-Control", "private, max-age=31536000, immutable")
} else {
w.Header().Set("Cache-Control", "private, max-age=30")
}
w.Header().Set("Vary", "Authorization, Cookie")
if r.Header.Get("If-None-Match") == etag {
w.WriteHeader(http.StatusNotModified)
return
}
view := revisionContentView(rev, viewPath)
contentType := view.ContentType
mediaType, _, parseErr := mime.ParseMediaType(contentType)
if parseErr != nil {
contentType = "application/octet-stream"
mediaType = contentType
}
filename := strings.TrimSpace(view.Filename)
if filename == "" {
filename = rev.UUID
}
disposition := "inline"
if mediaType == "text/html" ||
(!strings.HasPrefix(mediaType, "image/") &&
!strings.HasPrefix(mediaType, "audio/") &&
!strings.HasPrefix(mediaType, "video/") &&
mediaType != "application/pdf" &&
!strings.HasPrefix(mediaType, "text/")) {
disposition = "attachment"
}
w.Header().Set("Content-Type", contentType)
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set(
"Content-Disposition",
mime.FormatMediaType(disposition, map[string]string{
"filename": filename,
}),
)
if mediaType == "image/svg+xml" {
w.Header().Set("Content-Security-Policy", "sandbox")
}
http.ServeContent(
w,
r,
filename,
time.UnixMilli(rev.CreatedAtMS),
bytes.NewReader([]byte(rev.Body)),
)
}
func (s *Server) serveAssetHead(w http.ResponseWriter, r *http.Request, asset *store.Asset) {
if r.Method != http.MethodGet && r.Method != http.MethodHead {
w.Header().Set("Allow", "GET, HEAD")
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
reader, err := s.store.OpenDocumentContext(r.Context(), asset.HeadDocumentHash)
if err != nil {
http.NotFound(w, r)
return
}
defer reader.Close()
contentType := strings.TrimSpace(asset.ContentType)
if contentType == "" || contentType == "application/octet-stream" {
probe := make([]byte, 512)
count, _ := reader.Read(probe)
_, _ = reader.Seek(0, 0)
contentType = http.DetectContentType(probe[:count])
}
mediaType, _, err := mime.ParseMediaType(contentType)
if err != nil {
mediaType, contentType = "application/octet-stream", "application/octet-stream"
}
filename := strings.TrimSpace(asset.Filename)
if filename == "" {
filename = strings.TrimPrefix(asset.Address, "asset:")
}
disposition := "inline"
if mediaType == "text/html" || mediaType == "image/svg+xml" ||
(!strings.HasPrefix(mediaType, "image/") && !strings.HasPrefix(mediaType, "audio/") &&
!strings.HasPrefix(mediaType, "video/") && mediaType != "application/pdf" &&
!strings.HasPrefix(mediaType, "text/")) {
disposition = "attachment"
}
etag := `"sha256:` + asset.HeadDocumentHash + `"`
w.Header().Set("ETag", etag)
w.Header().Set("Cache-Control", "private, no-cache")
w.Header().Set("Vary", "Authorization, Cookie")
w.Header().Set("Content-Type", contentType)
w.Header().Set("Content-Disposition", mime.FormatMediaType(disposition, map[string]string{"filename": filename}))
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set("X-PagesWiki-Asset-Address", asset.Address)
w.Header().Set("X-PagesWiki-Asset-Head", asset.HeadDocumentHash)
setDocumentIdentityHeaders(w.Header(), asset.ObjectUUID, "", asset.HeadDocumentHash)
if mediaType == "image/svg+xml" {
w.Header().Set("Content-Security-Policy", "sandbox")
}
if r.Header.Get("If-None-Match") == etag {
w.WriteHeader(http.StatusNotModified)
return
}
http.ServeContent(w, r, filename, time.UnixMilli(asset.UpdatedAtMS), reader)
}
func setDocumentIdentityHeaders(header http.Header, objectUUID, revisionUUID, documentHash string) {
if objectUUID != "" {
header.Set("X-PagesWiki-Object-UUID", objectUUID)
}
if revisionUUID != "" {
header.Set("X-PagesWiki-Revision-UUID", revisionUUID)
}
if documentHash != "" {
header.Set("X-PagesWiki-Document-SHA256", documentHash)
// Compatibility with the first immutable-object implementation.
header.Set("X-Document-SHA256", documentHash)
}
}
func rawProjectionURL(address string) string {
return "/v/raw/" + address
}