1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Uploading will set blob's byte size and checksum

This commit is contained in:
David Heinemeier Hansson 2017-07-01 14:25:02 +02:00
parent a9d2ce5d18
commit 59d3e03b81
2 changed files with 11 additions and 4 deletions

View file

@ -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

View file

@ -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