-
Notifications
You must be signed in to change notification settings - Fork 44
feat: возможность для пользователя передавать тип загружаемого файла как строку #119
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: main
Are you sure you want to change the base?
Changes from all commits
413abae
9e5e1d9
b2a4be8
40e1747
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 | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -6,6 +6,53 @@ | |||||||||||||
|
|
||||||||||||||
| from ..enums.upload_type import UploadType | ||||||||||||||
|
|
||||||||||||||
| READ_FILE_CHUNK_SIZE = 4096 | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def detect_file_type(data: bytes) -> UploadType: | ||||||||||||||
| """ | ||||||||||||||
| Определяет тип файла на основе его содержимого (MIME-типа). | ||||||||||||||
|
|
||||||||||||||
| Args: | ||||||||||||||
| data (bytes): Буфер с содержимым файла. | ||||||||||||||
| Returns: | ||||||||||||||
| UploadType: Определенный тип файла. Если MIME-тип не удалось | ||||||||||||||
| определить или при определении произошла ошибка, | ||||||||||||||
| возвращается ``UploadType.FILE``. | ||||||||||||||
| """ | ||||||||||||||
| try: | ||||||||||||||
| matches = puremagic.magic_string(data) | ||||||||||||||
| if matches: | ||||||||||||||
| mime_type = matches[0].mime_type | ||||||||||||||
|
||||||||||||||
| mime_type = matches[0].mime_type | |
| match = matches[0] | |
| if isinstance(match, (tuple, list)) and len(match) > 1: | |
| mime_type = match[1] | |
| else: | |
| mime_type = getattr(match, "mime_type", None) |
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.
Здесь matches - это list из namedtuple. Луче обратиться по атрибуту. Исправил поведение в maxapi/connection/base.py, там обращение в принципе было не к mime_type по индексу элемента в кортеже.
Copilot
AI
Apr 19, 2026
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.
Добавлена новая функциональность: принимать type как строку и выбрасывать ValueError с перечислением допустимых значений. В тестах сейчас нет проверок этого сценария (особенно: строка валидная → корректный UploadType, строка невалидная → ожидаемое сообщение ошибки). Стоит добавить/расширить тесты для InputMedia и InputMediaBuffer, чтобы зафиксировать контракт.
Copilot
AI
Apr 26, 2026
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.
InputMediaBuffer.type is always set to an UploadType instance after initialization, but the docstring/types currently describe the attribute as UploadType | str | None. Consider documenting/annotating the attribute as UploadType (while keeping the constructor parameter as UploadType | str | None) so consumers and type-checkers don't assume str/None are possible post-init.
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.
В
detect_file_typeдокстринг описывает аргументы, но не документирует возвращаемое значение (и возможные исключения). В этом репозитории обычно указываются секцииReturns:(и частоRaises:) — стоит добавить их, чтобы поведение функции было однозначно описано.