Unverified Commit aa300c43 authored by Sayandip Dutta's avatar Sayandip Dutta Committed by GitHub
Browse files

[Bugfix] Unquote file uri before reading image (#22912)


Signed-off-by: default avatarSayandip Dutta <sayandip199309@gmail.com>
Co-authored-by: default avatarCyrus Leung <cyrus.tl.leung@gmail.com>
parent fe91ce95
...@@ -148,6 +148,32 @@ async def test_fetch_image_local_files(image_url: str): ...@@ -148,6 +148,32 @@ async def test_fetch_image_local_files(image_url: str):
f"file://{temp_dir}/../{os.path.basename(image_url)}") f"file://{temp_dir}/../{os.path.basename(image_url)}")
@pytest.mark.asyncio
async def test_fetch_image_local_files_with_space_in_name():
image_url = TEST_IMAGE_URLS[0]
connector = MediaConnector()
with TemporaryDirectory() as temp_dir:
local_connector = MediaConnector(allowed_local_media_path=temp_dir)
origin_image = connector.fetch_image(image_url)
filename = "file name with space.jpg"
origin_image.save(os.path.join(temp_dir, filename),
quality=100,
icc_profile=origin_image.info.get('icc_profile'))
try:
image_async = await local_connector.fetch_image_async(
f"file://{temp_dir}/{filename}")
image_sync = local_connector.fetch_image(
f"file://{temp_dir}/{filename}")
except FileNotFoundError as e:
pytest.fail(
"Failed to fetch image with space in name: {}".format(e))
# Check that the images are equal
assert not ImageChops.difference(image_sync, image_async).getbbox()
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_fetch_image_error_conversion(): async def test_fetch_image_error_conversion():
connector = MediaConnector() connector = MediaConnector()
......
...@@ -9,6 +9,7 @@ from itertools import groupby ...@@ -9,6 +9,7 @@ from itertools import groupby
from pathlib import Path from pathlib import Path
from typing import TYPE_CHECKING, Any, Optional, TypeVar, Union from typing import TYPE_CHECKING, Any, Optional, TypeVar, Union
from urllib.parse import ParseResult, urlparse from urllib.parse import ParseResult, urlparse
from urllib.request import url2pathname
import numpy as np import numpy as np
import numpy.typing as npt import numpy.typing as npt
...@@ -108,7 +109,7 @@ class MediaConnector: ...@@ -108,7 +109,7 @@ class MediaConnector:
raise RuntimeError("Cannot load local files without " raise RuntimeError("Cannot load local files without "
"`--allowed-local-media-path`.") "`--allowed-local-media-path`.")
filepath = Path(url_spec.path) filepath = Path(url2pathname(url_spec.path))
if allowed_local_media_path not in filepath.resolve().parents: if allowed_local_media_path not in filepath.resolve().parents:
raise ValueError( raise ValueError(
f"The file path {filepath} must be a subpath " f"The file path {filepath} must be a subpath "
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment