Fix local media browser source conflicting with local www folder (#40151)

This commit is contained in:
Franck Nijhof 2020-09-16 21:38:40 +02:00 committed by GitHub
parent 5ea04d64f6
commit f83f3c927a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 23 additions and 23 deletions

View file

@ -64,7 +64,7 @@ class LocalSource(MediaSource):
mime_type, _ = mimetypes.guess_type(
str(self.async_full_path(source_dir_id, location))
)
return PlayMedia(f"/local_source/{item.identifier}", mime_type)
return PlayMedia(f"/media/{item.identifier}", mime_type)
async def async_browse_media(
self, item: MediaSourceItem, media_types: Tuple[str] = MEDIA_MIME_TYPES
@ -177,7 +177,7 @@ class LocalMediaView(HomeAssistantView):
Returns media files in config/media.
"""
url = "/local_source/{source_dir_id}/{location:.*}"
url = "/media/{source_dir_id}/{location:.*}"
name = "media"
def __init__(self, hass: HomeAssistant, source: LocalSource):
@ -190,10 +190,10 @@ class LocalMediaView(HomeAssistantView):
) -> web.FileResponse:
"""Start a GET request."""
if location != sanitize_path(location):
return web.HTTPNotFound()
raise web.HTTPNotFound()
if source_dir_id not in self.hass.config.media_dirs:
return web.HTTPNotFound()
raise web.HTTPNotFound()
media_path = self.source.async_full_path(source_dir_id, location)

View file

@ -510,9 +510,9 @@ async def async_process_ha_core_config(hass: HomeAssistant, config: Dict) -> Non
if CONF_MEDIA_DIRS not in config:
if is_docker_env():
hac.media_dirs = {"media": "/media"}
hac.media_dirs = {"local": "/media"}
else:
hac.media_dirs = {"media": hass.config.path("media")}
hac.media_dirs = {"local": hass.config.path("media")}
# Init whitelist external dir
hac.allowlist_external_dirs = {hass.config.path("www"), *hac.media_dirs.values()}

View file

@ -205,7 +205,7 @@ async def async_test_home_assistant(loop):
hass.config.elevation = 0
hass.config.time_zone = date_util.get_time_zone("US/Pacific")
hass.config.units = METRIC_SYSTEM
hass.config.media_dirs = {"media": get_test_config_dir("media")}
hass.config.media_dirs = {"local": get_test_config_dir("media")}
hass.config.skip_pip = True
hass.config_entries = config_entries.ConfigEntries(hass, {})

View file

@ -65,7 +65,7 @@ async def test_async_resolve_media(hass):
media = await media_source.async_resolve_media(
hass,
media_source.generate_media_source_id(const.DOMAIN, "media/test.mp3"),
media_source.generate_media_source_id(const.DOMAIN, "local/test.mp3"),
)
assert isinstance(media, media_source.models.PlayMedia)
@ -140,7 +140,7 @@ async def test_websocket_resolve_media(hass, hass_ws_client):
client = await hass_ws_client(hass)
media = media_source.models.PlayMedia("/local_source/media/test.mp3", "audio/mpeg")
media = media_source.models.PlayMedia("/media/local/test.mp3", "audio/mpeg")
with patch(
"homeassistant.components.media_source.async_resolve_media",
@ -150,7 +150,7 @@ async def test_websocket_resolve_media(hass, hass_ws_client):
{
"id": 1,
"type": "media_source/resolve_media",
"media_content_id": f"{const.URI_SCHEME}{const.DOMAIN}/media/test.mp3",
"media_content_id": f"{const.URI_SCHEME}{const.DOMAIN}/local/test.mp3",
}
)

View file

@ -11,7 +11,7 @@ async def test_async_browse_media(hass):
"""Test browse media."""
local_media = hass.config.path("media")
await async_process_ha_core_config(
hass, {"media_dirs": {"media": local_media, "recordings": local_media}}
hass, {"media_dirs": {"local": local_media, "recordings": local_media}}
)
await hass.async_block_till_done()
@ -21,14 +21,14 @@ async def test_async_browse_media(hass):
# Test path not exists
with pytest.raises(media_source.BrowseError) as excinfo:
await media_source.async_browse_media(
hass, f"{const.URI_SCHEME}{const.DOMAIN}/media/test/not/exist"
hass, f"{const.URI_SCHEME}{const.DOMAIN}/local/test/not/exist"
)
assert str(excinfo.value) == "Path does not exist."
# Test browse file
with pytest.raises(media_source.BrowseError) as excinfo:
await media_source.async_browse_media(
hass, f"{const.URI_SCHEME}{const.DOMAIN}/media/test.mp3"
hass, f"{const.URI_SCHEME}{const.DOMAIN}/local/test.mp3"
)
assert str(excinfo.value) == "Path is not a directory."
@ -42,7 +42,7 @@ async def test_async_browse_media(hass):
# Test directory traversal
with pytest.raises(media_source.BrowseError) as excinfo:
await media_source.async_browse_media(
hass, f"{const.URI_SCHEME}{const.DOMAIN}/media/../configuration.yaml"
hass, f"{const.URI_SCHEME}{const.DOMAIN}/local/../configuration.yaml"
)
assert str(excinfo.value) == "Invalid path."
@ -53,7 +53,7 @@ async def test_async_browse_media(hass):
assert media
media = await media_source.async_browse_media(
hass, f"{const.URI_SCHEME}{const.DOMAIN}/media/."
hass, f"{const.URI_SCHEME}{const.DOMAIN}/local/."
)
assert media
@ -67,7 +67,7 @@ async def test_media_view(hass, hass_client):
"""Test media view."""
local_media = hass.config.path("media")
await async_process_ha_core_config(
hass, {"media_dirs": {"media": local_media, "recordings": local_media}}
hass, {"media_dirs": {"local": local_media, "recordings": local_media}}
)
await hass.async_block_till_done()
@ -77,23 +77,23 @@ async def test_media_view(hass, hass_client):
client = await hass_client()
# Protects against non-existent files
resp = await client.get("/local_source/media/invalid.txt")
resp = await client.get("/media/local/invalid.txt")
assert resp.status == 404
resp = await client.get("/local_source/recordings/invalid.txt")
resp = await client.get("/media/recordings/invalid.txt")
assert resp.status == 404
# Protects against non-media files
resp = await client.get("/local_source/media/not_media.txt")
resp = await client.get("/media/local/not_media.txt")
assert resp.status == 404
# Protects against unknown local media sources
resp = await client.get("/local_source/unknown_source/not_media.txt")
resp = await client.get("/media/unknown_source/not_media.txt")
assert resp.status == 404
# Fetch available media
resp = await client.get("/local_source/media/test.mp3")
resp = await client.get("/media/local/test.mp3")
assert resp.status == 200
resp = await client.get("/local_source/recordings/test.mp3")
resp = await client.get("/media/recordings/test.mp3")
assert resp.status == 200

View file

@ -499,7 +499,7 @@ async def test_loading_configuration_default_media_dirs_docker(hass):
assert hass.config.location_name == "Huis"
assert len(hass.config.allowlist_external_dirs) == 2
assert "/media" in hass.config.allowlist_external_dirs
assert hass.config.media_dirs == {"media": "/media"}
assert hass.config.media_dirs == {"local": "/media"}
async def test_loading_configuration_from_packages(hass):