sources: remove preview_urls method from base strategy.

Remove the `preview_urls` method from strategies. The only place this was used was
when doing IQDB searches, to download the thumbnail image from the source instead of
the full image.

This wasn't worth it for a few reasons:

* Thumbnails on other sites are sometimes not the size we want, which could affect
  IQDB results.
* Grabbing thumbnails is complex for some sites. You can't always just rewrite the
  image URL. Sometimes it requires extra API calls, which can be slower than just
  grabbing the full image.
* For videos and animations, thumbnails from other sites don't always match our
  thumbnails. We do smart thumbnail generation to try to avoid blank thumbnails, which
  means we don't always pick the first frame, which could affect IQDB results.

API changes:

* /iqdb_queries?search[file_url] now downloads the URL as is without any modification.
  Before it tried to change thumbnail and sample size image URLs to the full version.

* /iqdb_queries?search[url] now returns an error if the URL is for a HTML page that
  contains multiple images. Before it would grab only the first image and silently
  ignore the rest.
This commit is contained in:
evazion
2022-03-11 02:54:26 -06:00
parent 2f61486ac6
commit b4aea72d04
18 changed files with 12 additions and 171 deletions

View File

@@ -30,11 +30,15 @@ class IqdbClient
if file.present?
file = file.tempfile
elsif url.present?
file = download(url, :preview_url)
strategy = Sources::Strategies.find(url)
raise Error, "Can't do reverse image search: #{url} has multiple images. Enter the URL of a single image." if strategy.image_urls.size > 1
download_url = strategy.image_urls.first
file = Sources::Strategies.find(download_url).download_file!(download_url)
elsif image_url.present?
file = download(image_url, :url)
file = Sources::Strategies.find(image_url).download_file!(image_url)
elsif file_url.present?
file = download(file_url, :image_url)
file = Sources::Strategies.find(file_url).download_file!(file_url)
elsif post_id.present?
file = Post.find(post_id).file(:preview)
elsif media_asset_id.present?
@@ -54,17 +58,6 @@ class IqdbClient
file.try(:close)
end
# Download an URL to a file.
# @param url [String] the URL to download
# @param type [Symbol] the type of URL to download (:preview_url or full :image_url)
# @return [MediaFile] the downloaded file
def download(url, type)
strategy = Sources::Strategies.find(url)
download_url = strategy.send(type)
file = strategy.download_file!(download_url)
file
end
# Transform the JSON returned by IQDB to add the full post data for each
# match.
# @param matches [Array<Hash>] the array of IQDB matches