TelegramIndex-Fork/app/views/thumbnail_view.py

81 lines
2.6 KiB
Python
Raw Normal View History

2020-12-12 19:46:44 +05:30
import logging
from PIL import Image
import random
import io
from aiohttp import web
from telethon.tl import types
log = logging.getLogger(__name__)
class ThumbnailView:
async def thumbnail_get(self, req):
file_id = int(req.match_info["id"])
2021-05-22 19:30:08 +05:30
alias_id = req.match_info["chat"]
chat = self.chat_ids[alias_id]
chat_id = chat["chat_id"]
2020-12-12 19:46:44 +05:30
try:
message = await self.client.get_messages(entity=chat_id, ids=file_id)
2021-05-22 19:30:08 +05:30
except Exception:
2020-12-12 19:46:44 +05:30
log.debug(f"Error in getting message {file_id} in {chat_id}", exc_info=True)
message = None
if not message or not message.file:
log.debug(f"no result for {file_id} in {chat_id}")
2021-05-22 19:30:08 +05:30
return web.Response(
status=410,
text="410: Gone. Access to the target resource is no longer available!",
)
2020-12-12 19:46:44 +05:30
if message.document:
media = message.document
thumbnails = media.thumbs
location = types.InputDocumentFileLocation
else:
media = message.photo
thumbnails = media.sizes
location = types.InputPhotoFileLocation
if not thumbnails:
color = tuple([random.randint(0, 255) for i in range(3)])
im = Image.new("RGB", (100, 100), color)
temp = io.BytesIO()
im.save(temp, "PNG")
body = temp.getvalue()
else:
2021-05-22 19:30:08 +05:30
thumb_pos = int(len(thumbnails) / 2)
2021-06-11 23:40:08 +05:30
try:
thumbnail = self.client._get_thumb(thumbnails, thumb_pos)
except Exception as e:
logging.debug(e)
thumbnail = None
2020-12-12 19:46:44 +05:30
if not thumbnail or isinstance(thumbnail, types.PhotoSizeEmpty):
2021-05-22 19:30:08 +05:30
return web.Response(
status=410,
text="410: Gone. Access to the target resource is no longer available!",
)
2020-12-12 19:46:44 +05:30
if isinstance(thumbnail, (types.PhotoCachedSize, types.PhotoStrippedSize)):
body = self.client._download_cached_photo_size(thumbnail, bytes)
else:
actual_file = location(
id=media.id,
access_hash=media.access_hash,
file_reference=media.file_reference,
2021-05-22 19:30:08 +05:30
thumb_size=thumbnail.type,
2020-12-12 19:46:44 +05:30
)
body = self.client.iter_download(actual_file)
2021-05-22 19:30:08 +05:30
return web.Response(
2020-12-12 19:46:44 +05:30
status=200,
body=body,
headers={
"Content-Type": "image/jpeg",
2021-05-22 19:30:08 +05:30
"Content-Disposition": 'inline; filename="thumbnail.jpg"',
},
2020-12-12 19:46:44 +05:30
)