aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_storage/blob.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2017-07-06 15:38:01 +0200
committerDavid Heinemeier Hansson <david@loudthinking.com>2017-07-06 15:38:01 +0200
commit152c4b07248d4aed4b734721bd634e546a89ef19 (patch)
treeda0003f340d2294a14d15cc2bc7773c935600298 /lib/active_storage/blob.rb
parent6de714a0ea755caafe5758e232582573ac9966a4 (diff)
downloadrails-152c4b07248d4aed4b734721bd634e546a89ef19.tar.gz
rails-152c4b07248d4aed4b734721bd634e546a89ef19.tar.bz2
rails-152c4b07248d4aed4b734721bd634e546a89ef19.zip
Compute checksum and byte_size client side
Then we can add integrity checks on uploads to prevent errors in transport.
Diffstat (limited to 'lib/active_storage/blob.rb')
-rw-r--r--lib/active_storage/blob.rb18
1 files changed, 15 insertions, 3 deletions
diff --git a/lib/active_storage/blob.rb b/lib/active_storage/blob.rb
index 4ce344e2a1..b10dc2c771 100644
--- a/lib/active_storage/blob.rb
+++ b/lib/active_storage/blob.rb
@@ -42,10 +42,10 @@ class ActiveStorage::Blob < ActiveRecord::Base
def upload(io)
- service.upload(key, io)
+ self.checksum = compute_checksum_in_chunks(io)
+ self.byte_size = io.size
- self.checksum = service.checksum(key)
- self.byte_size = service.byte_size(key)
+ service.upload(key, io)
end
def download
@@ -65,4 +65,16 @@ class ActiveStorage::Blob < ActiveRecord::Base
def purge_later
ActiveStorage::PurgeJob.perform_later(self)
end
+
+
+ private
+ def compute_checksum_in_chunks(io)
+ Digest::MD5.new.tap do |checksum|
+ while chunk = io.read(5.megabytes)
+ checksum << chunk
+ end
+
+ io.rewind
+ end.base64digest
+ end
end