mirror of
				https://github.com/LonamiWebs/Telethon.git
				synced 2025-10-30 23:47:33 +03:00 
			
		
		
		
	Support async fd on download/upload (#1334)
This commit is contained in:
		
							parent
							
								
									5d7e9f3879
								
							
						
					
					
						commit
						bdb74ac235
					
				|  | @ -434,7 +434,10 @@ class DownloadMethods: | ||||||
|         try: |         try: | ||||||
|             async for chunk in self.iter_download( |             async for chunk in self.iter_download( | ||||||
|                     input_location, request_size=part_size, dc_id=dc_id): |                     input_location, request_size=part_size, dc_id=dc_id): | ||||||
|                 f.write(chunk) |                 r = f.write(chunk) | ||||||
|  |                 if inspect.isawaitable(r): | ||||||
|  |                     await r | ||||||
|  | 
 | ||||||
|                 if progress_callback: |                 if progress_callback: | ||||||
|                     r = progress_callback(f.tell(), file_size) |                     r = progress_callback(f.tell(), file_size) | ||||||
|                     if inspect.isawaitable(r): |                     if inspect.isawaitable(r): | ||||||
|  |  | ||||||
|  | @ -484,6 +484,9 @@ class UploadMethods: | ||||||
|         elif isinstance(file, bytes): |         elif isinstance(file, bytes): | ||||||
|             file_size = len(file) |             file_size = len(file) | ||||||
|         else: |         else: | ||||||
|  |             # `aiofiles` shouldn't base `IOBase` because they change the | ||||||
|  |             # methods' definition. `seekable` would be `async` but since | ||||||
|  |             # we won't get to check that, there's no need to maybe-await. | ||||||
|             if isinstance(file, io.IOBase) and file.seekable(): |             if isinstance(file, io.IOBase) and file.seekable(): | ||||||
|                 pos = file.tell() |                 pos = file.tell() | ||||||
|             else: |             else: | ||||||
|  | @ -491,6 +494,9 @@ class UploadMethods: | ||||||
| 
 | 
 | ||||||
|             # TODO Don't load the entire file in memory always |             # TODO Don't load the entire file in memory always | ||||||
|             data = file.read() |             data = file.read() | ||||||
|  |             if inspect.isawaitable(data): | ||||||
|  |                 data = await data | ||||||
|  | 
 | ||||||
|             if pos is not None: |             if pos is not None: | ||||||
|                 file.seek(pos) |                 file.seek(pos) | ||||||
| 
 | 
 | ||||||
|  | @ -591,7 +597,9 @@ class UploadMethods: | ||||||
|         if as_image is None: |         if as_image is None: | ||||||
|             as_image = utils.is_image(file) and not force_document |             as_image = utils.is_image(file) and not force_document | ||||||
| 
 | 
 | ||||||
|         if not isinstance(file, (str, bytes, io.IOBase)): |         # `aiofiles` do not base `io.IOBase` but do have `read`, so we | ||||||
|  |         # just check for the read attribute to see if it's file-like. | ||||||
|  |         if not isinstance(file, (str, bytes)) and not hasattr(file, 'read'): | ||||||
|             # The user may pass a Message containing media (or the media, |             # The user may pass a Message containing media (or the media, | ||||||
|             # or anything similar) that should be treated as a file. Try |             # or anything similar) that should be treated as a file. Try | ||||||
|             # getting the input media for whatever they passed and send it. |             # getting the input media for whatever they passed and send it. | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user