mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +00:00
Include path when media source file can be accessed on disk (#148180)
This commit is contained in:
parent
7898e3f0fb
commit
1e164c94b1
@ -80,7 +80,7 @@ class LocalSource(MediaSource):
|
||||
path = self.async_full_path(source_dir_id, location)
|
||||
mime_type, _ = mimetypes.guess_type(str(path))
|
||||
assert isinstance(mime_type, str)
|
||||
return PlayMedia(f"/media/{item.identifier}", mime_type)
|
||||
return PlayMedia(f"/media/{item.identifier}", mime_type, path=path)
|
||||
|
||||
async def async_browse_media(self, item: MediaSourceItem) -> BrowseMediaSource:
|
||||
"""Return media."""
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from dataclasses import dataclass, field
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from homeassistant.components.media_player import BrowseMedia, MediaClass, MediaType
|
||||
@ -10,6 +10,9 @@ from homeassistant.core import HomeAssistant, callback
|
||||
|
||||
from .const import MEDIA_SOURCE_DATA, URI_SCHEME, URI_SCHEME_REGEX
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class PlayMedia:
|
||||
@ -17,6 +20,7 @@ class PlayMedia:
|
||||
|
||||
url: str
|
||||
mime_type: str
|
||||
path: Path | None = field(kw_only=True, default=None)
|
||||
|
||||
|
||||
class BrowseMediaSource(BrowseMedia):
|
||||
|
@ -167,13 +167,23 @@ async def test_upload_view(
|
||||
res = await client.post(
|
||||
"/api/media_source/local_source/upload",
|
||||
data={
|
||||
"media_content_id": "media-source://media_source/test_dir/.",
|
||||
"media_content_id": "media-source://media_source/test_dir",
|
||||
"file": get_file("logo.png"),
|
||||
},
|
||||
)
|
||||
|
||||
assert res.status == 200
|
||||
assert (Path(temp_dir) / "logo.png").is_file()
|
||||
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()
|
||||
|
||||
resolved = await media_source.async_resolve_media(
|
||||
hass, data["media_content_id"], target_media_player=None
|
||||
)
|
||||
assert resolved.url == "/media/test_dir/logo.png"
|
||||
assert resolved.mime_type == "image/png"
|
||||
assert resolved.path == uploaded_path
|
||||
|
||||
# Test with bad media source ID
|
||||
for bad_id in (
|
||||
|
@ -28,12 +28,14 @@
|
||||
# name: test_file[system_bridge_media_source_file_image]
|
||||
dict({
|
||||
'mime_type': 'image/jpeg',
|
||||
'path': None,
|
||||
'url': 'http://127.0.0.1:9170/api/media/file/data?token=abc-123-def-456-ghi&base=documents&path=testimage.jpg',
|
||||
})
|
||||
# ---
|
||||
# name: test_file[system_bridge_media_source_file_text]
|
||||
dict({
|
||||
'mime_type': 'text/plain',
|
||||
'path': None,
|
||||
'url': 'http://127.0.0.1:9170/api/media/file/data?token=abc-123-def-456-ghi&base=documents&path=testfile.txt',
|
||||
})
|
||||
# ---
|
||||
|
Loading…
x
Reference in New Issue
Block a user