Close http connection in perform method of Request class (#6889)
HTTP connections must be explicitly closed in many cases, and letting perform method close connections makes its callers less redundant and prevent them from forgetting to close connections.
This commit is contained in:
parent
4e71b104e6
commit
54b273bf99
15 changed files with 134 additions and 127 deletions
|
@ -21,23 +21,23 @@ module Remotable
|
|||
return if !%w(http https).include?(parsed_url.scheme) || parsed_url.host.empty? || self[attribute_name] == url
|
||||
|
||||
begin
|
||||
response = Request.new(:get, url).perform
|
||||
Request.new(:get, url).perform do |response|
|
||||
next if response.code != 200
|
||||
|
||||
return if response.code != 200
|
||||
matches = response.headers['content-disposition']&.match(/filename="([^"]*)"/)
|
||||
filename = matches.nil? ? parsed_url.path.split('/').last : matches[1]
|
||||
basename = SecureRandom.hex(8)
|
||||
extname = if filename.nil?
|
||||
''
|
||||
else
|
||||
File.extname(filename)
|
||||
end
|
||||
|
||||
matches = response.headers['content-disposition']&.match(/filename="([^"]*)"/)
|
||||
filename = matches.nil? ? parsed_url.path.split('/').last : matches[1]
|
||||
basename = SecureRandom.hex(8)
|
||||
extname = if filename.nil?
|
||||
''
|
||||
else
|
||||
File.extname(filename)
|
||||
end
|
||||
send("#{attachment_name}=", StringIO.new(response.to_s))
|
||||
send("#{attachment_name}_file_name=", basename + extname)
|
||||
|
||||
send("#{attachment_name}=", StringIO.new(response.to_s))
|
||||
send("#{attachment_name}_file_name=", basename + extname)
|
||||
|
||||
self[attribute_name] = url if has_attribute?(attribute_name)
|
||||
self[attribute_name] = url if has_attribute?(attribute_name)
|
||||
end
|
||||
rescue HTTP::TimeoutError, HTTP::ConnectionError, OpenSSL::SSL::SSLError, Paperclip::Errors::NotIdentifiedByImageMagickError, Addressable::URI::InvalidURIError, Mastodon::HostValidationError => e
|
||||
Rails.logger.debug "Error fetching remote #{attachment_name}: #{e}"
|
||||
nil
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue