-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathutils.py
More file actions
45 lines (31 loc) · 1.44 KB
/
utils.py
File metadata and controls
45 lines (31 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""Utility functions for multimodal file handling."""
import base64
import re
import httpx
from uipath._utils._ssl_context import get_httpx_client_kwargs
from .types import IMAGE_MIME_TYPES
def sanitize_filename(filename: str) -> str:
"""Sanitize a filename to conform to provider document naming requirements.
Bedrock only allows: alphanumeric characters, whitespace, hyphens,
parentheses, and square brackets. No consecutive whitespace allowed.
"""
if not filename or filename.isspace():
return "document"
sanitized = re.sub(r"[^a-zA-Z0-9\s\-\(\)\[\]]", "-", filename)
sanitized = re.sub(r"\s+", " ", sanitized)
sanitized = re.sub(r"-+", "-", sanitized)
sanitized = sanitized.strip(" -")
return sanitized if sanitized else "document"
def is_pdf(mime_type: str) -> bool:
"""Check if the MIME type represents a PDF document."""
return mime_type.lower() == "application/pdf"
def is_image(mime_type: str) -> bool:
"""Check if the MIME type represents a supported image format."""
return mime_type.lower() in IMAGE_MIME_TYPES
async def download_file_base64(url: str) -> str:
"""Download a file from a URL and return its content as a base64 string."""
async with httpx.AsyncClient(**get_httpx_client_kwargs()) as client:
response = await client.get(url)
response.raise_for_status()
file_content = response.content
return base64.b64encode(file_content).decode("utf-8")