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

Signed-off-by: Sayandip Dutta <sayandip199309@gmail.com>
Co-authored-by: Cyrus Leung <cyrus.tl.leung@gmail.com>
This commit is contained in:
Sayandip Dutta 2025-08-15 14:58:00 +05:30 committed by GitHub
parent fe91ce9591
commit aa300c438d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 1 deletions

View File

@ -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()

View File

@ -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:
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 "