@@ -288,8 +288,10 @@ def __init__(self, component: EntityComponent[ImageEntity]) -> None:
288288 """Initialize an image view."""
289289 self .component = component
290290
291- async def get (self , request : web .Request , entity_id : str ) -> web .StreamResponse :
292- """Start a GET request."""
291+ async def _authenticate_request (
292+ self , request : web .Request , entity_id : str
293+ ) -> ImageEntity :
294+ """Authenticate request and return image entity."""
293295 if (image_entity := self .component .get_entity (entity_id )) is None :
294296 raise web .HTTPNotFound
295297
@@ -306,6 +308,31 @@ async def get(self, request: web.Request, entity_id: str) -> web.StreamResponse:
306308 # Invalid sigAuth or image entity access token
307309 raise web .HTTPForbidden
308310
311+ return image_entity
312+
313+ async def head (self , request : web .Request , entity_id : str ) -> web .Response :
314+ """Start a HEAD request.
315+
316+ This is sent by some DLNA renderers, like Samsung ones, prior to sending
317+ the GET request.
318+ """
319+ image_entity = await self ._authenticate_request (request , entity_id )
320+
321+ # Don't use `handle` as we don't care about the stream case, we only want
322+ # to verify that the image exists.
323+ try :
324+ image = await _async_get_image (image_entity , IMAGE_TIMEOUT )
325+ except (HomeAssistantError , ValueError ) as ex :
326+ raise web .HTTPInternalServerError from ex
327+
328+ return web .Response (
329+ content_type = image .content_type ,
330+ headers = {"Content-Length" : str (len (image .content ))},
331+ )
332+
333+ async def get (self , request : web .Request , entity_id : str ) -> web .StreamResponse :
334+ """Start a GET request."""
335+ image_entity = await self ._authenticate_request (request , entity_id )
309336 return await self .handle (request , image_entity )
310337
311338 async def handle (
@@ -317,7 +344,11 @@ async def handle(
317344 except (HomeAssistantError , ValueError ) as ex :
318345 raise web .HTTPInternalServerError from ex
319346
320- return web .Response (body = image .content , content_type = image .content_type )
347+ return web .Response (
348+ body = image .content ,
349+ content_type = image .content_type ,
350+ headers = {"Content-Length" : str (len (image .content ))},
351+ )
321352
322353
323354async def async_get_still_stream (
0 commit comments