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.
107 lines
3.0 KiB
Go
107 lines
3.0 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"pageswiki/internal/store"
|
|
)
|
|
|
|
type pathPickerEntry struct {
|
|
Name string `json:"name"`
|
|
Path string `json:"path"`
|
|
Kind string `json:"kind"`
|
|
ContentType string `json:"content_type,omitempty"`
|
|
Size int `json:"size,omitempty"`
|
|
}
|
|
|
|
func (s *Server) handlePathPickerAPI(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Cache-Control", "private, no-store")
|
|
if r.Method != http.MethodGet {
|
|
w.Header().Set("Allow", http.MethodGet)
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
current, ok := normalizePickerPath(r.URL.Query().Get("path"))
|
|
if !ok {
|
|
http.Error(w, "invalid path", http.StatusBadRequest)
|
|
return
|
|
}
|
|
if current == "" {
|
|
current = "/"
|
|
}
|
|
mode := strings.TrimSpace(r.URL.Query().Get("mode"))
|
|
switch mode {
|
|
case "", "any", "file", "folder", "image", "image-or-folder", "children":
|
|
default:
|
|
http.Error(w, "invalid picker mode", http.StatusBadRequest)
|
|
return
|
|
}
|
|
selected := ""
|
|
if mode != "folder" && mode != "children" && current != "/" {
|
|
if objectUUID, resolveErr := s.store.ResolvePathInDomain(s.domainUUIDForRequest(r), current); resolveErr == nil {
|
|
if revision, revisionErr := s.latestRevisionMetadataForRequest(r, objectUUID); revisionErr == nil && (mode == "" || mode == "any" || mode == "file" || imageContentType(revision.ContentType)) {
|
|
selected = current
|
|
current = parentPath(current)
|
|
}
|
|
}
|
|
}
|
|
prefix := r.URL.Query().Get("prefix")
|
|
if len(prefix) > 1024 {
|
|
http.Error(w, "prefix too long", http.StatusBadRequest)
|
|
return
|
|
}
|
|
var entries []store.BrowserEntry
|
|
var err error
|
|
truncated := false
|
|
if mode == "children" {
|
|
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second)
|
|
defer cancel()
|
|
entries, truncated, err = s.store.ListPathCompletionNames(ctx, s.domainUUIDForRequest(r), current, prefix)
|
|
} else {
|
|
entries, err = s.store.ListBrowserEntriesInDomain(s.domainUUIDForRequest(r), current)
|
|
}
|
|
if err != nil {
|
|
http.Error(w, "path listing unavailable", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
result := make([]pathPickerEntry, 0, min(len(entries), 250))
|
|
for _, entry := range entries {
|
|
if prefix != "" && !strings.HasPrefix(strings.ToLower(entry.Name), strings.ToLower(prefix)) {
|
|
continue
|
|
}
|
|
if !s.requestCanReadPath(r, entry.Path) {
|
|
continue
|
|
}
|
|
if !entry.IsDir {
|
|
if mode == "folder" {
|
|
continue
|
|
}
|
|
if (mode == "image" || mode == "image-or-folder") && !imageContentType(entry.ContentType) {
|
|
continue
|
|
}
|
|
}
|
|
if len(result) == cap(result) {
|
|
truncated = true
|
|
break
|
|
}
|
|
kind := "file"
|
|
if entry.IsDir {
|
|
kind = "folder"
|
|
}
|
|
result = append(result, pathPickerEntry{Name: entry.Name, Path: entry.Path, Kind: kind, ContentType: entry.ContentType, Size: entry.Size})
|
|
}
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
_ = json.NewEncoder(w).Encode(map[string]any{
|
|
"path": current,
|
|
"parent": parentPath(current),
|
|
"mode": mode,
|
|
"selected": selected,
|
|
"entries": result,
|
|
"truncated": truncated,
|
|
})
|
|
}
|