Extend image_upload to return the original image (#116652)

This commit is contained in:
Adam Kapos 2024-05-29 12:16:23 +03:00 committed by GitHub
parent 585892f067
commit 43f42dd512
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 21 deletions

View File

@ -191,16 +191,18 @@ class ImageServeView(HomeAssistantView):
filename: str, filename: str,
) -> web.FileResponse: ) -> web.FileResponse:
"""Serve image.""" """Serve image."""
image_info = self.image_collection.data.get(image_id)
if image_info is None:
raise web.HTTPNotFound
if filename == "original":
target_file = self.image_folder / image_id / filename
else:
try: try:
width, height = _validate_size_from_filename(filename) width, height = _validate_size_from_filename(filename)
except (ValueError, IndexError) as err: except (ValueError, IndexError) as err:
raise web.HTTPBadRequest from 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] hass = request.app[KEY_HASS]
target_file = self.image_folder / image_id / f"{width}x{height}" target_file = self.image_folder / image_id / f"{width}x{height}"

View File

@ -49,7 +49,14 @@ async def test_upload_image(
tempdir = pathlib.Path(tempdir) tempdir = pathlib.Path(tempdir)
item_folder: pathlib.Path = tempdir / item["id"] 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 # fetch non-existing image
res = await client.get("/api/image/serve/non-existing/256x256") res = await client.get("/api/image/serve/non-existing/256x256")