加入图片下载逻辑。
This commit is contained in:
@@ -81,7 +81,7 @@ class MessageStorage:
|
||||
'roomid': msg.roomid,
|
||||
'sender': msg.sender,
|
||||
'content': msg.content, # 添加消息内容
|
||||
'message_id': msg.id # 添加消息ID
|
||||
'message_id': msg.id # 添加消息ID
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error(f"存档消息出错: {e}")
|
||||
@@ -90,7 +90,7 @@ class MessageStorage:
|
||||
'roomid': msg.roomid,
|
||||
'sender': msg.sender,
|
||||
'content': msg.content, # 添加消息内容
|
||||
'message_id': msg.id, # 添加消息ID
|
||||
'message_id': msg.id, # 添加消息ID
|
||||
'error': str(e)
|
||||
}
|
||||
|
||||
@@ -112,57 +112,39 @@ class MessageStorage:
|
||||
def _process_image_task(self, msg: WxMsg):
|
||||
"""实际执行图片处理的任务函数"""
|
||||
try:
|
||||
# 从msg.extra中获取本地图片路径
|
||||
local_image_path = msg.extra
|
||||
# 使用wcf下载图片,确保图片存在
|
||||
if self.wcf and msg.id:
|
||||
# 尝试使用wcf下载图片到我们的图片目录
|
||||
download_path = self.wcf.download_image(msg.id, msg.extra, self.image_dir)
|
||||
if download_path:
|
||||
logger.info(f"使用wcf下载图片成功: {msg.id} -> {download_path}")
|
||||
|
||||
if not local_image_path or not os.path.exists(local_image_path):
|
||||
# 直接使用下载后的路径更新数据库
|
||||
self.message_db.update_message_image_path(msg.id, download_path)
|
||||
|
||||
return {
|
||||
'success': True,
|
||||
'message_id': msg.id,
|
||||
'roomid': msg.roomid,
|
||||
'sender': msg.sender,
|
||||
'file_path': download_path
|
||||
}
|
||||
else:
|
||||
return {
|
||||
'success': False,
|
||||
'message_id': msg.id,
|
||||
'roomid': msg.roomid,
|
||||
'sender': msg.sender,
|
||||
'error': "图片下载失败"
|
||||
}
|
||||
else:
|
||||
return {
|
||||
'success': False,
|
||||
'message_id': msg.id,
|
||||
'roomid': msg.roomid,
|
||||
'sender': msg.sender,
|
||||
'error': "图片本地路径不存在"
|
||||
'error': "WCF实例不存在或消息ID无效"
|
||||
}
|
||||
|
||||
# 生成目标文件名
|
||||
filename = self._generate_image_filename(local_image_path)
|
||||
|
||||
# 构建完整的目标文件路径
|
||||
target_path = os.path.join(self.image_dir, filename)
|
||||
# 使用绝对路径而不是相对路径
|
||||
relative_path = target_path
|
||||
|
||||
# 检查目标文件是否已存在
|
||||
if os.path.exists(target_path):
|
||||
logger.info(f"图片已存在,跳过复制: {msg.id} -> {target_path}")
|
||||
# 更新数据库中的图片路径
|
||||
self.message_db.update_message_image_path(msg.id, relative_path)
|
||||
return {
|
||||
'success': True,
|
||||
'message_id': msg.id,
|
||||
'roomid': msg.roomid,
|
||||
'sender': msg.sender,
|
||||
'file_path': target_path,
|
||||
'original_path': local_image_path,
|
||||
'skipped': True
|
||||
}
|
||||
|
||||
# 复制图片到静态目录
|
||||
shutil.copy2(local_image_path, target_path)
|
||||
|
||||
logger.info(f"图片处理成功: {msg.id} -> {target_path}")
|
||||
|
||||
# 更新数据库中的图片路径
|
||||
self.message_db.update_message_image_path(msg.id, relative_path)
|
||||
|
||||
return {
|
||||
'success': True,
|
||||
'message_id': msg.id,
|
||||
'roomid': msg.roomid,
|
||||
'sender': msg.sender,
|
||||
'file_path': target_path,
|
||||
'original_path': local_image_path
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error(f"图片处理出错: {msg.id}, 错误: {e}")
|
||||
return {
|
||||
@@ -173,36 +155,6 @@ class MessageStorage:
|
||||
'error': str(e)
|
||||
}
|
||||
|
||||
def _generate_image_filename(self, original_path):
|
||||
"""
|
||||
使用图片内容的哈希值生成唯一的文件名
|
||||
|
||||
Args:
|
||||
original_path: 原始图片路径
|
||||
|
||||
Returns:
|
||||
生成的文件名
|
||||
"""
|
||||
try:
|
||||
# 读取图片内容
|
||||
with open(original_path, 'rb') as f:
|
||||
image_content = f.read()
|
||||
|
||||
# 使用图片内容的哈希值生成唯一文件名
|
||||
hash_obj = hashlib.md5(image_content)
|
||||
file_ext = os.path.splitext(original_path)[-1] if '.' in original_path else '.jpg'
|
||||
if not file_ext or len(file_ext) > 5:
|
||||
file_ext = '.jpg' # 默认使用jpg扩展名
|
||||
return f"{hash_obj.hexdigest()}{file_ext}"
|
||||
except Exception as e:
|
||||
# 如果读取图片内容失败,回退到使用路径生成哈希值
|
||||
logger.warning(f"读取图片内容失败,使用路径生成哈希值: {e}")
|
||||
hash_obj = hashlib.md5(original_path.encode())
|
||||
file_ext = os.path.splitext(original_path)[-1] if '.' in original_path else '.jpg'
|
||||
if not file_ext or len(file_ext) > 5:
|
||||
file_ext = '.jpg' # 默认使用jpg扩展名
|
||||
return f"{hash_obj.hexdigest()}{file_ext}"
|
||||
|
||||
def _process_image_callback(self, future):
|
||||
"""处理异步图片处理任务完成后的回调"""
|
||||
try:
|
||||
@@ -239,18 +191,18 @@ class MessageStorage:
|
||||
# 从待处理列表中移除已完成的任务
|
||||
for task in completed_tasks:
|
||||
self.pending_tasks.remove(task)
|
||||
|
||||
|
||||
# 如果待处理任务过多,记录警告日志
|
||||
if len(self.pending_tasks) > 100:
|
||||
logger.warning(f"待处理的存档任务数量过多: {len(self.pending_tasks)}")
|
||||
|
||||
|
||||
# 只有当图片任务数量超过阈值时才进行清理
|
||||
if len(self.image_tasks) > 10:
|
||||
# 清理已完成的图片处理任务
|
||||
completed_image_tasks = [task for task in self.image_tasks if task.done()]
|
||||
for task in completed_image_tasks:
|
||||
self.image_tasks.remove(task)
|
||||
|
||||
|
||||
# 如果待处理任务过多,记录警告日志
|
||||
if len(self.image_tasks) > 50:
|
||||
logger.warning(f"待处理的图片处理任务数量过多: {len(self.image_tasks)}")
|
||||
|
||||
Reference in New Issue
Block a user