From 0d4ba40688785ad65da1af36b3ad8efd8e14505e Mon Sep 17 00:00:00 2001 From: Marcelo Perini Veloso Date: Wed, 5 Sep 2018 00:21:44 -0300 Subject: [PATCH] Fix zero-byte files upload --- .../app/models/active_storage/blob/identifiable.rb | 6 +++++- activestorage/test/fixtures/files/empty_file.txt | 0 activestorage/test/service/s3_service_test.rb | 7 +++++++ 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 activestorage/test/fixtures/files/empty_file.txt diff --git a/activestorage/app/models/active_storage/blob/identifiable.rb b/activestorage/app/models/active_storage/blob/identifiable.rb index 049e45dc3e..2c17ddc25f 100644 --- a/activestorage/app/models/active_storage/blob/identifiable.rb +++ b/activestorage/app/models/active_storage/blob/identifiable.rb @@ -15,6 +15,10 @@ module ActiveStorage::Blob::Identifiable end def download_identifiable_chunk - service.download_chunk key, 0...4.kilobytes + if byte_size.positive? + service.download_chunk key, 0...4.kilobytes + else + "" + end end end diff --git a/activestorage/test/fixtures/files/empty_file.txt b/activestorage/test/fixtures/files/empty_file.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/activestorage/test/service/s3_service_test.rb b/activestorage/test/service/s3_service_test.rb index 4bfcda017f..559aa028f2 100644 --- a/activestorage/test/service/s3_service_test.rb +++ b/activestorage/test/service/s3_service_test.rb @@ -31,6 +31,13 @@ if SERVICE_CONFIGURATIONS[:s3] end end + test "upload a zero byte file" do + blob = directly_upload_file_blob filename: "empty_file.txt", content_type: nil + user = User.create! name: "DHH", avatar: blob + + assert_equal user.avatar.blob, blob + end + test "signed URL generation" do url = @service.url(@key, expires_in: 5.minutes, disposition: :inline, filename: ActiveStorage::Filename.new("avatar.png"), content_type: "image/png")