Skip to content

Commit

Permalink
Fix pasting images from Web when URL contanis non-ASCII chars (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbreu committed Dec 28, 2023
1 parent c6a6811 commit 327126b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
5 changes: 3 additions & 2 deletions beeref/fileio/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ def load_image(path):
path = os.path.normpath(path.toLocalFile())
return (exif_rotated_image(path), path)

url = bytes(path.toEncoded()).decode()
img = exif_rotated_image()
try:
imgdata = request.urlopen(path.url()).read()
imgdata = request.urlopen(url).read()
except URLError as e:
logger.debug(f'Downloading image failed: {e.reason}')
else:
Expand All @@ -96,4 +97,4 @@ def load_image(path):
f.write(imgdata)
logger.debug(f'Temporarily saved in: {fname}')
img = exif_rotated_image(fname)
return (img, path.url())
return (img, url)
13 changes: 13 additions & 0 deletions tests/fileio/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ def test_load_image_loads_from_existing_web_url(view, imgdata3x3):
assert filename == url


@httpretty.activate
def test_load_image_loads_from_existing_web_url_non_ascii(view, imgdata3x3):
url = 'http://example.com/föö.png'
httpretty.register_uri(
httpretty.GET,
url,
body=imgdata3x3,
)
img, filename = load_image(QtCore.QUrl(url))
assert img.isNull() is False
assert filename == 'http://example.com/f%C3%B6%C3%B6.png'


@httpretty.activate
def test_load_image_loads_from_web_url_errors(view, imgfilename3x3):
url = 'http://example.com/foo.png'
Expand Down

0 comments on commit 327126b

Please sign in to comment.