custom_components/bahmcloud_store/views.py aktualisiert
This commit is contained in:
@@ -10,7 +10,7 @@ from aiohttp import web
|
|||||||
from homeassistant.components.http import HomeAssistantView
|
from homeassistant.components.http import HomeAssistantView
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from .core import BCSCore # only for type hints, avoids circular import at runtime
|
from .core import BCSCore # type hints only, avoids circular import
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -18,34 +18,56 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
class StaticAssetsView(HomeAssistantView):
|
class StaticAssetsView(HomeAssistantView):
|
||||||
url = "/api/bahmcloud_store_static/{path:.*}"
|
url = "/api/bahmcloud_store_static/{path:.*}"
|
||||||
name = "api:bahmcloud_store_static"
|
name = "api:bahmcloud_store_static"
|
||||||
requires_auth = True
|
|
||||||
|
# IMPORTANT:
|
||||||
|
# Custom panel JS is safe to serve without auth. Some setups (reverse proxies / cookie quirks)
|
||||||
|
# may cause 401 for module loads. Disabling auth here avoids "Unable to load custom panel".
|
||||||
|
requires_auth = False
|
||||||
|
|
||||||
async def get(self, request: web.Request, path: str) -> web.Response:
|
async def get(self, request: web.Request, path: str) -> web.Response:
|
||||||
base = Path(__file__).resolve().parent / "panel"
|
base = Path(__file__).resolve().parent / "panel"
|
||||||
target = (base / path).resolve()
|
base_resolved = base.resolve()
|
||||||
|
|
||||||
if not str(target).startswith(str(base.resolve())):
|
req_path = (path or "").lstrip("/")
|
||||||
|
|
||||||
|
# Default file
|
||||||
|
if req_path == "":
|
||||||
|
req_path = "index.html"
|
||||||
|
|
||||||
|
target = (base / req_path).resolve()
|
||||||
|
|
||||||
|
# Security: prevent path traversal
|
||||||
|
if not str(target).startswith(str(base_resolved)):
|
||||||
return web.Response(status=404)
|
return web.Response(status=404)
|
||||||
|
|
||||||
|
# If folder -> index.html
|
||||||
if target.is_dir():
|
if target.is_dir():
|
||||||
target = target / "index.html"
|
target = (target / "index.html").resolve()
|
||||||
|
|
||||||
if not target.exists():
|
if not target.exists():
|
||||||
|
_LOGGER.warning("BCS static asset not found: %s", target)
|
||||||
return web.Response(status=404)
|
return web.Response(status=404)
|
||||||
|
|
||||||
ct = "text/plain"
|
# Content types
|
||||||
|
ct = "text/plain; charset=utf-8"
|
||||||
if target.suffix == ".js":
|
if target.suffix == ".js":
|
||||||
ct = "application/javascript"
|
ct = "application/javascript; charset=utf-8"
|
||||||
elif target.suffix == ".html":
|
elif target.suffix == ".html":
|
||||||
ct = "text/html"
|
ct = "text/html; charset=utf-8"
|
||||||
elif target.suffix == ".css":
|
elif target.suffix == ".css":
|
||||||
ct = "text/css"
|
ct = "text/css; charset=utf-8"
|
||||||
elif target.suffix == ".svg":
|
elif target.suffix == ".svg":
|
||||||
ct = "image/svg+xml"
|
ct = "image/svg+xml"
|
||||||
elif target.suffix == ".png":
|
elif target.suffix == ".png":
|
||||||
ct = "image/png"
|
ct = "image/png"
|
||||||
|
|
||||||
return web.Response(body=target.read_bytes(), content_type=ct)
|
resp = web.Response(body=target.read_bytes(), content_type=ct)
|
||||||
|
|
||||||
|
# Avoid stale caching issues during development
|
||||||
|
resp.headers["Cache-Control"] = "no-store, no-cache, must-revalidate, max-age=0"
|
||||||
|
resp.headers["Pragma"] = "no-cache"
|
||||||
|
|
||||||
|
return resp
|
||||||
|
|
||||||
|
|
||||||
class BCSApiView(HomeAssistantView):
|
class BCSApiView(HomeAssistantView):
|
||||||
@@ -54,7 +76,6 @@ class BCSApiView(HomeAssistantView):
|
|||||||
requires_auth = True
|
requires_auth = True
|
||||||
|
|
||||||
def __init__(self, core: Any) -> None:
|
def __init__(self, core: Any) -> None:
|
||||||
# Any to avoid runtime import cycle (core imports views)
|
|
||||||
self.core = core
|
self.core = core
|
||||||
|
|
||||||
async def get(self, request: web.Request) -> web.Response:
|
async def get(self, request: web.Request) -> web.Response:
|
||||||
@@ -116,17 +137,14 @@ class BCSReadmeView(HomeAssistantView):
|
|||||||
|
|
||||||
html = None
|
html = None
|
||||||
|
|
||||||
# Render markdown via HA util (best effort)
|
|
||||||
try:
|
try:
|
||||||
from homeassistant.util.markdown import async_render_markdown # type: ignore
|
from homeassistant.util.markdown import async_render_markdown # type: ignore
|
||||||
|
|
||||||
rendered = await async_render_markdown(self.core.hass, md)
|
html = await async_render_markdown(self.core.hass, md)
|
||||||
html = rendered
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
_LOGGER.debug("Markdown render failed: %s", e)
|
_LOGGER.debug("Markdown render failed: %s", e)
|
||||||
html = None
|
html = None
|
||||||
|
|
||||||
# Sanitize HTML if available
|
|
||||||
if html:
|
if html:
|
||||||
try:
|
try:
|
||||||
from homeassistant.util.sanitize_html import async_sanitize_html # type: ignore
|
from homeassistant.util.sanitize_html import async_sanitize_html # type: ignore
|
||||||
|
|||||||
Reference in New Issue
Block a user