Skip to content

Commit

Permalink
Merge pull request #94 from insales/close_more_files
Browse files Browse the repository at this point in the history
Close file opened by open-uri
  • Loading branch information
Vasfed authored Apr 9, 2024
2 parents 3e12446 + 619b3d0 commit d7cf90f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/paperclip/storage/no_cache_s3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ def to_file(style = default_style, store_id = nil)
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
Expand Down
21 changes: 21 additions & 0 deletions test/storage/no_cache_s3_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,27 @@ def assert_no_leftover_tmp
# 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
Expand Down

0 comments on commit d7cf90f

Please sign in to comment.