Skip to content

Commit 128e022

Browse files
ANKUR DWIVEDIANKUR DWIVEDI
authored andcommitted
diactric fix
1 parent 0125fd2 commit 128e022

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

imagekitio/url.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import sys
44
from datetime import datetime as dt
55
from typing import Any, Dict, List
6-
from urllib.parse import ParseResult, urlparse, urlunparse, parse_qsl, urlencode
6+
from urllib.parse import ParseResult, urlparse, urlunparse, parse_qsl, urlencode, quote
77

88
from .constants.defaults import Default
99
from .constants.supported_transform import SUPPORTED_TRANS
@@ -85,6 +85,7 @@ def build_url(self, options: dict) -> str:
8585
private_key = options.get("private_key")
8686
expiry_timestamp = self.get_signature_timestamp(expire_seconds)
8787
url_signature = self.get_signature(
88+
self,
8889
private_key=private_key,
8990
url=url_object.geturl(),
9091
url_endpoint=url_endpoint,
@@ -129,7 +130,14 @@ def get_signature_timestamp(expiry_seconds: int = None) -> int:
129130
return current_timestamp + expiry_seconds
130131

131132
@staticmethod
132-
def get_signature(private_key, url, url_endpoint, expiry_timestamp: int) -> str:
133+
def get_signature(self, private_key, url, url_endpoint, expiry_timestamp: int) -> str:
134+
last_slash_pos = url.rfind('/')
135+
question_mark_pos = url.find('?', last_slash_pos)
136+
path = url[last_slash_pos + 1:question_mark_pos] if question_mark_pos != -1 else url[last_slash_pos + 1:]
137+
encoded_path = self.encode_string_if_required(self,path)
138+
encoded_url = url[:last_slash_pos + 1] + encoded_path + url[question_mark_pos:] if question_mark_pos != -1 else url[:last_slash_pos + 1] + encoded_path
139+
url = encoded_url
140+
print(url)
133141
""" "
134142
create signature(hashed hex key) from
135143
private_key, url, url_endpoint and expiry_timestamp
@@ -211,3 +219,11 @@ def transformation_to_str(transformation):
211219
)
212220

213221
return Default.CHAIN_TRANSFORM_DELIMITER.value.join(parsed_transforms)
222+
223+
@staticmethod
224+
def has_more_than_ascii(s):
225+
return any(ord(char) > 127 for char in s)
226+
227+
@staticmethod
228+
def encode_string_if_required(self,s):
229+
return quote(s) if self.has_more_than_ascii(s) else s

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
setuptools.setup(
1010
name="imagekitio",
11-
version="4.0.0",
11+
version="4.0.1",
1212
description="Python wrapper for the ImageKit API",
1313
long_description=long_description,
1414
long_description_content_type="text/markdown",

tests/test_generate_url.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,20 @@ def test_url_signed_with_expire_in_seconds(self):
325325
}
326326
url = self.client.url(options)
327327
self.assertIn("ik-t", url)
328+
329+
def test_url_signed_with_expire_in_seconds_with_diacritic(self):
330+
options = {
331+
"path": "/four-penguins-with-é.webp",
332+
"transformation": [
333+
{
334+
"width": "400",
335+
},
336+
],
337+
"signed": True,
338+
"expire_seconds": 100,
339+
}
340+
url = self.client.url(options)
341+
self.assertIn("ik-t", url)
328342

329343
def test_generate_url_with_path_and_src_uses_path(self):
330344
"""

0 commit comments

Comments
 (0)