Skip to content

Commit

Permalink
[Validator] Handle Rails 6.1.4 not supporting blob#download_chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
Mth0158 committed Dec 10, 2024
1 parent 70c8b67 commit 2ae166e
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/active_storage_validations/shared/asv_attachable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ def attachable_content_type_rails_like(attachable)
def attachable_io(attachable, max_byte_size: nil)
io = case attachable
when ActiveStorage::Blob
max_byte_size ? attachable.download_chunk(0...max_byte_size) : attachable.download
(max_byte_size && supports_blob_download_chunk?) ? attachable.download_chunk(0...max_byte_size) : attachable.download
when ActionDispatch::Http::UploadedFile
max_byte_size ? attachable.read(max_byte_size) : attachable.read
when Rack::Test::UploadedFile
max_byte_size ? attachable.read(max_byte_size) : attachable.read
when String
blob = ActiveStorage::Blob.find_signed!(attachable)
max_byte_size ? blob.download_chunk(0...max_byte_size) : blob.download
(max_byte_size && supports_blob_download_chunk?) ? blob.download_chunk(0...max_byte_size) : blob.download
when Hash
max_byte_size ? attachable[:io].read(max_byte_size) : attachable[:io].read
when File
Expand Down Expand Up @@ -163,6 +163,13 @@ def supports_file_attachment?
end
alias :supports_pathname_attachment? :supports_file_attachment?

# Check if the current Rails version supports ActiveStorage::Blob#download_chunk
#
# https://github.com/rails/rails/blob/7-0-stable/activestorage/CHANGELOG.md#rails-700alpha1-september-15-2021
def supports_blob_download_chunk?
Rails.gem_version >= Gem::Version.new('7.0.0.alpha1')
end

# Retrieve the content_type from the file name only
def marcel_content_type_from_filename(attachable)
Marcel::MimeType.for(name: attachable_filename(attachable).to_s)
Expand Down

0 comments on commit 2ae166e

Please sign in to comment.