Skip to content

Commit 41f2297

Browse files
authored
Fix: stop repeated “IMAGE_NOT_FOUND” / “Invalid character in chunk size” errors when Plex artwork is missing
1 parent 1b49e5f commit 41f2297

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

custom_components/plex_recently_added/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
"iot_class": "local_polling",
1010
"issue_tracker": "https://github.com/custom-components/sensor.plex_recently_added/issues",
1111
"requirements": [],
12-
"version": "0.5.4"
12+
"version": "0.5.5"
1313
}

custom_components/plex_recently_added/redirect.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
from homeassistant.components.http import HomeAssistantView
22
from homeassistant.config_entries import ConfigEntry
3-
from aiohttp import web
3+
from aiohttp import web, ClientError
44
from homeassistant.helpers.aiohttp_client import async_get_clientsession
55
import requests
66
import os
7+
import logging
8+
9+
_LOGGER = logging.getLogger(__name__)
710

811
from homeassistant.const import (
912
CONF_API_KEY,
@@ -44,25 +47,30 @@ async def get(self, request):
4447
if if_none:
4548
fwd_headers["If-None-Match"] = if_none
4649

47-
async with self._session.get(url, headers=fwd_headers, timeout=10) as res:
48-
if res.status == 304:
49-
return web.Response(status=304)
50+
try:
51+
async with self._session.get(url, headers=fwd_headers, timeout=10) as res:
52+
if res.status == 304:
53+
return web.Response(status=304)
5054

51-
if res.status == 200:
52-
body = await res.read()
53-
headers = {
54-
"Content-Type": res.headers.get("Content-Type", "image/jpeg"),
55-
# Strong client caching: immutable for a year cuts repeat requests
56-
"Cache-Control": "public, max-age=31536000, immutable",
57-
}
58-
etag = res.headers.get("ETag")
59-
last_mod = res.headers.get("Last-Modified")
60-
if etag:
61-
headers["ETag"] = etag
62-
if last_mod:
63-
headers["Last-Modified"] = last_mod
64-
return web.Response(body=body, headers=headers)
55+
ctype = res.headers.get("Content-Type", "")
56+
if res.status == 200 and ctype.startswith("image/"):
57+
body = await res.read()
58+
headers = {
59+
"Content-Type": ctype or "image/jpeg",
60+
"Cache-Control": "public, max-age=31536000, immutable",
61+
}
62+
etag = res.headers.get("ETag")
63+
last_mod = res.headers.get("Last-Modified")
64+
if etag:
65+
headers["ETag"] = etag
66+
if last_mod:
67+
headers["Last-Modified"] = last_mod
68+
return web.Response(body=body, headers=headers)
6569

66-
return web.HTTPNotFound()
70+
_LOGGER.debug("Missing artwork (status=%s, ctype=%s) url=%s", res.status, ctype, url)
71+
return web.HTTPNotFound()
72+
except ClientError:
73+
_LOGGER.debug("Upstream image fetch failed url=%s", url)
74+
return web.HTTPBadGateway()
6775

6876

0 commit comments

Comments
 (0)