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(`
`) case "detail": out.WriteString(``) default: out.WriteString(``, html.EscapeString(item), html.EscapeString(href), name, html.EscapeString(kind), entry.Size)) default: out.WriteString(`
  • ` + name + `
  • `) } } switch args.View { case "icon": out.WriteString(``) case "detail": out.WriteString(`
    %s%s%d
    `) default: out.WriteString(``) } args.TargetPath = scope args.Limit = limit writeChildrenMore(&out, args, end, len(keys)) return out.String(), nil }