mirror of
https://github.com/home-assistant/core.git
synced 2025-04-26 02:07:54 +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 collections.abc import Mapping
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
import sys
|
||||||
from typing import Final
|
from typing import Final
|
||||||
|
|
||||||
from aiohttp.hdrs import CACHE_CONTROL, CONTENT_TYPE
|
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}
|
CACHE_HEADERS: Mapping[str, str] = {CACHE_CONTROL: CACHE_HEADER}
|
||||||
RESPONSE_CACHE: LRU[tuple[str, Path], tuple[Path, str]] = LRU(512)
|
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):
|
class CachingStaticResource(StaticResource):
|
||||||
"""Static Resource handler that will add cache headers."""
|
"""Static Resource handler that will add cache headers."""
|
||||||
@ -37,9 +47,7 @@ class CachingStaticResource(StaticResource):
|
|||||||
# Must be directory index; ignore caching
|
# Must be directory index; ignore caching
|
||||||
return response
|
return response
|
||||||
file_path = response._path # noqa: SLF001
|
file_path = response._path # noqa: SLF001
|
||||||
response.content_type = (
|
response.content_type = _GUESSER(file_path)[0] or FALLBACK_CONTENT_TYPE
|
||||||
CONTENT_TYPES.guess_type(file_path)[0] or FALLBACK_CONTENT_TYPE
|
|
||||||
)
|
|
||||||
# Cache actual header after setter construction.
|
# Cache actual header after setter construction.
|
||||||
content_type = response.headers[CONTENT_TYPE]
|
content_type = response.headers[CONTENT_TYPE]
|
||||||
RESPONSE_CACHE[key] = (file_path, content_type)
|
RESPONSE_CACHE[key] = (file_path, content_type)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user