-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(core): fix Record.convert_to_file_path saving audio as .jpg #6868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -63,6 +63,16 @@ def save_temp_img(img: Image.Image | bytes) -> str: | |
| return p | ||
|
|
||
|
|
||
| def save_temp_audio(audio_data: bytes) -> str: | ||
| """Save audio data to a temporary file with a proper extension.""" | ||
| temp_dir = get_astrbot_temp_path() | ||
| timestamp = f"{int(time.time())}_{uuid.uuid4().hex[:8]}" | ||
| p = os.path.join(temp_dir, f"recordseg_{timestamp}.audio") | ||
| with open(p, "wb") as f: | ||
| f.write(audio_data) | ||
| return p | ||
|
|
||
|
|
||
| async def download_image_by_url( | ||
| url: str, | ||
| post: bool = False, | ||
|
|
@@ -123,6 +133,34 @@ async def download_image_by_url( | |
| raise e | ||
|
|
||
|
|
||
| async def download_audio_by_url(url: str) -> str: | ||
| """Download audio from URL, preserving extension. Returns local file path.""" | ||
| try: | ||
| ssl_context = ssl.create_default_context(cafile=certifi.where()) | ||
| connector = aiohttp.TCPConnector(ssl=ssl_context) | ||
| async with aiohttp.ClientSession( | ||
| trust_env=True, | ||
| connector=connector, | ||
| ) as session: | ||
| async with session.get(url) as resp: | ||
| data = await resp.read() | ||
| return save_temp_audio(data) | ||
| except (aiohttp.ClientConnectorSSLError, aiohttp.ClientConnectorCertificateError): | ||
| logger.warning( | ||
| f"SSL certificate verification failed for {url}. " | ||
| "Disabling SSL verification (CERT_NONE) as a fallback." | ||
| ) | ||
| ssl_context = ssl.create_default_context() | ||
| ssl_context.check_hostname = False | ||
| ssl_context.verify_mode = ssl.CERT_NONE | ||
| async with aiohttp.ClientSession() as session: | ||
| async with session.get(url, ssl=ssl_context) as resp: | ||
| data = await resp.read() | ||
| return save_temp_audio(data) | ||
| except Exception as e: | ||
| raise e | ||
|
Comment on lines
+136
to
+161
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This new function To improve maintainability and correctness, you could refactor this function to reduce duplication and update the docstring. Here's a suggested refactoring that uses a nested helper function to avoid repeating the download logic: async def download_audio_by_url(url: str) -> str:
"""Download audio from URL. Returns local file path."""
async def _download_and_save(session, **kwargs):
async with session.get(url, **kwargs) as resp:
resp.raise_for_status()
data = await resp.read()
return save_temp_audio(data)
try:
ssl_context = ssl.create_default_context(cafile=certifi.where())
connector = aiohttp.TCPConnector(ssl=ssl_context)
async with aiohttp.ClientSession(
trust_env=True,
connector=connector,
) as session:
return await _download_and_save(session)
except (aiohttp.ClientConnectorSSLError, aiohttp.ClientConnectorCertificateError):
logger.warning(
f"SSL certificate verification failed for {url}. "
"Disabling SSL verification (CERT_NONE) as a fallback."
)
ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
async with aiohttp.ClientSession() as session:
return await _download_and_save(session, ssl=ssl_context) |
||
|
|
||
|
|
||
| async def download_file(url: str, path: str, show_progress: bool = False) -> None: | ||
| """从指定 url 下载文件到指定路径 path""" | ||
| try: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue: Function docstring claims a proper extension is used, but the implementation always writes
.audio.Please either (a) change the implementation to accept/use an extension or filename hint so it truly uses a “proper” extension, or (b) update the docstring to state that the file is always saved with a
.audioextension, so callers don’t rely on the extension to infer the actual audio type.