feat: SHIFT-CMD-F zoek functionaliteit toegevoegd

This commit is contained in:
kodi
2026-03-12 11:22:24 +01:00
parent 8c2fbfef74
commit 6f8f884d75
20 changed files with 688 additions and 0 deletions
Binary file not shown.
+18
View File
@@ -0,0 +1,18 @@
from __future__ import annotations
from fastapi import APIRouter, Depends
from backend.app.api.schemas import SearchResponse
from backend.app.dependencies import get_search_service
from backend.app.services.search_service import SearchService
router = APIRouter(prefix="/search")
@router.get("", response_model=SearchResponse)
async def search(
path: str,
query: str,
service: SearchService = Depends(get_search_service),
) -> SearchResponse:
return service.search(path=path, query=query)
+13
View File
@@ -166,3 +166,16 @@ class HistoryItem(BaseModel):
class HistoryListResponse(BaseModel):
items: list[HistoryItem]
class SearchResultItem(BaseModel):
name: str
path: str
type: str
parent_path: str
root: str
class SearchResponse(BaseModel):
items: list[SearchResultItem]
truncated: bool
+5
View File
@@ -14,6 +14,7 @@ from backend.app.services.copy_task_service import CopyTaskService
from backend.app.services.file_ops_service import FileOpsService
from backend.app.services.history_service import HistoryService
from backend.app.services.move_task_service import MoveTaskService
from backend.app.services.search_service import SearchService
from backend.app.services.task_service import TaskService
from backend.app.tasks_runner import TaskRunner
@@ -95,3 +96,7 @@ async def get_bookmark_service() -> BookmarkService:
async def get_history_service() -> HistoryService:
return HistoryService(repository=get_history_repository())
async def get_search_service() -> SearchService:
return SearchService(path_guard=get_path_guard(), filesystem=get_filesystem_adapter())
@@ -29,6 +29,29 @@ class FilesystemAdapter:
return directories, files
def search_names(self, directory: Path, query: str, limit: int) -> tuple[list[dict], bool]:
normalized_query = query.lower()
results: list[dict] = []
for root, dirnames, filenames in __import__("os").walk(directory):
dirnames[:] = sorted([name for name in dirnames if not name.startswith(".")], key=str.lower)
filenames = sorted([name for name in filenames if not name.startswith(".")], key=str.lower)
root_path = Path(root)
for name in dirnames:
if normalized_query in name.lower():
results.append({"name": name, "kind": "directory", "absolute": root_path / name})
if len(results) >= limit:
return results, True
for name in filenames:
if normalized_query in name.lower():
results.append({"name": name, "kind": "file", "absolute": root_path / name})
if len(results) >= limit:
return results, True
return results, False
def make_directory(self, path: Path) -> None:
path.mkdir(parents=False, exist_ok=False)
+2
View File
@@ -13,6 +13,7 @@ from backend.app.api.routes_copy import router as copy_router
from backend.app.api.routes_files import router as files_router
from backend.app.api.routes_history import router as history_router
from backend.app.api.routes_move import router as move_router
from backend.app.api.routes_search import router as search_router
from backend.app.api.routes_tasks import router as tasks_router
from backend.app.logging import configure_logging
@@ -29,6 +30,7 @@ app.include_router(browse_router, prefix="/api")
app.include_router(files_router, prefix="/api")
app.include_router(copy_router, prefix="/api")
app.include_router(move_router, prefix="/api")
app.include_router(search_router, prefix="/api")
app.include_router(bookmarks_router, prefix="/api")
app.include_router(history_router, prefix="/api")
app.include_router(tasks_router, prefix="/api")
@@ -0,0 +1,82 @@
from __future__ import annotations
from backend.app.api.errors import AppError
from backend.app.api.schemas import SearchResponse, SearchResultItem
from backend.app.fs.filesystem_adapter import FilesystemAdapter
from backend.app.security.path_guard import PathGuard
SEARCH_RESULT_LIMIT = 100
SEARCH_MIN_QUERY_LENGTH = 3
class SearchService:
def __init__(self, path_guard: PathGuard, filesystem: FilesystemAdapter):
self._path_guard = path_guard
self._filesystem = filesystem
def search(self, path: str, query: str) -> SearchResponse:
normalized_query = (query or "").strip()
if len(normalized_query) < SEARCH_MIN_QUERY_LENGTH:
raise AppError(
code="invalid_request",
message=f"Query must be at least {SEARCH_MIN_QUERY_LENGTH} characters",
status_code=400,
)
items: list[SearchResultItem] = []
truncated = False
if self._path_guard.is_virtual_volumes_path(path):
for entry in self._path_guard.virtual_volumes_entries():
resolved = self._path_guard.resolve_directory_path(entry["path"])
remaining = SEARCH_RESULT_LIMIT - len(items)
if remaining <= 0:
truncated = True
break
matches, chunk_truncated = self._filesystem.search_names(
resolved.absolute,
normalized_query,
remaining,
)
items.extend(self._map_results(matches, resolved.display_style))
if chunk_truncated or len(items) >= SEARCH_RESULT_LIMIT:
truncated = True
break
return SearchResponse(items=items, truncated=truncated)
resolved = self._path_guard.resolve_directory_path(path)
matches, truncated = self._filesystem.search_names(
resolved.absolute,
normalized_query,
SEARCH_RESULT_LIMIT,
)
return SearchResponse(items=self._map_results(matches, resolved.display_style), truncated=truncated)
def _map_results(self, matches: list[dict], display_style: str) -> list[SearchResultItem]:
items: list[SearchResultItem] = []
for match in matches:
absolute = match["absolute"]
alias = self._match_alias_for_path(absolute)
path = self._path_guard.entry_relative_path(alias, absolute, display_style=display_style)
parent_path = self._path_guard.entry_relative_path(alias, absolute.parent, display_style=display_style)
items.append(
SearchResultItem(
name=match["name"],
path=path,
type=match["kind"],
parent_path=parent_path,
root=alias,
)
)
return items
def _match_alias_for_path(self, absolute) -> str:
resolved = absolute.resolve(strict=False)
for alias, root in self._path_guard._roots.items(): # internal mapping, shared security source
if self._path_guard._is_under_root(resolved, root):
return alias
raise AppError(
code="path_outside_whitelist",
message="Requested path is outside allowed roots",
status_code=403,
)