Files
pages/internal/server/object_listing.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

98 lines
3.0 KiB
Go

package server
import (
"context"
"encoding/json"
"errors"
"net/http"
"strconv"
"time"
"pageswiki/internal/api"
"pageswiki/internal/store"
)
func (s *Server) serveObjectMetadataList(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "private, no-store")
if r.Method != http.MethodGet && r.Method != http.MethodHead {
w.Header().Set("Allow", "GET, HEAD")
api.WriteError(w, 405, "method_not_allowed", "method not allowed")
return
}
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second)
defer cancel()
r = r.WithContext(ctx)
limit := 100
if raw := r.URL.Query().Get("limit"); raw != "" {
value, err := strconv.Atoi(raw)
if err != nil || value < 1 {
api.WriteError(w, 400, "invalid_limit", "limit must be a positive integer")
return
}
limit = min(value, 200)
}
domain := s.domainUUIDForRequest(r)
if domain == "" {
api.WriteError(w, 404, "domain_not_found", "Domain not found")
return
}
mode, err := s.store.GetConfigInDomain(domain, "authorization_mode")
if err != nil {
api.WriteError(w, 500, "list_failed", "object listing unavailable")
return
}
principalID := ""
broad := mode != "enforcing"
var exact map[string]bool
if !broad {
principal, err := s.store.PrincipalForSession(domain, s.authTokenFromRequest(r))
if err != nil {
api.WriteError(w, 401, "authentication_required", "authentication required")
return
}
principalID = principal.UUID
exact, broad, err = s.store.LoadObjectListReadGrants(ctx, domain, principalID)
if err != nil {
api.WriteError(w, 503, "list_unavailable", "object listing is temporarily unavailable")
return
}
}
after, err := s.store.DecodeObjectListCursor(r.URL.Query().Get("cursor"), domain, principalID)
if err != nil {
api.WriteError(w, 400, "invalid_cursor", "invalid or expired pagination cursor")
return
}
var allowed func(string) bool
if !broad {
allowed = s.pathReadFilter(r)
}
objects, next, err := s.store.ListObjectMetadataPage(ctx, domain, after, limit, allowed, exact, broad)
if err != nil {
status := http.StatusInternalServerError
if ctx.Err() != nil || errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) || errors.Is(err, store.ErrObjectListBudget) {
status = http.StatusServiceUnavailable
}
api.WriteError(w, status, "list_unavailable", "object listing is temporarily unavailable")
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Cache-Control", "private, no-store")
if next != nil {
cursor, err := s.store.EncodeObjectListCursor(domain, principalID, *next)
if err != nil {
api.WriteError(w, 500, "list_failed", "pagination unavailable")
return
}
link := *r.URL
query := link.Query()
query.Set("cursor", cursor)
query.Set("limit", strconv.Itoa(limit))
link.RawQuery = query.Encode()
w.Header().Set("X-Next-Cursor", cursor)
w.Header().Set("Link", "<"+link.RequestURI()+">; rel=\"next\"")
}
if r.Method != http.MethodHead {
_ = json.NewEncoder(w).Encode(objects)
}
}