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.
This commit is contained in:
Anya Tokugawa committed 2026-09-27 14:44:43 +09:00
1 parent 288e6e94ac
commit c1175d389f
699 files changed
+110197 -4117

No files matched your search

+179
View File
@@ -0,0 +1,179 @@
package server
import (
"encoding/json"
"errors"
"io"
"net/http"
"strings"
"pageswiki/internal/store"
)
func (s *Server) handleAssetsAPI(w http.ResponseWriter, r *http.Request) {
domainUUID := s.domainUUIDForRequest(r)
principal := s.requestPrincipal(r)
if principal == nil {
s.authRequired(w, r)
return
}
if !s.canManageAssets(r) {
http.Error(w, "forbidden", http.StatusForbidden)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Cache-Control", "no-store")
switch r.Method {
case http.MethodGet:
address := strings.TrimSpace(r.URL.Query().Get("address"))
if address == "" {
assets, err := s.store.ListAssets(domainUUID)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
_ = json.NewEncoder(w).Encode(map[string]any{"assets": assets})
return
}
asset, err := s.store.GetAsset(domainUUID, address)
if err != nil {
http.NotFound(w, r)
return
}
history, err := s.store.ListAssetHistory(domainUUID, address)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
dependencies, err := s.store.ListAssetDependencies(domainUUID, address)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
setAssetETag(w, asset.HeadDocumentHash)
_ = json.NewEncoder(w).Encode(map[string]any{"asset": asset, "history": history, "dependencies": dependencies})
case http.MethodPost:
if strings.HasPrefix(strings.ToLower(r.Header.Get("Content-Type")), "application/json") {
var input struct {
Action string `json:"action"`
Address string `json:"address"`
HistoryID int64 `json:"history_id"`
}
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
http.Error(w, err.Error(), 400)
return
}
if input.Action != "restore" {
http.Error(w, "unsupported action", 400)
return
}
expected := parseAssetETag(r.Header.Get("If-Match"))
if expected == "" {
http.Error(w, "If-Match is required", http.StatusPreconditionRequired)
return
}
asset, err := s.store.RestoreAssetHead(domainUUID, input.Address, input.HistoryID, expected, principal.UUID)
if errors.Is(err, store.ErrAssetConflict) {
http.Error(w, "asset HEAD conflict", http.StatusPreconditionFailed)
return
} else if errors.Is(err, store.ErrAssetNotFound) {
http.NotFound(w, r)
return
} else if err != nil {
http.Error(w, err.Error(), 500)
return
}
setAssetETag(w, asset.HeadDocumentHash)
_ = json.NewEncoder(w).Encode(map[string]any{"asset": asset, "restored": true})
return
}
r.Body = http.MaxBytesReader(w, r.Body, 256<<20)
if err := r.ParseMultipartForm(8 << 20); err != nil {
http.Error(w, err.Error(), 400)
return
}
defer r.MultipartForm.RemoveAll()
address := r.FormValue("address")
file, header, err := r.FormFile("file")
if err != nil {
http.Error(w, "file is required", 400)
return
}
defer file.Close()
payload, err := io.ReadAll(file)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
current, currentErr := s.store.GetAsset(domainUUID, address)
expected := parseAssetETag(r.Header.Get("If-Match"))
if currentErr == nil && expected == "" {
http.Error(w, "If-Match is required", http.StatusPreconditionRequired)
return
}
if errors.Is(currentErr, store.ErrAssetNotFound) && r.Header.Get("If-None-Match") != "*" {
http.Error(w, "If-None-Match: * is required", http.StatusPreconditionRequired)
return
}
contentType := strings.TrimSpace(r.FormValue("content_type"))
if contentType == "" {
contentType = header.Header.Get("Content-Type")
}
filename := strings.TrimSpace(r.FormValue("filename"))
if filename == "" {
filename = header.Filename
}
asset, changed, err := s.store.PutAsset(domainUUID, address, payload, contentType, filename, expected, principal.UUID)
if errors.Is(err, store.ErrAssetConflict) {
http.Error(w, "asset HEAD conflict", http.StatusPreconditionFailed)
return
}
if err != nil {
http.Error(w, err.Error(), 400)
return
}
setAssetETag(w, asset.HeadDocumentHash)
_ = json.NewEncoder(w).Encode(map[string]any{"asset": asset, "changed": changed, "previous": current})
case http.MethodDelete:
address := strings.TrimSpace(r.URL.Query().Get("address"))
expected := parseAssetETag(r.Header.Get("If-Match"))
if expected == "" {
http.Error(w, "If-Match is required", http.StatusPreconditionRequired)
return
}
if err := s.store.DeleteAsset(domainUUID, address, expected, principal.UUID); errors.Is(err, store.ErrAssetConflict) {
http.Error(w, "asset HEAD conflict", http.StatusPreconditionFailed)
return
} else if errors.Is(err, store.ErrAssetNotFound) {
http.NotFound(w, r)
return
} else if err != nil {
http.Error(w, err.Error(), 500)
return
}
w.WriteHeader(http.StatusNoContent)
default:
w.Header().Set("Allow", "GET, POST, DELETE")
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
}
}
func (s *Server) canManageAssets(r *http.Request) bool {
principal := s.requestPrincipal(r)
if principal == nil {
return false
}
domainUUID := s.domainUUIDForRequest(r)
domain := s.domainForRequest(r)
return s.store.IsBuiltinGroupAdmin(domainUUID, principal.UUID, "domain_admin") ||
(domain != nil && s.store.IsCampusAdmin(domain.CampusUUID, principal.UUID))
}
func parseAssetETag(value string) string {
return strings.TrimPrefix(strings.Trim(strings.TrimSpace(value), `"`), "sha256:")
}
func setAssetETag(w http.ResponseWriter, hash string) {
if hash != "" {
w.Header().Set("ETag", `"sha256:`+hash+`"`)
}
}