2021-05-26 04:41:35 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Onebox
|
|
|
|
class Preview
|
|
|
|
# see https://bugs.ruby-lang.org/issues/14688
|
2023-01-09 06:10:19 -06:00
|
|
|
client_exception =
|
|
|
|
defined?(Net::HTTPClientException) ? Net::HTTPClientException : Net::HTTPServerException
|
|
|
|
WEB_EXCEPTIONS ||= [
|
|
|
|
client_exception,
|
|
|
|
OpenURI::HTTPError,
|
|
|
|
Timeout::Error,
|
|
|
|
Net::HTTPError,
|
|
|
|
Errno::ECONNREFUSED,
|
|
|
|
]
|
2021-05-26 04:41:35 -05:00
|
|
|
|
|
|
|
def initialize(url, options = Onebox.options)
|
|
|
|
@url = url
|
|
|
|
@options = options.dup
|
|
|
|
|
|
|
|
allowed_origins = @options[:allowed_iframe_origins] || Onebox::Engine.all_iframe_origins
|
|
|
|
@options[:allowed_iframe_regexes] = Engine.origins_to_regexes(allowed_origins)
|
|
|
|
|
|
|
|
@engine_class = Matcher.new(@url, @options).oneboxed
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
return "" unless engine
|
|
|
|
sanitize process_html engine_html
|
|
|
|
rescue *WEB_EXCEPTIONS
|
|
|
|
""
|
|
|
|
end
|
|
|
|
|
|
|
|
def placeholder_html
|
|
|
|
return "" unless engine
|
|
|
|
sanitize process_html engine.placeholder_html
|
|
|
|
rescue *WEB_EXCEPTIONS
|
|
|
|
""
|
|
|
|
end
|
|
|
|
|
|
|
|
def errors
|
|
|
|
return {} unless engine
|
|
|
|
engine.errors
|
|
|
|
end
|
|
|
|
|
|
|
|
def data
|
|
|
|
return {} unless engine
|
|
|
|
engine.data
|
|
|
|
end
|
|
|
|
|
2021-06-01 15:23:18 -05:00
|
|
|
def verified_data
|
|
|
|
return {} unless engine
|
|
|
|
engine.verified_data
|
|
|
|
end
|
|
|
|
|
2021-05-26 04:41:35 -05:00
|
|
|
def options
|
|
|
|
OpenStruct.new(@options)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def engine_html
|
|
|
|
engine.to_html
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_html(html)
|
|
|
|
return "" unless html
|
|
|
|
|
|
|
|
if @options[:max_width]
|
2023-01-09 06:10:19 -06:00
|
|
|
doc = Nokogiri::HTML5.fragment(html)
|
2021-05-26 04:41:35 -05:00
|
|
|
if doc
|
2023-01-09 06:10:19 -06:00
|
|
|
doc
|
|
|
|
.css("[width]")
|
|
|
|
.each do |e|
|
|
|
|
width = e["width"].to_i
|
|
|
|
|
|
|
|
if width > @options[:max_width]
|
|
|
|
height = e["height"].to_i
|
|
|
|
if (height > 0)
|
|
|
|
ratio = (height.to_f / width.to_f)
|
|
|
|
e["height"] = (@options[:max_width] * ratio).floor
|
|
|
|
end
|
|
|
|
e["width"] = @options[:max_width]
|
2021-05-26 04:41:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return doc.to_html
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
html
|
|
|
|
end
|
|
|
|
|
|
|
|
def sanitize(html)
|
2022-04-06 08:19:41 -05:00
|
|
|
config = @options[:sanitize_config] || SanitizeConfig::ONEBOX
|
2021-05-26 04:41:35 -05:00
|
|
|
config = config.merge(allowed_iframe_regexes: @options[:allowed_iframe_regexes])
|
|
|
|
|
|
|
|
Sanitize.fragment(html, config)
|
|
|
|
end
|
|
|
|
|
|
|
|
def engine
|
|
|
|
return nil unless @engine_class
|
|
|
|
return @engine if defined?(@engine)
|
|
|
|
|
|
|
|
@engine = @engine_class.new(@url)
|
|
|
|
@engine.options = @options
|
|
|
|
@engine
|
|
|
|
end
|
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
class InvalidURI < StandardError
|
|
|
|
end
|
2021-05-26 04:41:35 -05:00
|
|
|
end
|
|
|
|
end
|