Skip to content

Commit

Permalink
Fix deprecated call to mimetypes.guess_type in CachingStaticResource (h…
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Dec 6, 2024
1 parent edc857b commit 88eb611
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions homeassistant/components/http/static.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from collections.abc import Mapping
from pathlib import Path
import sys
from typing import Final

from aiohttp.hdrs import CACHE_CONTROL, CONTENT_TYPE
Expand All @@ -17,6 +18,15 @@
CACHE_HEADERS: Mapping[str, str] = {CACHE_CONTROL: CACHE_HEADER}
RESPONSE_CACHE: LRU[tuple[str, Path], tuple[Path, str]] = LRU(512)

if sys.version_info >= (3, 13):
# guess_type is soft-deprecated in 3.13
# for paths and should only be used for
# URLs. guess_file_type should be used
# for paths instead.
_GUESSER = CONTENT_TYPES.guess_file_type
else:
_GUESSER = CONTENT_TYPES.guess_type


class CachingStaticResource(StaticResource):
"""Static Resource handler that will add cache headers."""
Expand All @@ -37,9 +47,7 @@ async def _handle(self, request: Request) -> StreamResponse:
# Must be directory index; ignore caching
return response
file_path = response._path # noqa: SLF001
response.content_type = (
CONTENT_TYPES.guess_type(file_path)[0] or FALLBACK_CONTENT_TYPE
)
response.content_type = _GUESSER(file_path)[0] or FALLBACK_CONTENT_TYPE
# Cache actual header after setter construction.
content_type = response.headers[CONTENT_TYPE]
RESPONSE_CACHE[key] = (file_path, content_type)
Expand Down

0 comments on commit 88eb611

Please sign in to comment.