Skip to content

Commit 8e4bf0c

Browse files
authored
Merge pull request #7 from Fileboost/feature/support-disposition-attachment
Feature: Support disposition attachment
2 parents b017a35 + 88c07fc commit 8e4bf0c

4 files changed

Lines changed: 38 additions & 4 deletions

File tree

lib/fileboost/helpers.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Helpers
55
# Generate an optimized image tag using Fileboost
66
#
77
# @param asset [ActiveStorage::Blob, ActiveStorage::Attached, ActiveStorage::VariantWithRecord] The ActiveStorage image asset
8-
# @param options [Hash] Image transformation and HTML options
8+
# @param options [Hash] Accepts a `:resize` hash for transformations plus standard HTML options
99
# @return [String] HTML image tag
1010
#
1111
# Examples:
@@ -25,7 +25,7 @@ def fileboost_image_tag(asset, **options)
2525
# Generate an optimized URL using Fileboost
2626
#
2727
# @param asset [ActiveStorage::Blob, ActiveStorage::Attached, ActiveStorage::VariantWithRecord] The ActiveStorage image asset
28-
# @param options [Hash] Image transformation options
28+
# @param options [Hash] Supports a `:resize` hash for image transformations and an optional `:disposition`
2929
# @return [String, nil] The optimized URL or nil if generation failed
3030
#
3131
# Examples:

lib/fileboost/url_builder.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,20 @@ def self.build_url(asset, **options)
4545
# Extract and normalize transformation parameters
4646
transformation_params = extract_transformation_params(asset, options)
4747

48-
# Generate HMAC signature for secure authentication
48+
# Separate disposition from transformation params for signature generation
49+
disposition = transformation_params.delete("disposition")
50+
51+
# Generate HMAC signature for secure authentication (excluding disposition)
4952
signature = Fileboost::SignatureGenerator.generate(
5053
asset_path: asset_path,
5154
params: transformation_params
5255
)
5356

5457
raise SignatureGenerationError, "Failed to generate signature" unless signature
5558

56-
# Add signature to parameters
59+
# Add signature and disposition back to final URL parameters
5760
all_params = transformation_params.merge("sig" => signature)
61+
all_params["disposition"] = disposition if disposition
5862

5963
# Build final URL
6064
uri = URI.join(base_url, full_path)
@@ -118,6 +122,11 @@ def self.extract_transformation_params(asset, options)
118122
end
119123
end
120124

125+
disposition = options[:disposition] || options["disposition"]
126+
if disposition.present?
127+
params["disposition"] = disposition.to_s.strip.downcase
128+
end
129+
121130
params
122131
end
123132

spec/fileboost/helpers_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@
6060
expect(url).to include("fit=scale-down")
6161
end
6262

63+
it "includes disposition parameter when provided" do
64+
url = fileboost_url_for(blob, disposition: :attachment)
65+
66+
expect(url).to include("disposition=attachment")
67+
end
68+
6369
it "raises ArgumentError for invalid asset type" do
6470
expect {
6571
fileboost_url_for("invalid", resize: { w: 300 })

spec/fileboost/url_builder_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,5 +150,24 @@
150150
expect(url).to include("fit=scale-down")
151151
end
152152
end
153+
154+
context "with disposition parameter" do
155+
it "includes disposition in URL but excludes from signature generation" do
156+
# Generate URLs with and without disposition
157+
url_without_disposition = Fileboost::UrlBuilder.build_url(blob, resize: { w: 300 })
158+
url_with_disposition = Fileboost::UrlBuilder.build_url(blob, resize: { w: 300 }, disposition: "attachment")
159+
160+
# Extract signatures from both URLs
161+
sig_without = URI.decode_www_form(URI.parse(url_without_disposition).query).to_h["sig"]
162+
sig_with = URI.decode_www_form(URI.parse(url_with_disposition).query).to_h["sig"]
163+
164+
# Signatures should be identical (disposition not affecting signature)
165+
expect(sig_without).to eq(sig_with)
166+
167+
# But the URL with disposition should include the disposition parameter
168+
expect(url_with_disposition).to include("disposition=attachment")
169+
expect(url_without_disposition).not_to include("disposition=")
170+
end
171+
end
153172
end
154173
end

0 commit comments

Comments
 (0)