Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add functionality to optionally disable path encoding #98

Merged
merged 9 commits into from
Oct 10, 2022
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- [Installation](#installation)
- [Usage](#usage)
- [Signed URLs](#signed-urls)
- [Disabled Path Encoding](#disabled-path-encoding)
- [Srcset Generation](#srcset-generation)
* [Fixed-Width Images](#fixed-width-images)
+ [Variable Quality](#variable-quality)
Expand Down Expand Up @@ -69,6 +70,19 @@ To produce a signed URL, you must enable secure URLs on your source and then pro

```

## Disabled Path Encoding

Path encoding is enabled by default. It can be toggled off by setting `disable_path_encoding` to `True`:

```python
>>> from imgix import UrlBuilder
>>> ub = UrlBuilder("sdk-test.imgix.net", disable_path_encoding=True)
>>> ub.create_url(" <>[]{}|^%.jpg", {'w': 100, 'h': 100})
'https://sdk-test.imgix.net/ <>[]{}|^%.jpg?h=100&w=100'
```

Normally this would output a source URL like `https://demo.imgix.net/%20%3C%3E%5B%5D%7B%7D%7C%5E%25.jpg?h=100&2=100`, but since path encoding is disabled, it will output a source URL like `https://sdk-test.imgix.net/ <>[]{}|^%.jpg?h=100&w=100`.

## Srcset Generation

The imgix-python package allows for generation of custom srcset attributes, which can be invoked through the `create_srcset` method. By default, the generated srcset will allow for responsive size switching by building a list of image-width mappings.
Expand Down
10 changes: 8 additions & 2 deletions imgix/urlbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class UrlBuilder(object):
include_library_param : bool
If `True`, each created URL is suffixed with 'ixlib' parameter
indicating the library used for generating the URLs. (default `True`)
disable_path_encoding : bool
If `True`, the path component of the URL will not be encoded. (default `False`)

Methods
-------
Expand All @@ -58,14 +60,16 @@ def __init__(
domain,
use_https=True,
sign_key=None,
include_library_param=True):
include_library_param=True,
disable_path_encoding=False):

self.validate_domain(domain)

self._domain = domain
self._sign_key = sign_key
self._use_https = use_https
self._include_library_param = include_library_param
self._disable_path_encoding = disable_path_encoding

def validate_domain(self, domain):
"""
Expand Down Expand Up @@ -133,7 +137,9 @@ def _sanitize_path(self, path):

# Encode the path without a leading forward slash,
# then add it back before returning.
if _path.startswith("http"):
if self._disable_path_encoding:
return "/" + _path
elif _path.startswith("http"):
return "/" + self._encode_proxy_path(_path)
else:
return "/" + self._encode_file_path(_path)
Expand Down
17 changes: 17 additions & 0 deletions tests/test_srcset.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
DOMAIN = "testing.imgix.net"
TOKEN = "MYT0KEN"
JPG_PATH = "image.jpg"
JPG_PATH_WITH_SPACE = 'image 123.jpg'


def extract_descriptors(srcset=""):
Expand Down Expand Up @@ -132,6 +133,22 @@ def test_create_srcset_100_to_108():
assert expected == actual


def test_create_srcset_with_widths_and_disable_path_encoding_true():
ub = imgix.UrlBuilder(DOMAIN, include_library_param=False, disable_path_encoding=True)
actual = ub.create_srcset(JPG_PATH_WITH_SPACE, widths=[100])
expected = "https://testing.imgix.net/image 123.jpg?w=100 100w"

assert(expected == actual)


def test_create_srcset_start_equals_stop_with_disable_path_encoding_true():
ub = imgix.UrlBuilder(DOMAIN, include_library_param=False, disable_path_encoding=True)
actual = ub.create_srcset(JPG_PATH_WITH_SPACE, start=713, stop=713)
expected = "https://testing.imgix.net/image 123.jpg?w=713 713w"

assert(expected == actual)


def test_given_width_srcset_is_DPR():
srcset = _default_srcset({"w": 100})
device_pixel_ratio = 1
Expand Down
25 changes: 25 additions & 0 deletions tests/test_url_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ def _default_builder_with_signature():
)


def _default_builder_with_disable_path_encoding_true():
return imgix.UrlBuilder('my-social-network.imgix.net', disable_path_encoding=True,
include_library_param=False)


def test_create():
builder = imgix.UrlBuilder("my-social-network.imgix.net")
assert type(builder) is imgix.UrlBuilder
Expand Down Expand Up @@ -164,6 +169,26 @@ def test_create_url_with_questionable_chars_in_path():
)


def test_create_url_with_unicode_path_and_disable_path_encoding_true():
builder = _default_builder_with_disable_path_encoding_true()
url = builder.create_url("ساندویچ.jpg")
assert url == "https://my-social-network.imgix.net/" \
"ساندویچ.jpg"

def test_create_url_with_spaces_brackets_in_path_and_disable_path_encoding_true():
builder = _default_builder_with_disable_path_encoding_true()
url = builder.create_url(r" <>[]{}|\^%.jpg")
assert url == "https://my-social-network.imgix.net/" \
r" <>[]{}|\^%.jpg"


def test_create_url_with_questionable_chars_in_path_and_disable_path_encoding_true():
builder = _default_builder_with_disable_path_encoding_true()
url = builder.create_url("&$+,:;=?@#.jpg")
assert url == "https://my-social-network.imgix.net/" \
"&$+,:;=?@#.jpg"


def test_use_https():
# Defaults to https
builder = imgix.UrlBuilder("my-social-network.imgix.net")
Expand Down