diff --git a/lib/paperclip/storage/no_cache_s3.rb b/lib/paperclip/storage/no_cache_s3.rb index 8a0573d..6cdb79b 100644 --- a/lib/paperclip/storage/no_cache_s3.rb +++ b/lib/paperclip/storage/no_cache_s3.rb @@ -101,7 +101,9 @@ module Paperclip return unless synced_to?(self.class.main_store_id) if self.class.download_by_url - create_tempfile(URI.parse(presigned_url(style)).open.read) + URI.parse(presigned_url(style)).open do |tempfile| + create_tempfile(tempfile.read) + end else download_from_store(self.class.main_store_id, style_key) end diff --git a/test/storage/no_cache_s3_test.rb b/test/storage/no_cache_s3_test.rb index eb7242d..11dd57c 100644 --- a/test/storage/no_cache_s3_test.rb +++ b/test/storage/no_cache_s3_test.rb @@ -117,6 +117,27 @@ class NoCacheS3Test < Test::Unit::TestCase # Paperclip.expects(:log).with { puts "Log: #{_1}"; true }.at_least(3) assert_no_leftover_tmp { @instance.avatar.reprocess! } end + + context "with download_by_url" do + setup do + @instance.avatar.class.instance_variable_set(:@download_by_url, true) + @instance.avatar.stubs(:presigned_url).returns("http://example.com/some_file") # чтобы не стабать store.object.presigned_uri + require 'open-uri' + # правильнее было бы webmock притащить и сам запрос застабить, но ради одного теста жирновато + Net::HTTP.any_instance.stubs(:start).yields(nil) + resp = Net::HTTPSuccess.new(1.1, 200, 'ok') + str = @gif_pixel.dup + str.stubs(:clear) # чтобы не попортить вторым вызовом + # начиная с OpenURI::Buffer::StringMax open-uri генерит tempfile + resp.stubs(:read_body).multiple_yields(str, "\0" * OpenURI::Buffer::StringMax) + Net::HTTP.any_instance.stubs(:request).yields(resp) + end + + should "delete tmp files" do + @store1_stub.expects(:put_object).times(1 + (@instance.avatar.options[:styles].keys - [:original]).size) + assert_no_leftover_tmp { @instance.avatar.reprocess! } + end + end end context "with delayed_paperclip process_in_background" do # rubocop:disable Style/MultilineIfModifier