fix(media): bound attachment and request resources
This commit is contained in:
1 parent
2065fe2acd
commit
a105be4822
5 files changed
+110
-4
No files matched your search
+13
-1
@@ -15,6 +15,9 @@ class ImageHostError(RuntimeError):
|
||||
"""Ошибка при работе с image-host."""
|
||||
|
||||
|
||||
MAX_MIRRORED_IMAGE_BYTES = 15 * 1024 * 1024
|
||||
|
||||
|
||||
class ImageHostClient:
|
||||
"""
|
||||
Клиент для mirror VK-фото на external image-host.
|
||||
@@ -52,7 +55,16 @@ class ImageHostClient:
|
||||
return None
|
||||
source_url = str(photo["url"])
|
||||
with urlopen(source_url, timeout=self.timeout) as response:
|
||||
body = response.read()
|
||||
raw_length = response.headers.get("Content-Length")
|
||||
try:
|
||||
content_length = int(raw_length) if raw_length else None
|
||||
except ValueError:
|
||||
content_length = None
|
||||
if content_length is not None and content_length > MAX_MIRRORED_IMAGE_BYTES:
|
||||
raise ImageHostError("VK image is too large to mirror")
|
||||
body = response.read(MAX_MIRRORED_IMAGE_BYTES + 1)
|
||||
if len(body) > MAX_MIRRORED_IMAGE_BYTES:
|
||||
raise ImageHostError("VK image is too large to mirror")
|
||||
content_type = response.headers.get_content_type() or "image/jpeg"
|
||||
file_name = f"vk-{photo.get('owner_id', 'photo')}-{photo.get('id', 'upload')}.jpg"
|
||||
return self.upload_bytes(body, file_name, content_type)
|
||||
|
||||
Reference in new issue
Block a user