-
-
Notifications
You must be signed in to change notification settings - Fork 2k
fix: 修复qqofficial上传图片超时问题 - 改用分片上传 #7176
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
Open
bunwen
wants to merge
3
commits into
AstrBotDevs:master
Choose a base branch
from
bunwen:fix/qqofficial-image-upload
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Empty file.
891 changes: 891 additions & 0 deletions
891
astrbot/core/platform/sources/qqofficial/chunked_upload.py
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| """ | ||
| 文件工具模块 | ||
| 参照 openclaw-qqbot 的 file-utils.ts 实现 | ||
| """ | ||
|
|
||
| import os | ||
| from typing import Optional | ||
|
|
||
|
|
||
| # ============ 文件类型与大小限制 ============ | ||
|
|
||
| class MediaFileType: | ||
| IMAGE = 1 | ||
| VIDEO = 2 | ||
| VOICE = 3 | ||
| FILE = 4 | ||
|
|
||
|
|
||
| # QQ Bot API 上传大小限制(字节)- 与 openclaw-qqbot 一致 | ||
| MAX_UPLOAD_SIZES = { | ||
| MediaFileType.IMAGE: 30 * 1024 * 1024, # 30MB | ||
| MediaFileType.VIDEO: 100 * 1024 * 1024, # 100MB | ||
| MediaFileType.VOICE: 20 * 1024 * 1024, # 20MB | ||
| MediaFileType.FILE: 100 * 1024 * 1024, # 100MB | ||
| } | ||
|
|
||
| FILE_TYPE_NAMES = { | ||
| MediaFileType.IMAGE: "图片", | ||
| MediaFileType.VIDEO: "视频", | ||
| MediaFileType.VOICE: "语音", | ||
| MediaFileType.FILE: "文件", | ||
| } | ||
|
|
||
|
|
||
| def format_file_size(size_bytes: int) -> str: | ||
| """格式化文件大小""" | ||
| if size_bytes < 1024: | ||
| return f"{size_bytes}B" | ||
| elif size_bytes < 1024 * 1024: | ||
| return f"{size_bytes / 1024:.1f}KB" | ||
| elif size_bytes < 1024 * 1024 * 1024: | ||
| return f"{size_bytes / (1024 * 1024):.1f}MB" | ||
| else: | ||
| return f"{size_bytes / (1024 * 1024 * 1024):.2f}GB" | ||
|
|
||
|
|
||
| def get_max_upload_size(file_type: int) -> int: | ||
| """获取文件类型对应的最大上传大小""" | ||
| return MAX_UPLOAD_SIZES.get(file_type, 100 * 1024 * 1024) | ||
|
|
||
|
|
||
| def get_file_type_name(file_type: int) -> str: | ||
| """获取文件类型名称""" | ||
| return FILE_TYPE_NAMES.get(file_type, "文件") | ||
|
|
||
|
|
||
| async def file_exists_async(file_path: str) -> bool: | ||
| """异步检查文件是否存在""" | ||
| return os.path.exists(file_path) | ||
|
|
||
|
|
||
| async def get_file_size_async(file_path: str) -> int: | ||
| """异步获取文件大小""" | ||
| try: | ||
| return os.path.getsize(file_path) | ||
| except OSError: | ||
| return 0 | ||
bunwen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def is_image_file(file_path: str, mime_type: Optional[str] = None) -> bool: | ||
| """判断是否为图片文件""" | ||
| if mime_type and mime_type.startswith("image/"): | ||
| return True | ||
|
|
||
|
|
||
| ext = os.path.splitext(file_path)[1].lower() | ||
| return ext in {'.jpg', '.jpeg', '.png', '.gif', '.webp', '.bmp'} | ||
|
|
||
|
|
||
| def is_video_file(file_path: str, mime_type: Optional[str] = None) -> bool: | ||
| """判断是否为视频文件""" | ||
| if mime_type and mime_type.startswith("video/"): | ||
| return True | ||
|
|
||
|
|
||
| ext = os.path.splitext(file_path)[1].lower() | ||
| return ext in {'.mp4', '.mov', '.avi', '.mkv', '.webm', '.flv', '.wmv'} | ||
|
|
||
|
|
||
| def is_audio_file(file_path: str, mime_type: Optional[str] = None) -> bool: | ||
| """判断是否为音频文件""" | ||
| if mime_type and mime_type.startswith("audio/"): | ||
| return True | ||
|
|
||
|
|
||
| ext = os.path.splitext(file_path)[1].lower() | ||
| return ext in {'.mp3', '.wav', '.ogg', '.m4a', '.amr', '.silk', '.aac', '.flac'} | ||
|
|
||
|
|
||
| def get_file_extension(file_path: str) -> str: | ||
| """ | ||
| 获取文件扩展名(去除查询参数和 hash) | ||
|
|
||
| Args: | ||
| file_path: 文件路径或 URL | ||
|
|
||
| Returns: | ||
| 文件扩展名(小写,包含点号) | ||
| """ | ||
| # 去除查询参数和 hash | ||
| clean_path = file_path.split("?")[0].split("#")[0] | ||
| ext = os.path.splitext(clean_path)[1].lower() | ||
| return ext | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.