def _parse_telegram_document_meta( evt: Message, file: DBTelegramFile, attrs: DocAttrs, thumb_size: TypePhotoSize) -> Tuple[ImageInfo, str]: document = evt.media.document name = evt.message or attrs.name if attrs.is_sticker: alt = attrs.sticker_alt if len(alt) > 0: try: name = f"{alt} ({unicodedata.name(alt[0]).lower()})" except ValueError: name = alt generic_types = ("text/plain", "application/octet-stream") if file.mime_type in generic_types and document.mime_type not in generic_types: mime_type = document.mime_type or file.mime_type elif file.mime_type == 'application/ogg': mime_type = 'audio/ogg' else: mime_type = file.mime_type or document.mime_type info = ImageInfo(size=file.size, mimetype=mime_type) if attrs.mime_type and not file.was_converted: file.mime_type = attrs.mime_type or file.mime_type if file.width and file.height: info.width, info.height = file.width, file.height elif attrs.width and attrs.height: info.width, info.height = attrs.width, attrs.height if file.thumbnail: if file.thumbnail.decryption_info: info.thumbnail_file = file.thumbnail.decryption_info else: info.thumbnail_url = file.thumbnail.mxc info.thumbnail_info = ThumbnailInfo( mimetype=file.thumbnail.mime_type, height=file.thumbnail.height or thumb_size.h, width=file.thumbnail.width or thumb_size.w, size=file.thumbnail.size) else: # This is a hack for bad clients like Riot iOS that require a thumbnail if file.decryption_info: info.thumbnail_file = file.decryption_info else: info.thumbnail_url = file.mxc info.thumbnail_info = ImageInfo.deserialize(info.serialize()) return info, name
async def _reupload(self, url: URL, title: Optional[str] = None, dimensions: Optional[Tuple[int, int]] = None, external_url: Optional[str] = None, thumbnail_url: Optional[URL] = None, thumbnail_dimensions: Optional[Tuple[int, int]] = None, headers: Optional[Dict[str, str]] = None) -> Image: info = ImageInfo() if "user_agent" in self.config: headers["User-Agent"] = self.config["user_agent"] async with self.bot.http.get(url, headers=headers) as resp: data = await resp.read() info.size = len(data) info.mimetype = resp.headers["Content-Type"] if not info.mimetype: info.mimetype = magic.from_buffer(data, mime=True) if not title: title = self._get_filename(url, resp, info.mimetype) if dimensions: info.width, info.height = dimensions elif Pillow: img = Pillow.open(BytesIO(data)) info.width, info.height = img.size mxc = await self.bot.client.upload_media(data, info.mimetype) if thumbnail_url: thumbnail = await self._reupload(thumbnail_url, title=title, headers=headers, dimensions=thumbnail_dimensions) info.thumbnail_url = thumbnail.url info.thumbnail_info = thumbnail.info return Image(url=mxc, info=info, title=title, external_url=external_url)