import httpx import re import mimetypes from uuid import UUID, uuid4 from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy import select from models.attachment import Attachment from core.storage import get_storage_provider from core.logging import get_logger logger = get_logger(__name__) # Comprehensive pattern for images: Markdown ![alt](url) OR raw URLs ending in extensions # Matches: # 1. ![alt text](https://url) # 2. https://url.jpg, .png, .jpeg, .webp, .gif (even without markdown) IMAGE_PATTERN = re.compile( r'(?:!\[.*?\]\((https?://[^\s\)]+)\))|' r'(https?://[^\s\)]+(?:\.jpg|\.jpeg|\.png|\.webp|\.gif)(?:\?[^\s\)]*)?)', re.IGNORECASE ) INTERNAL_ATTACHMENT_PATTERN = re.compile(r'/attachments/([0-9a-fA-F-]{36})/') async def parse_mentions(content: str, workspace_id: UUID, db: AsyncSession) -> list[dict[str, any]]: """ Parses mentions from content. Supports: 1. Structured @[Name](UUID) - modern RichEditor format 2. Structured @[Name](email) - historical/manual format 3. Simple @email - legacy format """ if not content: return [] # 1. Match structured @[Name](email or UUID) structured_raw = re.findall(r"@\[.*?\]\(([a-fA-F0-9-]{36}|[\w\.-]+@[\w\.-]+\.\w+)\)", content) # 2. Match simple @email simple_emails = re.findall(r"(? {storage_path}") except Exception as e: logger.error(f"Image Sync: Error processing {url}: {e}") if new_attachments: # We flush so IDs are available for multimodal processing in the same task await db.flush() logger.info(f"Image Sync: Successfully created {len(new_attachments)} attachments for {entity_type} {entity_id}") return new_attachments