Improve image checks for generic camera (#69037)

This commit is contained in:
Dave T 2022-03-31 23:05:39 +01:00 committed by GitHub
parent 130ca2213f
commit a741b26e28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 4 deletions

View File

@ -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"

View File

@ -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"

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

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