iqdb: fix non-jpeg files not working with direct file upload.

Convert non-JPEGs to JPEG before sending them to IQDB.
This commit is contained in:
evazion
2021-09-02 03:16:04 -05:00
parent 80bf54205c
commit ed600f4829

View File

@@ -21,8 +21,9 @@ class IqdbClient
similarity = similarity.to_f.clamp(0.0, 100.0) similarity = similarity.to_f.clamp(0.0, 100.0)
high_similarity = high_similarity.to_f.clamp(0.0, 100.0) high_similarity = high_similarity.to_f.clamp(0.0, 100.0)
if file.blank? if file.present?
if url.present? file = file.tempfile
elsif url.present?
file = download(url, :preview_url) file = download(url, :preview_url)
elsif image_url.present? elsif image_url.present?
file = download(image_url, :url) file = download(image_url, :url)
@@ -33,7 +34,6 @@ class IqdbClient
else else
return [[], [], []] return [[], [], []]
end end
end
results = query(file, limit: limit) results = query(file, limit: limit)
results = results.select { |result| result["score"] >= similarity }.take(limit) results = results.select { |result| result["score"] >= similarity }.take(limit)
@@ -53,7 +53,7 @@ class IqdbClient
strategy = Sources::Strategies.find(url) strategy = Sources::Strategies.find(url)
download_url = strategy.send(type) download_url = strategy.send(type)
file = strategy.download_file!(download_url) file = strategy.download_file!(download_url)
file.preview(Danbooru.config.small_image_width, Danbooru.config.small_image_width) file
end end
# Transform the JSON returned by IQDB to add the full post data for each # Transform the JSON returned by IQDB to add the full post data for each
@@ -83,7 +83,9 @@ class IqdbClient
# Search for an image in IQDB. # Search for an image in IQDB.
# @param file [File] the image to search # @param file [File] the image to search
def query(file, limit: 20) def query(file, limit: 20)
file = HTTP::FormData::File.new(file) media_file = MediaFile.open(file)
preview = media_file.preview(Danbooru.config.small_image_width, Danbooru.config.small_image_width)
file = HTTP::FormData::File.new(preview)
request(:post, "query", form: { file: file }, params: { limit: limit }) request(:post, "query", form: { file: file }, params: { limit: limit })
end end