Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts. Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation. Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
101 lines
3.0 KiB
Go
101 lines
3.0 KiB
Go
package store
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"pageswiki/internal/mestionora"
|
|
)
|
|
|
|
type BrowserEntry struct {
|
|
Name string
|
|
Path string
|
|
ObjectUUID string
|
|
IsDir bool
|
|
Size int
|
|
UpdatedAt int64
|
|
ChildCount int
|
|
ContentType string
|
|
Filename string
|
|
DocumentHash string
|
|
RevisionUUID string
|
|
Title string
|
|
}
|
|
|
|
func firstHTMLHeading(source string) string { return mestionora.FirstHTMLHeading(source) }
|
|
|
|
func normalizeBrowserPath(p string) string {
|
|
p = strings.TrimSpace(p)
|
|
if p == "" {
|
|
return "/"
|
|
}
|
|
if !strings.HasPrefix(p, "/") {
|
|
p = "/" + p
|
|
}
|
|
if p != "/" {
|
|
p = strings.TrimRight(p, "/")
|
|
}
|
|
return p
|
|
}
|
|
|
|
func (s *Store) ListBrowserEntries(parent string) ([]BrowserEntry, error) {
|
|
domainUUID, err := s.defaultDomainUUID()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return s.ListBrowserEntriesInDomain(domainUUID, parent)
|
|
}
|
|
|
|
func (s *Store) ListBrowserEntriesInDomain(domainUUID, parent string) ([]BrowserEntry, error) {
|
|
parent = normalizeBrowserPath(parent)
|
|
prefix := strings.TrimRight(parent, "/") + "/"
|
|
// Collapse descendants before consulting revisions. A folder listing must
|
|
// not rank every revision of every document in the subtree. Binary ranges
|
|
// keep prefix matching literal (including %, _) and use the Path index.
|
|
rows, err := s.DB.Query(`
|
|
WITH relative AS (
|
|
SELECT substr(path, length(?) + 1) AS name, object_uuid
|
|
FROM paths WHERE domain_uuid=? AND path >= ? AND path < ? AND path != ?
|
|
), children AS (
|
|
SELECT CASE WHEN instr(name,'/')=0 THEN name ELSE substr(name,1,instr(name,'/')-1) END AS name,
|
|
MAX(CASE WHEN instr(name,'/')=0 THEN object_uuid ELSE '' END) AS object_uuid,
|
|
MAX(instr(name,'/')>0) AS is_dir,
|
|
SUM(instr(name,'/')>0) AS child_count
|
|
FROM relative WHERE name != '' GROUP BY 1
|
|
)
|
|
SELECT c.name,c.object_uuid,c.is_dir,c.child_count,
|
|
COALESCE(length(CAST(r.body AS BLOB)),0),COALESCE(r.created_at,0),
|
|
COALESCE(r.content_type,''),COALESCE(r.filename,''),
|
|
COALESCE(r.document_hash,''),COALESCE(r.uuid,''),
|
|
COALESCE(substr(CAST(r.body AS TEXT),1,65536),'')
|
|
FROM children c LEFT JOIN revisions r ON r.rowid=(
|
|
SELECT head.rowid FROM revisions head
|
|
WHERE c.is_dir=0 AND head.object_uuid=c.object_uuid
|
|
ORDER BY head.created_at_ms DESC, head.rowid DESC LIMIT 1
|
|
)
|
|
ORDER BY c.is_dir DESC,c.name ASC
|
|
`, prefix, domainUUID, prefix, strings.TrimSuffix(prefix, "/")+"0", parent)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var out []BrowserEntry
|
|
for rows.Next() {
|
|
var entry BrowserEntry
|
|
if err := rows.Scan(&entry.Name, &entry.ObjectUUID, &entry.IsDir, &entry.ChildCount,
|
|
&entry.Size, &entry.UpdatedAt, &entry.ContentType, &entry.Filename, &entry.DocumentHash, &entry.RevisionUUID, &entry.Title); err != nil {
|
|
return nil, err
|
|
}
|
|
if !entry.IsDir {
|
|
entry.Title = firstHTMLHeading(entry.Title)
|
|
}
|
|
entry.Path = prefix + entry.Name
|
|
if !entry.IsDir && s.DocumentRoot != "" && entry.DocumentHash != "" {
|
|
if size, err := s.DocumentSize(entry.DocumentHash); err == nil {
|
|
entry.Size = int(size)
|
|
}
|
|
}
|
|
out = append(out, entry)
|
|
}
|
|
return out, rows.Err()
|
|
}
|