mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-02-19 21:11:06 +03:00
Support extracting metadata from bytes and stream objects (#1547)
This should enable more accurate uploads of in-memory files.
This commit is contained in:
parent
02b8f1d007
commit
1ed0f75c49
|
@ -601,16 +601,52 @@ def get_message_id(message):
|
||||||
|
|
||||||
|
|
||||||
def _get_metadata(file):
|
def _get_metadata(file):
|
||||||
# `hachoir` only deals with paths to in-disk files, while
|
if not hachoir:
|
||||||
# `_get_extension` supports a few other things. The parser
|
return
|
||||||
# may also fail in any case and we don't want to crash if
|
|
||||||
|
stream = None
|
||||||
|
close_stream = True
|
||||||
|
seekable = True
|
||||||
|
|
||||||
|
# The parser may fail and we don't want to crash if
|
||||||
# the extraction process fails.
|
# the extraction process fails.
|
||||||
if hachoir and isinstance(file, str) and os.path.isfile(file):
|
try:
|
||||||
try:
|
# Note: aiofiles are intentionally left out for simplicity
|
||||||
with hachoir.parser.createParser(file) as parser:
|
if isinstance(file, str):
|
||||||
return hachoir.metadata.extractMetadata(parser)
|
stream = open(file, 'rb')
|
||||||
except Exception as e:
|
elif isinstance(file, bytes):
|
||||||
_log.warning('Failed to analyze %s: %s %s', file, e.__class__, e)
|
stream = io.BytesIO(file)
|
||||||
|
else:
|
||||||
|
stream = file
|
||||||
|
close_stream = False
|
||||||
|
if getattr(file, 'seekable', None):
|
||||||
|
seekable = file.seekable()
|
||||||
|
else:
|
||||||
|
seekable = False
|
||||||
|
|
||||||
|
if not seekable:
|
||||||
|
return None
|
||||||
|
|
||||||
|
pos = stream.tell()
|
||||||
|
filename = getattr(file, 'name', '')
|
||||||
|
|
||||||
|
parser = hachoir.parser.guess.guessParser(hachoir.stream.InputIOStream(
|
||||||
|
stream,
|
||||||
|
source='file:' + filename,
|
||||||
|
tags=[],
|
||||||
|
filename=filename
|
||||||
|
))
|
||||||
|
|
||||||
|
return hachoir.metadata.extractMetadata(parser)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
_log.warning('Failed to analyze %s: %s %s', file, e.__class__, e)
|
||||||
|
|
||||||
|
finally:
|
||||||
|
if stream and close_stream:
|
||||||
|
stream.close()
|
||||||
|
elif stream and seekable:
|
||||||
|
stream.seek(pos)
|
||||||
|
|
||||||
|
|
||||||
def get_attributes(file, *, attributes=None, mime_type=None,
|
def get_attributes(file, *, attributes=None, mime_type=None,
|
||||||
|
@ -803,15 +839,31 @@ def is_gif(file):
|
||||||
|
|
||||||
|
|
||||||
def is_audio(file):
|
def is_audio(file):
|
||||||
"""Returns `True` if the file extension looks like an audio file."""
|
"""Returns `True` if the file has an audio mime type."""
|
||||||
file = 'a' + _get_extension(file)
|
ext = _get_extension(file)
|
||||||
return (mimetypes.guess_type(file)[0] or '').startswith('audio/')
|
if not ext:
|
||||||
|
metadata = _get_metadata(file)
|
||||||
|
if metadata and metadata.has('mime_type'):
|
||||||
|
return metadata.get('mime_type').startswith('audio/')
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
file = 'a' + ext
|
||||||
|
return (mimetypes.guess_type(file)[0] or '').startswith('audio/')
|
||||||
|
|
||||||
|
|
||||||
def is_video(file):
|
def is_video(file):
|
||||||
"""Returns `True` if the file extension looks like a video file."""
|
"""Returns `True` if the file has a video mime type."""
|
||||||
file = 'a' + _get_extension(file)
|
ext = _get_extension(file)
|
||||||
return (mimetypes.guess_type(file)[0] or '').startswith('video/')
|
if not ext:
|
||||||
|
metadata = _get_metadata(file)
|
||||||
|
if metadata and metadata.has('mime_type'):
|
||||||
|
return metadata.get('mime_type').startswith('video/')
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
file = 'a' + ext
|
||||||
|
return (mimetypes.guess_type(file)[0] or '').startswith('video/')
|
||||||
|
|
||||||
|
|
||||||
def is_list_like(obj):
|
def is_list_like(obj):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user