Skip to content

Commit 5668c7b

Browse files
committed
Only accept images in allows MIME types
1 parent 50be780 commit 5668c7b

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

custom_components/immich/hub.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
_HEADER_API_KEY = "x-api-key"
1212
_LOGGER = logging.getLogger(__name__)
1313

14+
_ALLOWED_MIME_TYPES = ["image/png", "image/jpeg"]
15+
1416

1517
class ImmichHub:
1618
"""Immich API hub."""
@@ -65,7 +67,7 @@ async def get_my_user_info(self) -> dict:
6567
_LOGGER.error("Error connecting to the API: %s", exception)
6668
raise CannotConnect from exception
6769

68-
async def download_asset(self, asset_id: str) -> bytes:
70+
async def download_asset(self, asset_id: str) -> bytes | None:
6971
"""Download the asset."""
7072
try:
7173
async with aiohttp.ClientSession() as session:
@@ -75,7 +77,13 @@ async def download_asset(self, asset_id: str) -> bytes:
7577
async with session.get(url=url, headers=headers) as response:
7678
if response.status != 200:
7779
_LOGGER.error("Error from API: status=%d", response.status)
78-
raise ApiError()
80+
return None
81+
82+
if response.content_type not in _ALLOWED_MIME_TYPES:
83+
_LOGGER.error(
84+
"MIME type is not supported: %s", response.content_type
85+
)
86+
return None
7987

8088
return await response.read()
8189
except aiohttp.ClientError as exception:

custom_components/immich/image.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Image device for Immich integration."""
22
from __future__ import annotations
33

4+
import asyncio
45
from datetime import datetime, timedelta
56
import logging
67
import random
@@ -113,10 +114,20 @@ async def _get_next_asset_id(self) -> str | None:
113114

114115
async def _load_and_cache_next_image(self) -> None:
115116
"""Download and cache the image."""
116-
asset_id = await self._get_next_asset_id()
117+
asset_bytes = None
118+
119+
while not asset_bytes:
120+
asset_id = await self._get_next_asset_id()
121+
122+
if not asset_id:
123+
return
117124

118-
if asset_id:
119125
asset_bytes = await self.hub.download_asset(asset_id)
126+
127+
if not asset_bytes:
128+
await asyncio.sleep(1)
129+
continue
130+
120131
self._current_image_bytes = asset_bytes
121132
self._attr_image_last_updated = datetime.now()
122133

custom_components/immich/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
"issue_tracker": "https://github.com/outadoc/immich-home-assistant/issues",
1111
"requirements": ["url-normalize==1.4.3"],
1212
"ssdp": [],
13-
"version": "0.1.0",
13+
"version": "0.2.0",
1414
"zeroconf": []
1515
}

0 commit comments

Comments
 (0)