mirror of
https://github.com/home-assistant/core.git
synced 2026-02-15 07:36:16 +00:00
Create dir on media upload if not exists (#152254)
This commit is contained in:
@@ -133,14 +133,13 @@ class LocalSource(MediaSource):
|
||||
|
||||
def _do_move() -> None:
|
||||
"""Move file to target."""
|
||||
if not target_dir.is_dir():
|
||||
raise PathNotSupportedError("Target is not an existing directory")
|
||||
|
||||
target_path = target_dir / uploaded_file.filename
|
||||
|
||||
try:
|
||||
target_path = target_dir / uploaded_file.filename
|
||||
|
||||
target_path.relative_to(target_dir)
|
||||
raise_if_invalid_path(str(target_path))
|
||||
|
||||
target_dir.mkdir(parents=True, exist_ok=True)
|
||||
except ValueError as err:
|
||||
raise PathNotSupportedError("Invalid path") from err
|
||||
|
||||
|
||||
@@ -164,19 +164,21 @@ async def test_upload_view(
|
||||
client = await hass_client()
|
||||
|
||||
# Test normal upload
|
||||
res = await client.post(
|
||||
"/api/media_source/local_source/upload",
|
||||
data={
|
||||
"media_content_id": "media-source://media_source/test_dir",
|
||||
"file": get_file("logo.png"),
|
||||
},
|
||||
)
|
||||
with patch.object(Path, "mkdir", autospec=True, return_value=None) as mock_mkdir:
|
||||
res = await client.post(
|
||||
"/api/media_source/local_source/upload",
|
||||
data={
|
||||
"media_content_id": "media-source://media_source/test_dir",
|
||||
"file": get_file("logo.png"),
|
||||
},
|
||||
)
|
||||
|
||||
assert res.status == 200
|
||||
data = await res.json()
|
||||
assert data["media_content_id"] == "media-source://media_source/test_dir/logo.png"
|
||||
uploaded_path = Path(temp_dir) / "logo.png"
|
||||
assert uploaded_path.is_file()
|
||||
mock_mkdir.assert_called_once()
|
||||
|
||||
resolved = await media_source.async_resolve_media(
|
||||
hass, data["media_content_id"], target_media_player=None
|
||||
@@ -187,8 +189,6 @@ async def test_upload_view(
|
||||
|
||||
# Test with bad media source ID
|
||||
for bad_id in (
|
||||
# Subdir doesn't exist
|
||||
"media-source://media_source/test_dir/some-other-dir",
|
||||
# Main dir doesn't exist
|
||||
"media-source://media_source/test_dir2",
|
||||
# Location is invalid
|
||||
|
||||
Reference in New Issue
Block a user