Refactor images filter

Rel: #633
pull/631/merge
Thibaut Courouble 8 years ago
parent c05822a8eb
commit 911909f679

@ -11,20 +11,22 @@ module Docs
@image_optim.optimize_image_data(data) @image_optim.optimize_image_data(data)
end end
def self.cache
@cache ||= {}
end
def call def call
return doc if context[:download_images] == false return doc if context[:download_images] == false
@@cache ||= {}
doc.css('img[src]').each do |node| doc.css('img[src]').each do |node|
src = node['src'] src = node['src']
if @@cache.key?(src) if self.class.cache.key?(src)
node['src'] = @@cache[src] unless @@cache[src] == false node['src'] = self.class.cache[src] unless self.class.cache[src] == false
next next
end end
@@cache[src] = false self.class.cache[src] = false
url = Docs::URL.parse(src) url = Docs::URL.parse(src)
url.scheme = 'https' if url.scheme.nil? url.scheme = 'https' if url.scheme.nil?
@ -57,7 +59,7 @@ module Docs
image = Base64.strict_encode64(image) image = Base64.strict_encode64(image)
image.prepend "data:#{response.mime_type};base64," image.prepend "data:#{response.mime_type};base64,"
node['src'] = @@cache[src] = image node['src'] = self.class.cache[src] = image
end end
rescue => exception rescue => exception
instrument 'error.image', url: url, exception: exception instrument 'error.image', url: url, exception: exception

Loading…
Cancel
Save