Skip to content

Commit 5d0ab55

Browse files
Merge pull request rails#50107 from chaadow/fix_proxy_controller_untracked_variants
[ActiveStorage] Fix Non tracked variants not working with `ActiveStorage::Representations::ProxyController`
2 parents 0ad26f7 + cb3fdaf commit 5d0ab55

File tree

5 files changed

+50
-23
lines changed

5 files changed

+50
-23
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` to proxy untracked
2+
variants.
3+
4+
*Chedli Bourguiba*
5+
16
* When using the `preprocessed: true` option, avoid enqueuing transform jobs
27
for blobs that are not representable.
38

activestorage/app/models/active_storage/blob.rb

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ def validate_global_service_configuration # :nodoc:
175175
include Analyzable
176176
include Identifiable
177177
include Representable
178+
include Servable
178179

179180
# Returns a signed ID for this blob that's suitable for reference on the client-side without fear of tampering.
180181
def signed_id(purpose: :blob_id, expires_in: nil, expires_at: nil)
@@ -245,16 +246,6 @@ def service_headers_for_direct_upload
245246
service.headers_for_direct_upload key, filename: filename, content_type: content_type, content_length: byte_size, checksum: checksum, custom_metadata: custom_metadata
246247
end
247248

248-
def content_type_for_serving # :nodoc:
249-
forcibly_serve_as_binary? ? ActiveStorage.binary_content_type : content_type
250-
end
251-
252-
def forced_disposition_for_serving # :nodoc:
253-
if forcibly_serve_as_binary? || !allowed_inline?
254-
:attachment
255-
end
256-
end
257-
258249

259250
# Uploads the +io+ to the service on the +key+ for this blob. Blobs are intended to be immutable, so you shouldn't be
260251
# using this method after a file has already been uploaded to fit with a blob. If you want to create a derivative blob,
@@ -373,14 +364,6 @@ def extract_content_type(io)
373364
Marcel::MimeType.for io, name: filename.to_s, declared_type: content_type
374365
end
375366

376-
def forcibly_serve_as_binary?
377-
ActiveStorage.content_types_to_serve_as_binary.include?(content_type)
378-
end
379-
380-
def allowed_inline?
381-
ActiveStorage.content_types_allowed_inline.include?(content_type)
382-
end
383-
384367
def web_image?
385368
ActiveStorage.web_image_content_types.include?(content_type)
386369
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
module ActiveStorage::Blob::Servable # :nodoc:
4+
def content_type_for_serving
5+
forcibly_serve_as_binary? ? ActiveStorage.binary_content_type : content_type
6+
end
7+
8+
def forced_disposition_for_serving
9+
if forcibly_serve_as_binary? || !allowed_inline?
10+
:attachment
11+
end
12+
end
13+
14+
private
15+
def forcibly_serve_as_binary?
16+
ActiveStorage.content_types_to_serve_as_binary.include?(content_type)
17+
end
18+
19+
def allowed_inline?
20+
ActiveStorage.content_types_allowed_inline.include?(content_type)
21+
end
22+
end

activestorage/app/models/active_storage/variant.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
# * {ImageProcessing::Vips}[https://github.com/janko/image_processing/blob/master/doc/vips.md#methods]
5454
# * {ruby-vips reference}[http://www.rubydoc.info/gems/ruby-vips/Vips/Image]
5555
class ActiveStorage::Variant
56+
include ActiveStorage::Blob::Servable
57+
5658
attr_reader :blob, :variation
5759
delegate :service, to: :blob
5860
delegate :content_type, to: :variation

activestorage/test/controllers/representations/proxy_controller_test.rb

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@
66
class ActiveStorage::Representations::ProxyControllerWithVariantsTest < ActionDispatch::IntegrationTest
77
setup do
88
@blob = create_file_blob filename: "racecar.jpg"
9+
@transformations = { resize_to_limit: [100, 100] }
910
end
1011

1112
test "showing variant attachment" do
1213
get rails_blob_representation_proxy_url(
1314
disposition: :attachment,
1415
filename: @blob.filename,
1516
signed_blob_id: @blob.signed_id,
16-
variation_key: ActiveStorage::Variation.encode(resize_to_limit: [100, 100]))
17+
variation_key: ActiveStorage::Variation.encode(@transformations))
1718

1819
assert_response :ok
1920
assert_match(/^attachment/, response.headers["Content-Disposition"])
2021

21-
image = read_image(@blob.variant(resize_to_limit: [100, 100]))
22+
image = read_image(@blob.variant(@transformations))
2223
assert_equal 100, image.width
2324
assert_equal 67, image.height
2425
end
@@ -27,21 +28,35 @@ class ActiveStorage::Representations::ProxyControllerWithVariantsTest < ActionDi
2728
get rails_blob_representation_proxy_url(
2829
filename: @blob.filename,
2930
signed_blob_id: @blob.signed_id,
30-
variation_key: ActiveStorage::Variation.encode(resize_to_limit: [100, 100]))
31+
variation_key: ActiveStorage::Variation.encode(@transformations))
3132

3233
assert_response :ok
3334
assert_match(/^inline/, response.headers["Content-Disposition"])
3435

35-
image = read_image(@blob.variant(resize_to_limit: [100, 100]))
36+
image = read_image(@blob.variant(@transformations))
3637
assert_equal 100, image.width
3738
assert_equal 67, image.height
3839
end
3940

41+
test "showing untracked variant" do
42+
without_variant_tracking do
43+
get rails_blob_representation_proxy_url(
44+
disposition: :attachment,
45+
filename: @blob.filename,
46+
signed_blob_id: @blob.signed_id,
47+
variation_key: ActiveStorage::Variation.encode(@transformations))
48+
49+
assert_response :ok
50+
assert_match(/^attachment/, response.headers["Content-Disposition"])
51+
assert_equal @blob.representation(@transformations).download, response.body
52+
end
53+
end
54+
4055
test "showing variant with invalid signed blob ID" do
4156
get rails_blob_representation_proxy_url(
4257
filename: @blob.filename,
4358
signed_blob_id: "invalid",
44-
variation_key: ActiveStorage::Variation.encode(resize_to_limit: [100, 100]))
59+
variation_key: ActiveStorage::Variation.encode(@transformations))
4560

4661
assert_response :not_found
4762
end

0 commit comments

Comments
 (0)