mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-30 05:13:45 +03:00
Support download_file with None path to return bytes
This commit is contained in:
parent
b71511cd63
commit
f16289cf93
|
@ -2174,7 +2174,7 @@ class TelegramClient(TelegramBareClient):
|
||||||
|
|
||||||
def download_file(self,
|
def download_file(self,
|
||||||
input_location,
|
input_location,
|
||||||
file,
|
file=None,
|
||||||
part_size_kb=None,
|
part_size_kb=None,
|
||||||
file_size=None,
|
file_size=None,
|
||||||
progress_callback=None):
|
progress_callback=None):
|
||||||
|
@ -2185,10 +2185,13 @@ class TelegramClient(TelegramBareClient):
|
||||||
input_location (:tl:`InputFileLocation`):
|
input_location (:tl:`InputFileLocation`):
|
||||||
The file location from which the file will be downloaded.
|
The file location from which the file will be downloaded.
|
||||||
|
|
||||||
file (`str` | `file`):
|
file (`str` | `file`, optional):
|
||||||
The output file path, directory, or stream-like object.
|
The output file path, directory, or stream-like object.
|
||||||
If the path exists and is a file, it will be overwritten.
|
If the path exists and is a file, it will be overwritten.
|
||||||
|
|
||||||
|
If the file path is ``None``, then the result will be
|
||||||
|
saved in memory and returned as `bytes`.
|
||||||
|
|
||||||
part_size_kb (`int`, optional):
|
part_size_kb (`int`, optional):
|
||||||
Chunk size when downloading files. The larger, the less
|
Chunk size when downloading files. The larger, the less
|
||||||
requests will be made (up to 512KB maximum).
|
requests will be made (up to 512KB maximum).
|
||||||
|
@ -2219,7 +2222,10 @@ class TelegramClient(TelegramBareClient):
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
'The part size must be evenly divisible by 4096.')
|
'The part size must be evenly divisible by 4096.')
|
||||||
|
|
||||||
if isinstance(file, str):
|
in_memory = file is None
|
||||||
|
if in_memory:
|
||||||
|
f = io.BytesIO()
|
||||||
|
elif isinstance(file, str):
|
||||||
# Ensure that we'll be able to download the media
|
# Ensure that we'll be able to download the media
|
||||||
helpers.ensure_parent_dir_exists(file)
|
helpers.ensure_parent_dir_exists(file)
|
||||||
f = open(file, 'wb')
|
f = open(file, 'wb')
|
||||||
|
@ -2261,6 +2267,10 @@ class TelegramClient(TelegramBareClient):
|
||||||
# So there is nothing left to download and write
|
# So there is nothing left to download and write
|
||||||
if not result.bytes:
|
if not result.bytes:
|
||||||
# Return some extra information, unless it's a CDN file
|
# Return some extra information, unless it's a CDN file
|
||||||
|
if in_memory:
|
||||||
|
f.flush()
|
||||||
|
return f.getvalue()
|
||||||
|
else:
|
||||||
return getattr(result, 'type', '')
|
return getattr(result, 'type', '')
|
||||||
|
|
||||||
f.write(result.bytes)
|
f.write(result.bytes)
|
||||||
|
@ -2276,7 +2286,7 @@ class TelegramClient(TelegramBareClient):
|
||||||
cdn_decrypter.client.disconnect()
|
cdn_decrypter.client.disconnect()
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
if isinstance(file, str):
|
if isinstance(file, str) or in_memory:
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
# endregion
|
# endregion
|
||||||
|
|
Loading…
Reference in New Issue
Block a user