mirror of
https://github.com/home-assistant/core.git
synced 2026-10-05 16:37:35 +01:00
Add path to resolved media in image_upload (#152093)
This commit is contained in:
1 parent
2cdf0b74d5
commit
f91e4090f9
4 files changed
+18
-4
No files matched your search
@@ -24,7 +24,7 @@ from homeassistant.helpers.storage import Store
|
||||
from homeassistant.helpers.typing import ConfigType, VolDictType
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from .const import DOMAIN
|
||||
from .const import DOMAIN, FOLDER_IMAGE
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
STORAGE_KEY = "image"
|
||||
@@ -45,7 +45,7 @@ CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
|
||||
|
||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||
"""Set up the Image integration."""
|
||||
image_dir = pathlib.Path(hass.config.path("image"))
|
||||
image_dir = pathlib.Path(hass.config.path(FOLDER_IMAGE))
|
||||
hass.data[DOMAIN] = storage_collection = ImageStorageCollection(hass, image_dir)
|
||||
await storage_collection.async_load()
|
||||
ImageUploadStorageCollectionWebsocket(
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
"""Constants for the Image Upload integration."""
|
||||
|
||||
DOMAIN = "image_upload"
|
||||
FOLDER_IMAGE = "image"
|
||||
@@ -2,6 +2,10 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pathlib
|
||||
|
||||
from propcache.api import cached_property
|
||||
|
||||
from homeassistant.components.media_player import BrowseError, MediaClass
|
||||
from homeassistant.components.media_source import (
|
||||
BrowseMediaSource,
|
||||
@@ -12,7 +16,7 @@ from homeassistant.components.media_source import (
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .const import DOMAIN
|
||||
from .const import DOMAIN, FOLDER_IMAGE
|
||||
|
||||
|
||||
async def async_get_media_source(hass: HomeAssistant) -> ImageUploadMediaSource:
|
||||
@@ -30,6 +34,11 @@ class ImageUploadMediaSource(MediaSource):
|
||||
super().__init__(DOMAIN)
|
||||
self.hass = hass
|
||||
|
||||
@cached_property
|
||||
def image_folder(self) -> pathlib.Path:
|
||||
"""Return the image folder path."""
|
||||
return pathlib.Path(self.hass.config.path(FOLDER_IMAGE))
|
||||
|
||||
async def async_resolve_media(self, item: MediaSourceItem) -> PlayMedia:
|
||||
"""Resolve media to a url."""
|
||||
image = self.hass.data[DOMAIN].data.get(item.identifier)
|
||||
@@ -38,7 +47,9 @@ class ImageUploadMediaSource(MediaSource):
|
||||
raise Unresolvable(f"Could not resolve media item: {item.identifier}")
|
||||
|
||||
return PlayMedia(
|
||||
f"/api/image/serve/{image['id']}/original", image["content_type"]
|
||||
f"/api/image/serve/{image['id']}/original",
|
||||
image["content_type"],
|
||||
path=self.image_folder / item.identifier / "original",
|
||||
)
|
||||
|
||||
async def async_browse_media(
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"""Test image_upload media source."""
|
||||
|
||||
from pathlib import Path
|
||||
import tempfile
|
||||
from unittest.mock import patch
|
||||
|
||||
@@ -79,6 +80,7 @@ async def test_resolving(
|
||||
assert item is not None
|
||||
assert item.url == f"/api/image/serve/{image_id}/original"
|
||||
assert item.mime_type == "image/png"
|
||||
assert item.path == Path(hass.config.path("image")) / image_id / "original"
|
||||
|
||||
invalid_id = "aabbccddeeff"
|
||||
with pytest.raises(
|
||||
|
||||
Reference in new issue
Block a user