|
11 | 11 | from astrbot.core.utils.astrbot_path import get_astrbot_temp_path |
12 | 12 | from astrbot.core.utils.media_utils import ( |
13 | 13 | describe_media_ref, |
| 14 | + detect_image_mime_type_async, |
14 | 15 | ensure_wav, |
15 | 16 | file_uri_to_path, |
16 | 17 | is_file_uri, |
@@ -47,6 +48,54 @@ def _track_temp_media(event: AstrMessageEvent, media_path: str) -> None: |
47 | 48 | return |
48 | 49 | event.track_temporary_local_file(str(path)) |
49 | 50 |
|
| 51 | + @staticmethod |
| 52 | + def _is_existing_local_image_ref(media_ref: str | None) -> bool: |
| 53 | + """Return whether an image reference already points at a local file.""" |
| 54 | + if not media_ref: |
| 55 | + return False |
| 56 | + if is_file_uri(media_ref): |
| 57 | + return True |
| 58 | + if media_ref.startswith(("http://", "https://", "data:", "base64://")): |
| 59 | + return False |
| 60 | + try: |
| 61 | + return Path(media_ref).exists() |
| 62 | + except OSError: |
| 63 | + return False |
| 64 | + |
| 65 | + async def _normalize_image_component( |
| 66 | + self, |
| 67 | + event: AstrMessageEvent, |
| 68 | + component: Image, |
| 69 | + ) -> None: |
| 70 | + """Resolve and validate an image while preserving cleanup ownership.""" |
| 71 | + media_ref = component.url or component.file |
| 72 | + image_path: str | None = None |
| 73 | + materialized = False |
| 74 | + try: |
| 75 | + image_path = await component.convert_to_file_path() |
| 76 | + materialized = ( |
| 77 | + not self._is_existing_local_image_ref(media_ref) |
| 78 | + and Path(image_path).is_file() |
| 79 | + ) |
| 80 | + if materialized: |
| 81 | + self._track_temp_media(event, image_path) |
| 82 | + detected_mime_type = await detect_image_mime_type_async( |
| 83 | + image_path, |
| 84 | + default_mime_type=None, |
| 85 | + ) |
| 86 | + if detected_mime_type is None: |
| 87 | + raise ValueError("image content could not be identified") |
| 88 | + except Exception: |
| 89 | + if image_path and not materialized: |
| 90 | + event.untrack_temporary_local_file(image_path) |
| 91 | + raise |
| 92 | + |
| 93 | + component.file = image_path |
| 94 | + component.path = image_path |
| 95 | + # Image.convert_to_file_path() prefers url, so keep it aligned. |
| 96 | + component.url = image_path |
| 97 | + event.untrack_temporary_local_file(image_path) |
| 98 | + |
50 | 99 | async def process( |
51 | 100 | self, |
52 | 101 | event: AstrMessageEvent, |
@@ -129,14 +178,8 @@ async def process( |
129 | 178 | logger.warning(f"Voice processing failed: {e}") |
130 | 179 | elif isinstance(component, Image): |
131 | 180 | try: |
132 | | - image_path = await component.convert_to_file_path() |
133 | | - component.file = image_path |
134 | | - component.path = image_path |
135 | | - # Image.convert_to_file_path() prefers url, so keep it aligned. |
136 | | - component.url = image_path |
| 181 | + await self._normalize_image_component(event, component) |
137 | 182 | message_chain[idx] = component |
138 | | - # Attachment references outlive the event; model copies do not. |
139 | | - event.untrack_temporary_local_file(image_path) |
140 | 183 | except Exception as e: |
141 | 184 | media_ref = component.url or component.file |
142 | 185 | logger.warning( |
@@ -164,13 +207,8 @@ async def process( |
164 | 207 | ) |
165 | 208 | elif isinstance(reply_comp, Image): |
166 | 209 | try: |
167 | | - image_path = await reply_comp.convert_to_file_path() |
168 | | - reply_comp.file = image_path |
169 | | - reply_comp.path = image_path |
170 | | - # Image.convert_to_file_path() prefers url, so keep it aligned. |
171 | | - reply_comp.url = image_path |
| 210 | + await self._normalize_image_component(event, reply_comp) |
172 | 211 | component.chain[idx] = reply_comp |
173 | | - event.untrack_temporary_local_file(image_path) |
174 | 212 | except Exception as e: |
175 | 213 | media_ref = reply_comp.url or reply_comp.file |
176 | 214 | logger.warning( |
|
0 commit comments