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,18 +21,18 @@ 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
file = download(url, :preview_url) elsif url.present?
elsif image_url.present? file = download(url, :preview_url)
file = download(image_url, :url) elsif image_url.present?
elsif file_url.present? file = download(image_url, :url)
file = download(file_url, :image_url) elsif file_url.present?
elsif post_id.present? file = download(file_url, :image_url)
file = Post.find(post_id).file(:preview) elsif post_id.present?
else file = Post.find(post_id).file(:preview)
return [[], [], []] else
end return [[], [], []]
end end
results = query(file, limit: limit) results = query(file, limit: 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