2019-07-10 11:46:21 +10:00
|
|
|
# Copyright 2019 miruka
|
|
|
|
# This file is part of harmonyqml, licensed under LGPLv3.
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
import random
|
|
|
|
import re
|
|
|
|
from io import BytesIO
|
|
|
|
from pathlib import Path
|
|
|
|
from typing import Tuple
|
|
|
|
from urllib.parse import urlparse
|
|
|
|
|
2019-07-19 10:44:17 +10:00
|
|
|
import aiofiles
|
2019-07-10 11:46:21 +10:00
|
|
|
from dataclasses import dataclass, field
|
|
|
|
from PIL import Image as PILImage
|
|
|
|
|
|
|
|
import nio
|
|
|
|
import pyotherside
|
|
|
|
from nio.api import ResizingMethod
|
|
|
|
|
|
|
|
Size = Tuple[int, int]
|
|
|
|
ImageData = Tuple[bytearray, Size, int] # last int: pyotherside format enum
|
|
|
|
|
2019-07-19 10:49:47 +10:00
|
|
|
CONCURRENT_DOWNLOADS_LIMIT = asyncio.BoundedSemaphore(8)
|
|
|
|
|
2019-07-10 11:46:21 +10:00
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Thumbnail:
|
2019-07-14 10:15:20 +10:00
|
|
|
# pylint: disable=no-member
|
|
|
|
provider: "ImageProvider" = field()
|
|
|
|
mxc: str = field()
|
|
|
|
width: int = field()
|
|
|
|
height: int = field()
|
2019-07-10 11:46:21 +10:00
|
|
|
|
|
|
|
def __post_init__(self) -> None:
|
2019-07-14 10:15:20 +10:00
|
|
|
self.mxc = re.sub(r"#auto$", "", self.mxc)
|
2019-07-10 11:46:21 +10:00
|
|
|
|
2019-07-14 10:15:20 +10:00
|
|
|
if not re.match(r"^mxc://.+/.+", self.mxc):
|
|
|
|
raise ValueError(f"Invalid mxc URI: {self.mxc}")
|
2019-07-10 11:46:21 +10:00
|
|
|
|
|
|
|
|
|
|
|
@property
|
2019-07-14 10:15:20 +10:00
|
|
|
def server_size(self) -> Tuple[int, int]:
|
|
|
|
# https://matrix.org/docs/spec/client_server/latest#thumbnails
|
|
|
|
|
|
|
|
if self.width > 640 or self.height > 480:
|
|
|
|
return (800, 600)
|
|
|
|
|
|
|
|
if self.width > 320 or self.height > 240:
|
|
|
|
return (640, 480)
|
|
|
|
|
|
|
|
if self.width > 96 or self.height > 96:
|
|
|
|
return (320, 240)
|
|
|
|
|
|
|
|
if self.width > 32 or self.height > 32:
|
|
|
|
return (96, 96)
|
|
|
|
|
|
|
|
return (32, 32)
|
2019-07-10 11:46:21 +10:00
|
|
|
|
|
|
|
|
|
|
|
@property
|
2019-07-14 10:15:20 +10:00
|
|
|
def resize_method(self) -> ResizingMethod:
|
|
|
|
return ResizingMethod.scale \
|
|
|
|
if self.width > 96 or self.height > 96 else ResizingMethod.crop
|
2019-07-10 11:46:21 +10:00
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
def http(self) -> str:
|
|
|
|
return nio.Api.mxc_to_http(self.mxc)
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
def local_path(self) -> Path:
|
2019-07-14 10:15:20 +10:00
|
|
|
# pylint: disable=bad-string-format-type
|
2019-07-10 11:46:21 +10:00
|
|
|
parsed = urlparse(self.mxc)
|
2019-07-14 10:15:20 +10:00
|
|
|
name = "%s.%03d.%03d.%s" % (
|
2019-07-10 11:46:21 +10:00
|
|
|
parsed.path.lstrip("/"),
|
2019-07-14 10:15:20 +10:00
|
|
|
self.server_size[0],
|
|
|
|
self.server_size[1],
|
2019-07-10 11:46:21 +10:00
|
|
|
self.resize_method.value,
|
|
|
|
)
|
|
|
|
return self.provider.cache / parsed.netloc / name
|
|
|
|
|
|
|
|
|
|
|
|
async def download(self) -> bytes:
|
|
|
|
client = random.choice(
|
|
|
|
tuple(self.provider.app.backend.clients.values())
|
|
|
|
)
|
|
|
|
parsed = urlparse(self.mxc)
|
|
|
|
|
2019-07-19 10:49:47 +10:00
|
|
|
async with CONCURRENT_DOWNLOADS_LIMIT:
|
|
|
|
response = await client.thumbnail(
|
|
|
|
server_name = parsed.netloc,
|
|
|
|
media_id = parsed.path.lstrip("/"),
|
|
|
|
width = self.server_size[0],
|
|
|
|
height = self.server_size[1],
|
|
|
|
method = self.resize_method,
|
|
|
|
)
|
2019-07-18 16:08:01 +10:00
|
|
|
|
|
|
|
if isinstance(response, nio.ThumbnailError):
|
|
|
|
# Return a transparent 1x1 PNG
|
|
|
|
with BytesIO() as img_out:
|
|
|
|
PILImage.new("RGBA", (1, 1), (0, 0, 0, 0)).save(img_out, "PNG")
|
|
|
|
return img_out.getvalue()
|
|
|
|
|
2019-07-10 11:46:21 +10:00
|
|
|
body = response.body
|
|
|
|
|
|
|
|
if response.content_type not in ("image/jpeg", "image/png"):
|
2019-07-14 10:15:20 +10:00
|
|
|
with BytesIO(body) as img_in, BytesIO() as img_out:
|
|
|
|
PILImage.open(img_in).save(img_out, "PNG")
|
|
|
|
body = img_out.getvalue()
|
2019-07-10 11:46:21 +10:00
|
|
|
|
|
|
|
self.local_path.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
2019-07-19 10:44:17 +10:00
|
|
|
async with aiofiles.open(self.local_path, "wb") as file:
|
|
|
|
await file.write(body)
|
2019-07-10 11:46:21 +10:00
|
|
|
|
|
|
|
return body
|
|
|
|
|
|
|
|
|
|
|
|
async def get_data(self) -> ImageData:
|
|
|
|
try:
|
|
|
|
body = self.local_path.read_bytes()
|
|
|
|
except FileNotFoundError:
|
|
|
|
body = await self.download()
|
|
|
|
|
2019-07-14 10:15:20 +10:00
|
|
|
with BytesIO(body) as img_in:
|
|
|
|
real_size = PILImage.open(img_in).size
|
|
|
|
|
|
|
|
return (bytearray(body), real_size, pyotherside.format_data)
|
2019-07-10 11:46:21 +10:00
|
|
|
|
|
|
|
|
|
|
|
class ImageProvider:
|
|
|
|
def __init__(self, app) -> None:
|
|
|
|
self.app = app
|
|
|
|
|
|
|
|
self.cache = Path(self.app.appdirs.user_cache_dir) / "thumbnails"
|
|
|
|
self.cache.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
def get(self, image_id: str, requested_size: Size) -> ImageData:
|
2019-07-14 10:15:20 +10:00
|
|
|
if requested_size[0] < 1 or requested_size[1] < 1:
|
|
|
|
raise ValueError(f"width or height < 1: {requested_size!r}")
|
2019-07-10 11:46:21 +10:00
|
|
|
|
|
|
|
return asyncio.run_coroutine_threadsafe(
|
2019-07-14 10:15:20 +10:00
|
|
|
Thumbnail(self, image_id, *requested_size).get_data(),
|
|
|
|
self.app.loop
|
2019-07-10 11:46:21 +10:00
|
|
|
).result()
|