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:
1 parent
288e6e94ac
commit
c1175d389f
699 files changed
+110197
-4117
No files matched your search
@@ -4,21 +4,47 @@ import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var diagramSourceCache = map[string]diagramCacheEntry{}
|
||||
var diagramSourceMu sync.Mutex
|
||||
|
||||
type diagramCacheEntry struct {
|
||||
Type string
|
||||
Source string
|
||||
At time.Time
|
||||
Domain string
|
||||
Path string
|
||||
}
|
||||
|
||||
func (s *Server) diagramCachePath(kind, hash string) string {
|
||||
root := s.diagramRoot
|
||||
if root == "" {
|
||||
root = "/data/cache/diagram"
|
||||
}
|
||||
return filepath.Join(root, kind, hash+".svg")
|
||||
}
|
||||
|
||||
func readDiagramCache(path string) ([]byte, error) {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
b, err := io.ReadAll(io.LimitReader(f, (4<<20)+1))
|
||||
if len(b) > 4<<20 {
|
||||
return nil, errors.New("diagram cache exceeds limit")
|
||||
}
|
||||
return b, err
|
||||
}
|
||||
|
||||
func diagramHash(
|
||||
@@ -81,6 +107,8 @@ func krokiURL() string {
|
||||
}
|
||||
|
||||
func (s *Server) renderDiagramsInHTML(
|
||||
r *http.Request,
|
||||
path string,
|
||||
html string,
|
||||
) string {
|
||||
|
||||
@@ -109,20 +137,35 @@ func (s *Server) renderDiagramsInHTML(
|
||||
htmlUnescapeString(
|
||||
m[2],
|
||||
)
|
||||
if len(source) > 32<<10 || path == "" {
|
||||
return block
|
||||
}
|
||||
|
||||
hash :=
|
||||
diagramHash(
|
||||
diagramType,
|
||||
source,
|
||||
htmlProjectionVersion+"\x00"+s.domainUUIDForRequest(r)+"\x00"+path+"\x00"+source,
|
||||
"svg",
|
||||
)
|
||||
|
||||
diagramSourceMu.Lock()
|
||||
if len(diagramSourceCache) >= 128 {
|
||||
oldest, key := time.Now(), ""
|
||||
for k, v := range diagramSourceCache {
|
||||
if v.At.Before(oldest) {
|
||||
oldest, key = v.At, k
|
||||
}
|
||||
}
|
||||
delete(diagramSourceCache, key)
|
||||
}
|
||||
diagramSourceCache[hash] =
|
||||
diagramCacheEntry{
|
||||
Type: diagramType,
|
||||
Source: source,
|
||||
At: time.Now(),
|
||||
Domain: s.domainUUIDForRequest(r), Path: path,
|
||||
}
|
||||
diagramSourceMu.Unlock()
|
||||
|
||||
return `<figure class="diagram diagram-` + diagramType + `">` +
|
||||
`<img src="/:/compile/diagram/` + diagramType + `/` + hash + `.svg" loading="lazy" alt="` + diagramType + ` diagram">` +
|
||||
@@ -153,6 +196,10 @@ func (s *Server) handleCompileDiagram(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) {
|
||||
// Apply also to disk-cache hits and direct navigation, not only new renders.
|
||||
w.Header().Set("Content-Security-Policy", "sandbox; default-src 'none'; style-src 'unsafe-inline'; img-src data:")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.Header().Set("Cache-Control", "private, no-store")
|
||||
|
||||
if r.Method == http.MethodPost {
|
||||
s.handleCompileDiagramPost(
|
||||
@@ -208,14 +255,16 @@ func (s *Server) handleCompileDiagram(
|
||||
return
|
||||
}
|
||||
|
||||
cachePath :=
|
||||
filepath.Join(
|
||||
"/data/cache/diagram",
|
||||
diagramType,
|
||||
hash+".svg",
|
||||
)
|
||||
cachePath := s.diagramCachePath(diagramType, hash)
|
||||
|
||||
if b, err := os.ReadFile(cachePath); err == nil {
|
||||
diagramSourceMu.Lock()
|
||||
entry, ok := diagramSourceCache[hash]
|
||||
diagramSourceMu.Unlock()
|
||||
if !ok || entry.Type != diagramType || entry.Domain != s.domainUUIDForRequest(r) || time.Since(entry.At) > 30*time.Minute || !s.pathReadFilter(r)(entry.Path) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if b, err := readDiagramCache(cachePath); err == nil {
|
||||
|
||||
w.Header().Set(
|
||||
"Content-Type",
|
||||
@@ -226,16 +275,9 @@ func (s *Server) handleCompileDiagram(
|
||||
return
|
||||
}
|
||||
|
||||
entry, ok :=
|
||||
diagramSourceCache[hash]
|
||||
|
||||
if !ok || entry.Type != diagramType {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
s.renderDiagramSVG(
|
||||
w,
|
||||
r,
|
||||
diagramType,
|
||||
entry.Source,
|
||||
cachePath,
|
||||
@@ -269,12 +311,12 @@ func (s *Server) handleCompileDiagramPost(
|
||||
io.ReadAll(
|
||||
io.LimitReader(
|
||||
r.Body,
|
||||
1024*1024,
|
||||
(1<<20)+1,
|
||||
),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
if err != nil || len(sourceBytes) > 1<<20 {
|
||||
http.Error(w, "diagram source exceeds limit", http.StatusRequestEntityTooLarge)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -288,14 +330,9 @@ func (s *Server) handleCompileDiagramPost(
|
||||
"svg",
|
||||
)
|
||||
|
||||
cachePath :=
|
||||
filepath.Join(
|
||||
"/data/cache/diagram",
|
||||
diagramType,
|
||||
hash+".svg",
|
||||
)
|
||||
cachePath := s.diagramCachePath(diagramType, hash)
|
||||
|
||||
if b, err := os.ReadFile(cachePath); err == nil {
|
||||
if b, err := readDiagramCache(cachePath); err == nil {
|
||||
|
||||
w.Header().Set(
|
||||
"Content-Type",
|
||||
@@ -308,6 +345,7 @@ func (s *Server) handleCompileDiagramPost(
|
||||
|
||||
s.renderDiagramSVG(
|
||||
w,
|
||||
r,
|
||||
diagramType,
|
||||
source,
|
||||
cachePath,
|
||||
@@ -316,13 +354,15 @@ func (s *Server) handleCompileDiagramPost(
|
||||
|
||||
func (s *Server) renderDiagramSVG(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
diagramType string,
|
||||
source string,
|
||||
cachePath string,
|
||||
) {
|
||||
|
||||
req, err :=
|
||||
http.NewRequest(
|
||||
http.NewRequestWithContext(
|
||||
r.Context(),
|
||||
http.MethodPost,
|
||||
krokiURL()+"/"+diagramType+"/svg",
|
||||
bytes.NewBufferString(source),
|
||||
@@ -364,11 +404,11 @@ func (s *Server) renderDiagramSVG(
|
||||
|
||||
b, err :=
|
||||
io.ReadAll(
|
||||
resp.Body,
|
||||
io.LimitReader(resp.Body, (4<<20)+1),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
if err != nil || len(b) > 4<<20 {
|
||||
http.Error(w, "diagram output unavailable or exceeds limit", http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -392,7 +432,7 @@ func (s *Server) renderDiagramSVG(
|
||||
|
||||
w.Header().Set(
|
||||
"Content-Security-Policy",
|
||||
"default-src 'none'; style-src 'unsafe-inline'; img-src data:",
|
||||
"sandbox; default-src 'none'; style-src 'unsafe-inline'; img-src data:",
|
||||
)
|
||||
|
||||
_, _ =
|
||||
|
||||
Reference in new issue
Block a user