mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +00:00
Extend image_upload to return the original image (#116652)
This commit is contained in:
parent
585892f067
commit
43f42dd512
@ -191,31 +191,33 @@ class ImageServeView(HomeAssistantView):
|
||||
filename: str,
|
||||
) -> web.FileResponse:
|
||||
"""Serve image."""
|
||||
try:
|
||||
width, height = _validate_size_from_filename(filename)
|
||||
except (ValueError, IndexError) as err:
|
||||
raise web.HTTPBadRequest from err
|
||||
|
||||
image_info = self.image_collection.data.get(image_id)
|
||||
|
||||
if image_info is None:
|
||||
raise web.HTTPNotFound
|
||||
|
||||
hass = request.app[KEY_HASS]
|
||||
target_file = self.image_folder / image_id / f"{width}x{height}"
|
||||
if filename == "original":
|
||||
target_file = self.image_folder / image_id / filename
|
||||
else:
|
||||
try:
|
||||
width, height = _validate_size_from_filename(filename)
|
||||
except (ValueError, IndexError) as err:
|
||||
raise web.HTTPBadRequest from err
|
||||
|
||||
if not target_file.is_file():
|
||||
async with self.transform_lock:
|
||||
# Another check in case another request already
|
||||
# finished it while waiting
|
||||
if not target_file.is_file():
|
||||
await hass.async_add_executor_job(
|
||||
_generate_thumbnail,
|
||||
self.image_folder / image_id / "original",
|
||||
image_info["content_type"],
|
||||
target_file,
|
||||
(width, height),
|
||||
)
|
||||
hass = request.app[KEY_HASS]
|
||||
target_file = self.image_folder / image_id / f"{width}x{height}"
|
||||
|
||||
if not target_file.is_file():
|
||||
async with self.transform_lock:
|
||||
# Another check in case another request already
|
||||
# finished it while waiting
|
||||
if not target_file.is_file():
|
||||
await hass.async_add_executor_job(
|
||||
_generate_thumbnail,
|
||||
self.image_folder / image_id / "original",
|
||||
image_info["content_type"],
|
||||
target_file,
|
||||
(width, height),
|
||||
)
|
||||
|
||||
return web.FileResponse(
|
||||
target_file,
|
||||
|
@ -49,7 +49,14 @@ async def test_upload_image(
|
||||
|
||||
tempdir = pathlib.Path(tempdir)
|
||||
item_folder: pathlib.Path = tempdir / item["id"]
|
||||
assert (item_folder / "original").read_bytes() == TEST_IMAGE.read_bytes()
|
||||
test_image_bytes = TEST_IMAGE.read_bytes()
|
||||
assert (item_folder / "original").read_bytes() == test_image_bytes
|
||||
|
||||
# fetch original image
|
||||
res = await client.get(f"/api/image/serve/{item['id']}/original")
|
||||
assert res.status == 200
|
||||
fetched_image_bytes = await res.read()
|
||||
assert fetched_image_bytes == test_image_bytes
|
||||
|
||||
# fetch non-existing image
|
||||
res = await client.get("/api/image/serve/non-existing/256x256")
|
||||
|
Loading…
x
Reference in New Issue
Block a user