Improve image checks for generic camera (#69037)

This commit is contained in:
Dave T
2022-03-31 15:10:01 -07:00
committed by Paulus Schoutsen
parent dcbca89d06
commit 2cb77c4702
4 changed files with 12 additions and 4 deletions
@@ -4,12 +4,13 @@ from __future__ import annotations
import contextlib
from errno import EHOSTUNREACH, EIO
from functools import partial
import imghdr
import io
import logging
from types import MappingProxyType
from typing import Any
from urllib.parse import urlparse, urlunparse
import PIL
from async_timeout import timeout
import av
from httpx import HTTPStatusError, RequestError, TimeoutException
@@ -110,9 +111,14 @@ def build_schema(
def get_image_type(image):
"""Get the format of downloaded bytes that could be an image."""
fmt = imghdr.what(None, h=image)
fmt = None
imagefile = io.BytesIO(image)
with contextlib.suppress(PIL.UnidentifiedImageError):
img = PIL.Image.open(imagefile)
fmt = img.format.lower()
if fmt is None:
# if imghdr can't figure it out, could be svg.
# if PIL can't figure it out, could be svg.
with contextlib.suppress(UnicodeDecodeError):
if image.decode("utf-8").lstrip().startswith("<svg"):
return "svg+xml"
@@ -2,7 +2,7 @@
"domain": "generic",
"name": "Generic Camera",
"config_flow": true,
"requirements": ["av==9.0.0"],
"requirements": ["av==9.0.0", "pillow==9.0.1"],
"documentation": "https://www.home-assistant.io/integrations/generic",
"codeowners": ["@davet2001"],
"iot_class": "local_push"
+1
View File
@@ -1201,6 +1201,7 @@ pi1wire==0.1.0
pilight==0.1.1
# homeassistant.components.doods
# homeassistant.components.generic
# homeassistant.components.image
# homeassistant.components.proxy
# homeassistant.components.qrcode
+1
View File
@@ -798,6 +798,7 @@ pi1wire==0.1.0
pilight==0.1.1
# homeassistant.components.doods
# homeassistant.components.generic
# homeassistant.components.image
# homeassistant.components.proxy
# homeassistant.components.qrcode