fix: handle exceptions in get_file_base64 method

v3.2
wxg0103 2025-09-19 11:12:57 +08:00
parent 0693ae99ca
commit 5bea2b2566
1 changed files with 16 additions and 11 deletions

View File

@ -71,17 +71,22 @@ class BaseImageToVideoNode(IImageToVideoNode):
'history_message': history_message, 'question': question}, {}) 'history_message': history_message, 'question': question}, {})
def get_file_base64(self, image_url): def get_file_base64(self, image_url):
if isinstance(image_url, list): try :
image_url = image_url[0].get('file_id') if isinstance(image_url, list):
if isinstance(image_url, str) and not image_url.startswith('http'): image_url = image_url[0].get('file_id')
file = QuerySet(File).filter(id=image_url).first() if isinstance(image_url, str) and not image_url.startswith('http'):
file_bytes = file.get_bytes() file = QuerySet(File).filter(id=image_url).first()
# 如果我不知道content_type 可以用 magic 库去检测 file_bytes = file.get_bytes()
file_type = file.file_name.split(".")[-1].lower() # 如果我不知道content_type 可以用 magic 库去检测
content_type = mime_types.get(file_type, 'application/octet-stream') file_type = file.file_name.split(".")[-1].lower()
encoded_bytes = base64.b64encode(file_bytes) content_type = mime_types.get(file_type, 'application/octet-stream')
return f'data:{content_type};base64,{encoded_bytes.decode()}' encoded_bytes = base64.b64encode(file_bytes)
return image_url return f'data:{content_type};base64,{encoded_bytes.decode()}'
return image_url
except Exception as e:
raise ValueError(
gettext("Failed to obtain the image"))
def generate_history_ai_message(self, chat_record): def generate_history_ai_message(self, chat_record):
for val in chat_record.details.values(): for val in chat_record.details.values():