iqdb: proxy iqdb searches through danbooru.

Previously the search form on the /iqdb_queries page submitted directly
to the iqdb service (karasuma.donmai.us), which redirected back to
Danbooru with the search results.

This was different than API requests, which submitted to
/iqdb_queries.json which proxied the call to iqdb through Danbooru.
Because of this, searches on the /iqdb_queries page had different
behavior than API requests. Things like filesize limits and referrer
spoofing were handled differently.

Now searches on the /iqdb_queries page submit directly to Danbooru. This
is simpler and it means that API requests and HTML requests have the
same behavior.
This commit is contained in:
evazion
2019-10-14 21:16:04 -05:00
parent f7116ad1c4
commit ae521e600e
5 changed files with 43 additions and 34 deletions

View File

@@ -1,15 +1,37 @@
class IqdbProxy
def self.query(url, limit, similarity)
def self.search(params)
raise NotImplementedError unless Danbooru.config.iqdbs_server.present?
limit ||= 20
similarity ||= 0.0
limit = params[:limit].presence || 20
limit = limit.to_i.clamp(1, 100)
similarity = params[:similarity].to_f.clamp(0.0, 100.0)
if params[:file].present?
results = query_file(params[:file], limit)
elsif params[:url].present?
url = Sources::Strategies.find(params[:url]).image_url
results = query_url(url, limit)
elsif params[:post_id].present?
url = Post.find(params[:post_id]).preview_file_url
results = query_url(url, limit)
else
results = []
end
results = results.select { |result| result["score"] >= similarity }.take(limit)
decorate_posts(results)
end
def self.query_url(url, limit)
query = { url: url, limit: limit }
response = HTTParty.get("#{Danbooru.config.iqdbs_server}/similar", query: query, **Danbooru.config.httparty_options)
response.parsed_response
end
json = decorate_posts(response.parsed_response)
json = json.select { |result| result["score"] >= similarity.to_f }.take(limit.to_i)
json
def self.query_file(file, limit)
body = { file: file, limit: limit }
response = HTTParty.post("#{Danbooru.config.iqdbs_server}/similar", body: body, **Danbooru.config.httparty_options)
response.parsed_response
end
def self.decorate_posts(json)