Skip to content

Commit b065505

Browse files
fix: clean up invalid materialized images (#10090)
* fix: clean up invalid materialized images * style: restore LF line endings in image cleanup fix --------- Co-authored-by: Soulter <[email protected]>
1 parent 54c98e8 commit b065505

2 files changed

Lines changed: 80 additions & 13 deletions

File tree

‎astrbot/core/pipeline/preprocess_stage/stage.py‎

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from astrbot.core.utils.astrbot_path import get_astrbot_temp_path
1212
from astrbot.core.utils.media_utils import (
1313
describe_media_ref,
14+
detect_image_mime_type_async,
1415
ensure_wav,
1516
file_uri_to_path,
1617
is_file_uri,
@@ -47,6 +48,54 @@ def _track_temp_media(event: AstrMessageEvent, media_path: str) -> None:
4748
return
4849
event.track_temporary_local_file(str(path))
4950

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+
5099
async def process(
51100
self,
52101
event: AstrMessageEvent,
@@ -129,14 +178,8 @@ async def process(
129178
logger.warning(f"Voice processing failed: {e}")
130179
elif isinstance(component, Image):
131180
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)
137182
message_chain[idx] = component
138-
# Attachment references outlive the event; model copies do not.
139-
event.untrack_temporary_local_file(image_path)
140183
except Exception as e:
141184
media_ref = component.url or component.file
142185
logger.warning(
@@ -164,13 +207,8 @@ async def process(
164207
)
165208
elif isinstance(reply_comp, Image):
166209
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)
172211
component.chain[idx] = reply_comp
173-
event.untrack_temporary_local_file(image_path)
174212
except Exception as e:
175213
media_ref = reply_comp.url or reply_comp.file
176214
logger.warning(

‎tests/test_preprocess_stage.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,35 @@ async def test_preprocess_image_cleanup_preserves_usable_file(
150150
assert Path(await image.convert_to_file_path()).exists()
151151

152152

153+
@pytest.mark.asyncio
154+
@pytest.mark.parametrize("quoted", [False, True])
155+
async def test_preprocess_image_cleanup_removes_invalid_materialized_file(
156+
tmp_path, monkeypatch, quoted
157+
):
158+
monkeypatch.setattr(media_utils, "get_astrbot_temp_path", lambda: str(tmp_path))
159+
monkeypatch.setattr(
160+
preprocess_stage, "get_astrbot_temp_path", lambda: str(tmp_path)
161+
)
162+
reference = "data:image/png;base64," + base64.b64encode(b"not an image").decode()
163+
image = Image(file=reference)
164+
event = FakeEvent([Reply(id="reply-1", chain=[image])] if quoted else [image])
165+
stage = PreProcessStage()
166+
stage.config = {}
167+
stage.platform_settings = {}
168+
stage.stt_settings = {"enable": False}
169+
170+
await stage.process(event)
171+
172+
materialized = list(tmp_path.glob("media_image_*"))
173+
assert materialized
174+
assert image.file == reference
175+
assert len(event.temporary_local_files) == 1
176+
AstrMessageEvent.cleanup_temporary_local_files(
177+
SimpleNamespace(_temporary_local_files=event.temporary_local_files)
178+
)
179+
assert not [path for path in materialized if path.exists()]
180+
181+
153182
@pytest.mark.asyncio
154183
async def test_preprocess_path_mapping_accepts_file_uri(tmp_path):
155184
from PIL import Image as PILImage

0 commit comments

Comments
 (0)