package server import ( "encoding/json" "net/http" "strings" ) 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": default: http.Error(w, "invalid picker mode", http.StatusBadRequest) return } selected := "" if mode != "folder" && 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) } } } 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)) truncated := false for _, entry := range entries { 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, }) }