feat: delete all bot log images at startup (#1650)

This commit is contained in:
Junyan Qin
2025-11-06 20:02:07 +08:00
parent cb48221ed3
commit 17070471f7
4 changed files with 22 additions and 1 deletions

View File

@@ -157,6 +157,9 @@ class PlatformManager:
self.adapter_dict = {}
async def initialize(self):
# delete all bot log images
await self.ap.storage_mgr.storage_provider.delete_dir_recursive('bot_log_images')
self.adapter_components = self.ap.discover.get_components_by_kind('MessagePlatformAdapter')
adapter_dict: dict[str, type[abstract_platform_adapter.AbstractMessagePlatformAdapter]] = {}
for component in self.adapter_components:

View File

@@ -149,7 +149,7 @@ class EventLogger(abstract_platform_event_logger.AbstractEventLogger):
extension = mimetypes.guess_extension(mime_type)
if extension is None:
extension = '.jpg'
image_key = f'{message_session_id}-{uuid.uuid4()}{extension}'
image_key = f'bot_log_images/{message_session_id}-{uuid.uuid4()}{extension}'
await self.ap.storage_mgr.storage_provider.save(image_key, img_bytes)
image_keys.append(image_key)

View File

@@ -42,3 +42,10 @@ class StorageProvider(abc.ABC):
key: str,
):
pass
@abc.abstractmethod
async def delete_dir_recursive(
self,
dir_path: str,
):
pass

View File

@@ -2,6 +2,7 @@ from __future__ import annotations
import os
import aiofiles
import shutil
from ...core import app
@@ -22,6 +23,8 @@ class LocalStorageProvider(provider.StorageProvider):
key: str,
value: bytes,
):
if not os.path.exists(os.path.join(LOCAL_STORAGE_PATH, os.path.dirname(key))):
os.makedirs(os.path.join(LOCAL_STORAGE_PATH, os.path.dirname(key)))
async with aiofiles.open(os.path.join(LOCAL_STORAGE_PATH, f'{key}'), 'wb') as f:
await f.write(value)
@@ -43,3 +46,11 @@ class LocalStorageProvider(provider.StorageProvider):
key: str,
):
os.remove(os.path.join(LOCAL_STORAGE_PATH, f'{key}'))
async def delete_dir_recursive(
self,
dir_path: str,
):
# 直接删除整个目录
if os.path.exists(os.path.join(LOCAL_STORAGE_PATH, dir_path)):
shutil.rmtree(os.path.join(LOCAL_STORAGE_PATH, dir_path))