From 59d3e03b81d09c70fc1249514c1848e701757513 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 1 Jul 2017 14:25:02 +0200 Subject: [PATCH] Uploading will set blob's byte size and checksum --- lib/active_file/blob.rb | 5 ++++- test/blob_test.rb | 10 +++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/active_file/blob.rb b/lib/active_file/blob.rb index 5ed2fce5ac..b5d149e1fa 100644 --- a/lib/active_file/blob.rb +++ b/lib/active_file/blob.rb @@ -39,7 +39,10 @@ class ActiveFile::Blob < ActiveRecord::Base def upload(data) - site.upload key, data + site.upload(key, data) + + self.checksum = site.checksum(key) + self.byte_size = site.byte_size(key) end def download diff --git a/test/blob_test.rb b/test/blob_test.rb index ad2df51ca9..04d636e189 100644 --- a/test/blob_test.rb +++ b/test/blob_test.rb @@ -5,8 +5,12 @@ require "active_file/blob" ActiveFile::Blob.site = ActiveFile::Sites::DiskSite.new(File.join(Dir.tmpdir, "active_file")) class ActiveFile::BlobTest < ActiveSupport::TestCase - test "create after upload" do - blob = ActiveFile::Blob.create_after_upload! data: StringIO.new("Hello world!"), filename: "hello.txt", content_type: "text/plain" - assert_equal "Hello world!", blob.download + test "create after upload sets byte size and checksum" do + data = "Hello world!" + blob = ActiveFile::Blob.create_after_upload! data: StringIO.new(data), filename: "hello.txt", content_type: "text/plain" + + assert_equal data, blob.download + assert_equal data.length, blob.byte_size + assert_equal Digest::MD5.hexdigest(data), blob.checksum end end