mirror of
				https://github.com/LonamiWebs/Telethon.git
				synced 2025-10-31 16:07:44 +03:00 
			
		
		
		
	Support .name attr in utils.is_type/client.upload_file
This means open()'ed files will have their name used properly, and that you can set b.name = 'somename.jpg' to io.BytesIO() objects to have them uploaded as images.
This commit is contained in:
		
							parent
							
								
									d8af64e3b9
								
							
						
					
					
						commit
						3973755333
					
				|  | @ -270,6 +270,9 @@ class UploadMethods(MessageParseMethods, UserMethods): | ||||||
|         if isinstance(file, (types.InputFile, types.InputFileBig)): |         if isinstance(file, (types.InputFile, types.InputFileBig)): | ||||||
|             return file  # Already uploaded |             return file  # Already uploaded | ||||||
| 
 | 
 | ||||||
|  |         if not file_name and getattr(file, 'name', None): | ||||||
|  |             file_name = file.name | ||||||
|  | 
 | ||||||
|         if isinstance(file, str): |         if isinstance(file, str): | ||||||
|             file_size = os.path.getsize(file) |             file_size = os.path.getsize(file) | ||||||
|         elif isinstance(file, bytes): |         elif isinstance(file, bytes): | ||||||
|  |  | ||||||
|  | @ -490,26 +490,36 @@ def get_input_location(location): | ||||||
|     _raise_cast_fail(location, 'InputFileLocation') |     _raise_cast_fail(location, 'InputFileLocation') | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | def _get_extension(file): | ||||||
|  |     """ | ||||||
|  |     Gets the extension for the given file, which can be either a | ||||||
|  |     str or an ``open()``'ed file (which has a ``.name`` attribute). | ||||||
|  |     """ | ||||||
|  |     if isinstance(file, str): | ||||||
|  |         return os.path.splitext(file)[-1] | ||||||
|  |     elif getattr(file, 'name', None): | ||||||
|  |         return _get_extension(file.name) | ||||||
|  |     else: | ||||||
|  |         return '' | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| def is_image(file): | def is_image(file): | ||||||
|     """ |     """ | ||||||
|     Returns ``True`` if the file extension looks like an image file to Telegram. |     Returns ``True`` if the file extension looks like an image file to Telegram. | ||||||
|     """ |     """ | ||||||
|     if not isinstance(file, str): |     return re.match(r'\.(png|jpe?g)', _get_extension(file), re.IGNORECASE) | ||||||
|         return False |  | ||||||
|     _, ext = os.path.splitext(file) |  | ||||||
|     return re.match(r'\.(png|jpe?g)', ext, re.IGNORECASE) |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def is_audio(file): | def is_audio(file): | ||||||
|     """Returns ``True`` if the file extension looks like an audio file.""" |     """Returns ``True`` if the file extension looks like an audio file.""" | ||||||
|     return (isinstance(file, str) and |     file = 'a' + _get_extension(file) | ||||||
|             (mimetypes.guess_type(file)[0] or '').startswith('audio/')) |     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 extension looks like a video file.""" | ||||||
|     return (isinstance(file, str) and |     file = 'a' + _get_extension(file) | ||||||
|             (mimetypes.guess_type(file)[0] or '').startswith('video/')) |     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