Skip to content

Commit c7ec3d1

Browse files
Fix image fallback for expired URLs (#2)
1 parent b597ff1 commit c7ec3d1

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

custom_components/sharepoint_photos/image.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ def __init__(self, coordinator, config_entry: ConfigEntry) -> None:
4040
self.access_tokens = []
4141
if not self.access_tokens:
4242
self.async_update_token()
43+
self._last_content: bytes | None = None
44+
self._last_content_type: str | None = None
4345
self._config_entry = config_entry
4446
site_name = config_entry.data.get("site_url", "").replace("https://", "").replace("/", "_")
4547
self._attr_unique_id = f"{DOMAIN}_{site_name}_current_image"
@@ -76,26 +78,40 @@ async def async_image(self) -> Optional[bytes]:
7678
api_client = self.coordinator._api_client
7779
content, content_type, status_code = await api_client.fetch_image_content(download_url)
7880

79-
if status_code == 401:
80-
_LOGGER.info("Image URL expired, refreshing coordinator data")
81+
if status_code in (401, 403):
82+
_LOGGER.info("Image URL expired (status=%s), refreshing coordinator data", status_code)
8183
await self.coordinator.async_request_refresh()
8284
photo = self._get_current_photo()
8385
if not photo:
84-
return None
86+
return self._last_content
8587
download_url = photo.get("download_url")
8688
if not download_url:
87-
return None
89+
return self._last_content
8890
content, content_type, status_code = await api_client.fetch_image_content(download_url)
8991

9092
if status_code == 200 and content:
9193
if content_type:
9294
self._attr_content_type = content_type
95+
self._last_content = content
96+
self._last_content_type = self._attr_content_type
9397
return content
9498

99+
if self._last_content:
100+
if self._last_content_type:
101+
self._attr_content_type = self._last_content_type
102+
_LOGGER.debug(
103+
"Returning cached image after fetch failed (status=%s)",
104+
status_code,
105+
)
106+
return self._last_content
107+
95108
_LOGGER.debug("Failed to fetch image content (status=%s)", status_code)
96109
return None
97110

98111
@property
99112
def available(self) -> bool:
100113
"""Return if entity is available."""
101-
return self.coordinator.last_update_success and self.coordinator.data is not None
114+
return (
115+
(self.coordinator.last_update_success and self.coordinator.data is not None)
116+
or self._last_content is not None
117+
)

custom_components/sharepoint_photos/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
"aiohttp>=3.8.0",
1414
"msal>=1.20.0"
1515
],
16-
"version": "1.1.4"
16+
"version": "1.1.5"
1717
}

0 commit comments

Comments
 (0)