-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
Handle bytes file input #2233
Handle bytes file input #2233
Changes from 1 commit
8e65f5b
e3df3e2
d892e73
624be50
3f73a3c
6b6952e
1a772a3
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 |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
import logging | ||
import mimetypes | ||
import os | ||
from typing import IO, Optional, Tuple | ||
from typing import IO, Optional, Tuple, Union | ||
from uuid import uuid4 | ||
|
||
DEFAULT_MIME_TYPE = 'application/octet-stream' | ||
|
@@ -39,7 +39,7 @@ class InputFile: | |
attach (:obj:`str`): Optional. Attach id for sending multiple files. | ||
|
||
Args: | ||
obj (:obj:`File handler`): An open file descriptor. | ||
obj (:obj:`File handler` | :obj:`bytes`): An open file descriptor. | ||
filename (:obj:`str`, optional): Filename for this InputFile. | ||
attach (:obj:`bool`, optional): Whether this should be send as one file or is part of a | ||
collection of files. | ||
|
@@ -49,15 +49,18 @@ class InputFile: | |
|
||
""" | ||
|
||
def __init__(self, obj: IO, filename: str = None, attach: bool = None): | ||
def __init__(self, obj: Union[IO, bytes], filename: str = None, attach: bool = None): | ||
self.filename = None | ||
self.input_file_content = obj.read() | ||
if isinstance(obj, bytes): | ||
self.input_file_content = obj | ||
else: | ||
self.input_file_content = obj.read() | ||
self.attach = 'attached' + uuid4().hex if attach else None | ||
|
||
if filename: | ||
self.filename = filename | ||
elif hasattr(obj, 'name') and not isinstance(obj.name, int): | ||
self.filename = os.path.basename(obj.name) | ||
elif hasattr(obj, 'name') and not isinstance(obj.name, int): # type: ignore[union-attr] | ||
self.filename = os.path.basename(obj.name) # type: ignore[union-attr] | ||
|
||
image_mime_type = self.is_image(self.input_file_content) | ||
if image_mime_type: | ||
|
@@ -99,6 +102,8 @@ def is_image(stream: bytes) -> Optional[str]: | |
|
||
@staticmethod | ||
def is_file(obj: object) -> bool: | ||
if isinstance(obj, bytes): | ||
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. Well. It's not accurate that it's a file 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. we could make this check in |
||
return True | ||
return hasattr(obj, 'read') | ||
|
||
def to_dict(self) -> Optional[str]: | ||
|
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.
The doc string needs to be adapted.