"""Microsoft Teams 媒体辅助工具。 提供 MIME 类型识别、文件名提取、消息 ID 提取等辅助函数。 """ from __future__ import annotations import mimetypes import os from typing import Any def get_mime_type(filename: str) -> str: mime_type, _ = mimetypes.guess_type(filename) return mime_type or "application/octet-stream" def extract_filename(activity: dict[str, Any]) -> str: attachments = activity.get("attachments", []) or [] for att in attachments: name = att.get("name", "").strip() if name: return os.path.basename(name) content = att.get("content", {}) or {} content_name = content.get("name", "").strip() if content_name: return os.path.basename(content_name) return "" def extract_message_id(activity: dict[str, Any]) -> str: msg_id = activity.get("id", "") if msg_id: return msg_id channel_data = activity.get("channelData", {}) or {} channel_msg_id = channel_data.get("id", "") if channel_msg_id: return channel_msg_id return "" def extract_attachment_urls(activity: dict[str, Any]) -> list[dict[str, str]]: urls: list[dict[str, str]] = [] attachments = activity.get("attachments", []) or [] for att in attachments: content_type = att.get("contentType", "") content_url = att.get("contentUrl", "") name = att.get("name", "") if content_url: urls.append( { "url": content_url, "name": name or os.path.basename(content_url), "content_type": content_type, } ) return urls def get_edited_timestamp(activity: dict[str, Any]) -> str | None: channel_data = activity.get("channelData", {}) or {} edit_time = channel_data.get("editedTimestamp", "") if edit_time: return edit_time return activity.get("editedTimestamp")