Files
pages/internal/server/readable_navigation.go
T
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

139 lines
4.0 KiB
Go

package server
import (
"fmt"
"html"
"net/http"
"path"
"sort"
"strings"
"pageswiki/internal/store"
)
// Branches derive only from actual readable Paths. A virtual parent is a
// navigation container, not authorization to read a private Page at that Path.
func (s *Server) readableNavigation(r *http.Request) (*publicNavigationSnapshot, error) {
paths, err := s.navigationPaths(r.Context(), s.domainUUIDForRequest(r))
if err != nil {
return nil, err
}
allowed := s.pathReadFilter(r)
p := &publicNavigationSnapshot{pages: map[string]store.Revision{}, branches: map[string]bool{}}
for _, item := range paths {
if !strings.HasPrefix(item, "/") || path.Clean(item) != item || strings.ContainsAny(item, "\\\x00\r\n") {
continue
}
if !allowed(item) {
continue
}
if strings.Count(item, "/") > 128 {
return nil, store.ErrNavigationBudget
}
p.paths = append(p.paths, item)
p.pages[item] = store.Revision{}
for at := item; ; at = path.Dir(at) {
p.branches[at] = true
if len(p.branches) > store.MaxNavigationEntries {
return nil, store.ErrNavigationBudget
}
if at == "/" {
break
}
}
}
return p, nil
}
func (s *Server) renderReadableChildren(r *http.Request, args childrenMacroArgs) (string, error) {
p, err := s.readableNavigation(r)
if err != nil {
return "", err
}
scope := path.Clean(args.TargetPath)
if scope == "/" {
args.Excludes = append(args.Excludes, "/u")
}
prefix := strings.TrimRight(scope, "/") + "/"
children := map[string]bool{}
for _, item := range p.paths {
if item == scope || !strings.HasPrefix(item, prefix) || isExcludedPath(item, args.Excludes) {
continue
}
child := prefix + strings.Split(strings.TrimPrefix(item, prefix), "/")[0]
if !isExcludedPath(child, args.Excludes) {
children[child] = true
}
}
keys := []string{}
for child := range children {
keys = append(keys, child)
}
sort.Strings(keys)
start := max(0, min(args.Offset, len(keys)))
limit := args.Limit
if limit <= 0 || limit > 400 {
limit = 120
}
end := min(start+limit, len(keys))
page := keys[start:end]
real := []string{}
for _, item := range page {
if p.allows(item) {
real = append(real, item)
}
}
entries := map[string]store.BrowserEntry{}
if args.View != "list" {
found, _, err := s.store.ListSelectedChildEntriesInDomain(s.domainUUIDForRequest(r), scope, real)
if err != nil {
return "", err
}
for _, e := range found {
entries[e.Path] = e
}
}
var out strings.Builder
switch args.View {
case "icon":
out.WriteString(`<div class="children-icon-grid">`)
case "detail":
out.WriteString(`<table class="children-detail"><tbody>`)
default:
out.WriteString(`<ul class="children-view-list">`)
}
for _, item := range page {
href := p.href(item)
name := html.EscapeString(path.Base(item))
entry, ok := entries[item]
if !ok {
entry = store.BrowserEntry{Path: item, IsDir: !p.allows(item)}
}
switch args.View {
case "icon":
// A private parent shown as a folder links to its filtered tree.
out.WriteString(`<a class="children-icon-item children-entry" data-child-path="` + html.EscapeString(item) + `" href="` + html.EscapeString(href) + `">` + s.entryPreviewHTML(entry, false) + `<span class="children-icon-name">` + name + `</span></a>`)
case "detail":
kind := entry.ContentType
if entry.IsDir {
kind = "folder"
}
out.WriteString(fmt.Sprintf(`<tr class="children-entry" data-child-path="%s"><td><a href="%s">%s</a></td><td>%s</td><td>%d</td></tr>`, html.EscapeString(item), html.EscapeString(href), name, html.EscapeString(kind), entry.Size))
default:
out.WriteString(`<li class="children-entry" data-child-path="` + html.EscapeString(item) + `"><a href="` + html.EscapeString(href) + `">` + name + `</a></li>`)
}
}
switch args.View {
case "icon":
out.WriteString(`</div>`)
case "detail":
out.WriteString(`</tbody></table>`)
default:
out.WriteString(`</ul>`)
}
args.TargetPath = scope
args.Limit = limit
writeChildrenMore(&out, args, end, len(keys))
return out.String(), nil
}