Skip to content

Commit f5acef7

Browse files
committed
Fix AS:Representations::ProxyController returning the wrong preview
When a blob is a representable of kind `previewable`, the preview image that's being proxied is always the original preview image, discarding completely the `variation_key` param passed in the request. This commit fixes this by editing `Preview` and `VariantWithRecord` to have full synchronized API with `Variant`. this will then allow the ProxyController to not call `representable#image` but `representable` instead. As all 3 classes are now 100% interchangeable, we can deprecate the use of `Representable#image`. Users of this class won't need to call this method as it's become obsolete. and in case of `Preview#image` erroneous.
1 parent 3d2e960 commit f5acef7

File tree

5 files changed

+28
-33
lines changed

5 files changed

+28
-33
lines changed

activestorage/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* Fix `ActiveStorage::Representations::ProxyController` not returning the proper
2+
preview image variant for previewable files.
3+
4+
*Chedli Bourguiba*
5+
16
* Fix `ActiveStorage::Representations::ProxyController` to proxy untracked
27
variants.
38

activestorage/app/controllers/active_storage/representations/proxy_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class ActiveStorage::Representations::ProxyController < ActiveStorage::Represent
1212

1313
def show
1414
http_cache_forever public: true do
15-
send_blob_stream @representation.image, disposition: params[:disposition]
15+
send_blob_stream @representation, disposition: params[:disposition]
1616
end
1717
end
1818
end

activestorage/app/models/active_storage/preview.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@
3131
# These libraries are not provided by \Rails. You must install them yourself to use the built-in previewers. Before you
3232
# install and use third-party software, make sure you understand the licensing implications of doing so.
3333
class ActiveStorage::Preview
34+
include ActiveStorage::Blob::Servable
35+
3436
class UnprocessedError < StandardError; end
3537

38+
delegate :filename, :content_type, to: :presentation
39+
3640
attr_reader :blob, :variation
3741

3842
def initialize(blob, variation_or_variation_key)

activestorage/app/models/active_storage/variant_with_record.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
# Like an ActiveStorage::Variant, but keeps detail about the variant in the database as an
66
# ActiveStorage::VariantRecord. This is only used if +ActiveStorage.track_variants+ is enabled.
77
class ActiveStorage::VariantWithRecord
8+
include ActiveStorage::Blob::Servable
9+
810
attr_reader :blob, :variation
911
delegate :service, to: :blob
1012
delegate :content_type, to: :variation

activestorage/test/controllers/representations/proxy_controller_test.rb

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ class ActiveStorage::Representations::ProxyControllerWithVariantsTest < ActionDi
1818

1919
assert_response :ok
2020
assert_match(/^attachment/, response.headers["Content-Disposition"])
21-
22-
image = read_image(@blob.variant(@transformations))
23-
assert_equal 100, image.width
24-
assert_equal 67, image.height
21+
assert_equal @blob.variant(@transformations).download, response.body
2522
end
2623

2724
test "showing variant inline" do
@@ -32,10 +29,7 @@ class ActiveStorage::Representations::ProxyControllerWithVariantsTest < ActionDi
3229

3330
assert_response :ok
3431
assert_match(/^inline/, response.headers["Content-Disposition"])
35-
36-
image = read_image(@blob.variant(@transformations))
37-
assert_equal 100, image.width
38-
assert_equal 67, image.height
32+
assert_equal @blob.variant(@transformations).download, response.body
3933
end
4034

4135
test "showing untracked variant" do
@@ -48,7 +42,7 @@ class ActiveStorage::Representations::ProxyControllerWithVariantsTest < ActionDi
4842

4943
assert_response :ok
5044
assert_match(/^attachment/, response.headers["Content-Disposition"])
51-
assert_equal @blob.representation(@transformations).download, response.body
45+
assert_equal @blob.variant(@transformations).download, response.body
5246
end
5347
end
5448

@@ -74,52 +68,46 @@ class ActiveStorage::Representations::ProxyControllerWithVariantsTest < ActionDi
7468
class ActiveStorage::Representations::ProxyControllerWithVariantsWithStrictLoadingTest < ActionDispatch::IntegrationTest
7569
setup do
7670
@blob = create_file_blob filename: "racecar.jpg"
77-
@blob.variant(resize_to_limit: [100, 100]).processed
71+
@transformations = { resize_to_limit: [100, 100] }
72+
@blob.variant(@transformations).processed
7873
end
7974

8075
test "showing existing variant record" do
8176
with_strict_loading_by_default do
8277
get rails_blob_representation_proxy_url(
8378
filename: @blob.filename,
8479
signed_blob_id: @blob.signed_id,
85-
variation_key: ActiveStorage::Variation.encode(resize_to_limit: [100, 100]))
80+
variation_key: ActiveStorage::Variation.encode(@transformations))
8681
end
82+
8783
assert_response :ok
8884
assert_match(/^inline/, response.headers["Content-Disposition"])
89-
90-
@blob.reload # became free of strict_loading?
91-
image = read_image(@blob.variant(resize_to_limit: [100, 100]))
92-
assert_equal 100, image.width
93-
assert_equal 67, image.height
85+
assert_equal @blob.variant(@transformations).download, response.body
9486
end
9587
end
9688

9789
class ActiveStorage::Representations::ProxyControllerWithPreviewsTest < ActionDispatch::IntegrationTest
9890
setup do
9991
@blob = create_file_blob filename: "report.pdf", content_type: "application/pdf"
92+
@transformations = { resize_to_limit: [100, 100] }
10093
end
10194

10295
test "showing preview inline" do
10396
get rails_blob_representation_proxy_url(
10497
filename: @blob.filename,
10598
signed_blob_id: @blob.signed_id,
106-
variation_key: ActiveStorage::Variation.encode(resize_to_limit: [100, 100]))
99+
variation_key: ActiveStorage::Variation.encode(@transformations))
107100

108101
assert_response :ok
109102
assert_match(/^inline/, response.headers["Content-Disposition"])
110-
111-
assert_predicate @blob.preview_image, :attached?
112-
113-
image = read_image(@blob.preview_image.variant(resize_to_limit: [100, 100]).processed)
114-
assert_equal 77, image.width
115-
assert_equal 100, image.height
103+
assert_equal @blob.preview(@transformations).download, response.body
116104
end
117105

118106
test "showing preview with invalid signed blob ID" do
119107
get rails_blob_representation_proxy_url(
120108
filename: @blob.filename,
121109
signed_blob_id: "invalid",
122-
variation_key: ActiveStorage::Variation.encode(resize_to_limit: [100, 100]))
110+
variation_key: ActiveStorage::Variation.encode(@transformations))
123111

124112
assert_response :not_found
125113
end
@@ -137,24 +125,20 @@ class ActiveStorage::Representations::ProxyControllerWithPreviewsTest < ActionDi
137125
class ActiveStorage::Representations::ProxyControllerWithPreviewsWithStrictLoadingTest < ActionDispatch::IntegrationTest
138126
setup do
139127
@blob = create_file_blob filename: "report.pdf", content_type: "application/pdf"
140-
@blob.preview(resize_to_limit: [100, 100]).processed
128+
@transformations = { resize_to_limit: [100, 100] }
129+
@blob.preview(@transformations).processed
141130
end
142131

143132
test "showing existing preview record" do
144133
with_strict_loading_by_default do
145134
get rails_blob_representation_proxy_url(
146135
filename: @blob.filename,
147136
signed_blob_id: @blob.signed_id,
148-
variation_key: ActiveStorage::Variation.encode(resize_to_limit: [100, 100]))
137+
variation_key: ActiveStorage::Variation.encode(@transformations))
149138
end
150139

151140
assert_response :ok
152141
assert_match(/^inline/, response.headers["Content-Disposition"])
153-
@blob.reload # became free of strict_loading?
154-
assert_predicate @blob.preview_image, :attached?
155-
156-
image = read_image(@blob.preview_image.variant(resize_to_limit: [100, 100]).processed)
157-
assert_equal 77, image.width
158-
assert_equal 100, image.height
142+
assert_equal @blob.preview(@transformations).download, response.body
159143
end
160144
end

0 commit comments

Comments
 (0)