Skip to content

Commit

Permalink
fix: #6718
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuanbo Li committed Jul 29, 2024
1 parent c6c9b44 commit 718a138
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
33 changes: 33 additions & 0 deletions api/core/file/message_file_parser.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from collections.abc import Mapping, Sequence
from typing import Any, Union
import re

import requests
from urllib.parse import urlparse, parse_qs

from core.app.app_config.entities import FileExtraConfig
from core.file.file_obj import FileBelongsTo, FileTransferMethod, FileType, FileVar
Expand Down Expand Up @@ -186,6 +188,37 @@ def _check_image_remote_url(self, url):
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
}

def is_s3_presigned_url(url):
try:
parsed_url = urlparse(url)

if 'amazonaws.com' not in parsed_url.netloc:
return False

query_params = parse_qs(parsed_url.query)

required_params = ['X-Amz-Signature', 'X-Amz-Expires']
for param in required_params:
if param not in query_params:
return False

if not query_params['X-Amz-Expires'][0].isdigit():
return False

signature = query_params['X-Amz-Signature'][0]
if not re.match(r'^[A-Za-z0-9+/]+={0,2}$', signature):
return False

return True

except Exception:
return False

if is_s3_presigned_url(url):
response = requests.get(url, headers=headers, allow_redirects=True)
if response.status_code in {200, 304}:
return True, ""

response = requests.head(url, headers=headers, allow_redirects=True)
if response.status_code in {200, 304}:
return True, ""
Expand Down
8 changes: 6 additions & 2 deletions api/core/model_runtime/model_providers/bedrock/llm/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,12 @@ def _convert_prompt_message_to_dict(self, message: PromptMessage) -> dict:
if not message_content.data.startswith("data:"):
# fetch image data from url
try:
image_content = requests.get(message_content.data).content
mime_type, _ = mimetypes.guess_type(message_content.data)
url = message_content.data
image_content = requests.get(url).content
if '?' in url:
url = url.split('?')[0]
mime_type, _ = mimetypes.guess_type(url)
base64_data = base64.b64encode(image_content).decode('utf-8')
except Exception as ex:
raise ValueError(f"Failed to fetch image data from url {message_content.data}, {ex}")
else:
Expand Down

0 comments on commit 718a138

Please sign in to comment.