Fix #5045: Exception on uploads when SauceNAO is the referrer URL.
Bug: We assumed the referer URL was from the same site as the target URL. We tried to call methods on the referer only supported by the target URL. Fix: Ignore the referer URL when it's from a different site than the target URL.
This commit is contained in:
@@ -30,22 +30,25 @@ module Sources
|
|||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
# * <tt>url</tt> - Should point to a resource suitable for
|
# Extract information from a target URL. The target URL may be either a
|
||||||
# downloading. This may sometimes point to the binary file.
|
# direct image URL, or the URL of a HTML page containing one or more
|
||||||
# It may also point to the artist's profile page, in cases
|
# images.
|
||||||
# where this class is being used to normalize artist urls.
|
#
|
||||||
# Implementations should be smart enough to detect this and
|
# The referer URL is optionally provided when uploading direct image URLs
|
||||||
# behave accordingly.
|
# with the bookmarklet. This lets us find the page containing the image
|
||||||
# * <tt>referer_url</tt> - Sometimes the HTML page cannot be
|
# for sites like Twitter, where the image URL by itself doesn't have
|
||||||
# determined from <tt>url</tt>. You should generally pass in a
|
# enough information to find the page containing the image.
|
||||||
# <tt>referrer_url</tt> so the strategy can discover the HTML
|
#
|
||||||
# page and other information.
|
# @param url [String] The target URL
|
||||||
|
# @param referer_url [String] If the the target URL is an image URL, this
|
||||||
|
# should be the HTML page containing the image.
|
||||||
def initialize(url, referer_url = nil)
|
def initialize(url, referer_url = nil)
|
||||||
@url = url.to_s
|
@url = url.to_s
|
||||||
@referer_url = referer_url&.to_s
|
@referer_url = referer_url&.to_s
|
||||||
|
|
||||||
@parsed_url = Source::URL.parse(url)
|
@parsed_url = Source::URL.parse(url)
|
||||||
@parsed_referer = Source::URL.parse(referer_url) if referer_url.present?
|
@parsed_referer = Source::URL.parse(referer_url) if referer_url.present?
|
||||||
|
@parsed_referer = nil if parsed_url&.site_name != parsed_referer&.site_name
|
||||||
end
|
end
|
||||||
|
|
||||||
# Should return true if this strategy should be used. By default, checks
|
# Should return true if this strategy should be used. By default, checks
|
||||||
|
|||||||
@@ -36,7 +36,6 @@ module Sources
|
|||||||
end
|
end
|
||||||
|
|
||||||
def page_url
|
def page_url
|
||||||
# XXX what if referer isn't from NicoSeiga?
|
|
||||||
parsed_referer&.page_url || parsed_url.page_url
|
parsed_referer&.page_url || parsed_url.page_url
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ require "test_helper"
|
|||||||
module Sources
|
module Sources
|
||||||
class MoebooruTest < ActiveSupport::TestCase
|
class MoebooruTest < ActiveSupport::TestCase
|
||||||
def assert_source_data_equals(url, referer = nil, site_name: nil, image_url: nil, page_url: nil, size: nil, tags: [], profile_url: nil, **params)
|
def assert_source_data_equals(url, referer = nil, site_name: nil, image_url: nil, page_url: nil, size: nil, tags: [], profile_url: nil, **params)
|
||||||
site = Sources::Strategies.find(url)
|
site = Sources::Strategies.find(url, referer)
|
||||||
|
|
||||||
assert_equal(site_name, site.site_name)
|
assert_equal(site_name, site.site_name)
|
||||||
assert_equal([image_url], site.image_urls)
|
assert_equal([image_url], site.image_urls)
|
||||||
@@ -71,6 +71,20 @@ module Sources
|
|||||||
assert_source_data_equals(@full, **@data)
|
assert_source_data_equals(@full, **@data)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "When the referer URL is SauceNao" do
|
||||||
|
should "ignore the referer" do
|
||||||
|
@url = "https://yande.re/post/show/469929"
|
||||||
|
@ref = "https://saucenao.com/"
|
||||||
|
|
||||||
|
assert_source_data_equals(@url, @ref,
|
||||||
|
site_name: "Yande.re",
|
||||||
|
image_url: "https://files.yande.re/image/36b031b266605d89aed2b62d479e64b1/yande.re%20469929.jpg",
|
||||||
|
page_url: "https://yande.re/post/show/469929",
|
||||||
|
tags: %w[anchovy bandages darjeeling girls_und_panzer katyusha kay_(girls_und_panzer) mika_(girls_und_panzer) nishi_kinuyo nishizumi_maho nishizumi_miho shimada_arisu uniform],
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "Konachan.com:" do
|
context "Konachan.com:" do
|
||||||
|
|||||||
Reference in New Issue
Block a user