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 parameter to disable path encoding #124

Merged
merged 2 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ client.path('/images/demo.png').to_url(w: 200)
#=> https://your-subdomain.imgix.net/images/demo.png?w=200&s=2eadddacaa9bba4b88900d245f03f51e
```

To disable path encoding, pass `{disable_path_encoding: true}` as the second argument to the `Imgix#Path#to_url` function.

```rb
client = Imgix::Client.new(domain: 'your-subdomain.imgix.net', secure_url_token: 'your-token')
client.path('/[images]/demo.png').to_url
#=> https://your-subdomain.imgix.net/%5Bimages%5D/demo.png?s=270832685733a36ba02bd8ab9fd72df5
client.path('/[images]/demo.png').to_url({},{disable_path_encoding: true})
#=> https://your-subdomain.imgix.net/[images]/demo.png?s=ed6eb07e9eff3f6c8bbcc83fc4f63198
```
## Srcset Generation

The imgix gem allows for generation of custom `srcset` attributes, which can be invoked through `Imgix::Path#to_srcset`. By default, the `srcset` generated will allow for responsive size switching by building a list of image-width mappings.
Expand Down
16 changes: 9 additions & 7 deletions lib/imgix/path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ def initialize(prefix, secure_url_token, path = "/")
@options = {}
end

def to_url(opts = {})
sanitized_path = sanitize_path(@path)
def to_url(params = {}, options = {})
sanitized_path = sanitize_path(@path, options)
prev_options = @options.dup
@options.merge!(opts)
@options.merge!(params)

current_path_and_params = path_and_params(sanitized_path)
url = @prefix + current_path_and_params
Expand Down Expand Up @@ -132,11 +132,13 @@ def method_missing(method, *args, &block)
# This includes " +?:#" characters. If a path is being used as a proxy, utf8
# encode everything. If it is not being used as proxy, leave certain chars, like
# "/", alone. Method assumes path is not already encoded.
def sanitize_path(path)
def sanitize_path(path, options = {})
# remove the leading "/", we'll add it back after encoding
path = path.slice(1, path.length) if Regexp.new('^/') =~ path
if options[:disable_path_encoding]
return "/" + path
# if path is being used as a proxy, encode the entire thing
if /^https?/ =~ path
elsif /^https?/ =~ path
return encode_URI_Component(path)
else
# otherwise, encode only specific characters
Expand Down Expand Up @@ -207,7 +209,7 @@ def build_srcset_pairs(options:, params:)

srcset_widths.each do |width|
params[:w] = width
srcset += "#{to_url(params)} #{width}w,\n"
srcset += "#{to_url(params, options)} #{width}w,\n"
end

srcset[0..-3]
Expand All @@ -227,7 +229,7 @@ def build_dpr_srcset(options:, params:)

params[:q] = quality || DPR_QUALITY[ratio] unless disable_variable_quality

srcset += "#{to_url(params)} #{ratio}x,\n"
srcset += "#{to_url(params, options)} #{ratio}x,\n"
end

srcset[0..-3]
Expand Down
6 changes: 6 additions & 0 deletions test/units/path_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ def test_creating_a_path
assert_equal "https://demo.imgix.net/images/demo.png?s=2c7c157eaf23b06a0deb2f60b81938c4", path.to_url
end

def test_disable_path_encoding
path = client.path("[images]/demo.png")
assert_equal "https://demo.imgix.net/%5Bimages%5D/demo.png?s=270832685733a36ba02bd8ab9fd72df5", path.to_url
assert_equal "https://demo.imgix.net/[images]/demo.png?s=ed6eb07e9eff3f6c8bbcc83fc4f63198", path.to_url({},{disable_path_encoding: true})
end

def test_signing_path_with_param
url = "https://demo.imgix.net/images/demo.png?w=200&s=da421114ca238d1f4a927b889f67c34e"
path = client.path("/images/demo.png").w(200)
Expand Down
25 changes: 25 additions & 0 deletions test/units/srcset_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -760,4 +760,29 @@ def srcset
)
end
end

class SrcsetDisablePathEncoding < Imgix::Test
include SrcsetTest

def test_encode_path_by_default
srcset = mock_client.to_srcset
src = srcset.split(" ")[0]
assert_equal(src, "https://testing.imgix.net/%5Bimage%5D.jpg?w=100")
end

def test_disable_path_encoding
srcset = mock_client.to_srcset(options: { disable_path_encoding: true })
src = srcset.split(" ")[0]
assert_equal(src, "https://testing.imgix.net/[image].jpg?w=100")
end

private

def mock_client
Imgix::Client.new(
domain: DOMAIN,
include_library_param: false
).path("[image].jpg")
end
end
end