mirror of
https://github.com/home-assistant/core.git
synced 2025-04-25 01:38:02 +00:00
Fix deprecated call to mimetypes.guess_type in CachingStaticResource (#132299)
This commit is contained in:
parent
edc857b365
commit
88eb611eef
@ -4,6 +4,7 @@ from __future__ import annotations
|
||||
|
||||
from collections.abc import Mapping
|
||||
from pathlib import Path
|
||||
import sys
|
||||
from typing import Final
|
||||
|
||||
from aiohttp.hdrs import CACHE_CONTROL, CONTENT_TYPE
|
||||
@ -17,6 +18,15 @@ CACHE_HEADER = f"public, max-age={CACHE_TIME}"
|
||||
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."""
|
||||
@ -37,9 +47,7 @@ class CachingStaticResource(StaticResource):
|
||||
# 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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user