diff --git a/backend/open_webui/routers/files.py b/backend/open_webui/routers/files.py index c30366545..8a2888d86 100644 --- a/backend/open_webui/routers/files.py +++ b/backend/open_webui/routers/files.py @@ -1,6 +1,7 @@ import logging import os import uuid +from fnmatch import fnmatch from pathlib import Path from typing import Optional from urllib.parse import quote @@ -177,6 +178,47 @@ async def list_files(user=Depends(get_verified_user), content: bool = Query(True return files +############################ +# Search Files +############################ + + +@router.get("/search", response_model=list[FileModelResponse]) +async def search_files( + filename: str = Query( + ..., + description="Filename pattern to search for. Supports wildcards such as '*.txt'", + ), + content: bool = Query(True), + user=Depends(get_verified_user), +): + """ + Search for files by filename with support for wildcard patterns. + """ + # Get files according to user role + if user.role == "admin": + files = Files.get_files() + else: + files = Files.get_files_by_user_id(user.id) + + # Get matching files + matching_files = [ + file for file in files if fnmatch(file.filename.lower(), filename.lower()) + ] + + if not matching_files: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail="No files found matching the pattern.", + ) + + if not content: + for file in matching_files: + del file.data["content"] + + return matching_files + + ############################ # Delete All Files ############################