2024-02-25 12:15:55 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-04-25 20:15:09 -04:00
|
|
|
class IqdbQueriesController < ApplicationController
|
2018-06-20 14:53:44 -04:00
|
|
|
respond_to :html, :json
|
2023-05-07 09:08:10 -04:00
|
|
|
# Show uses POST because it needs a file parameter. This would be GET otherwise.
|
|
|
|
skip_forgery_protection only: :show
|
2023-06-13 15:46:18 -04:00
|
|
|
before_action :validate_enabled
|
2014-04-25 20:15:09 -04:00
|
|
|
|
2017-12-30 01:51:54 -05:00
|
|
|
def show
|
2023-05-07 08:48:58 -04:00
|
|
|
# Allow legacy ?post_id=123 parameters
|
|
|
|
search_params = params[:search].presence || params
|
|
|
|
throttle(search_params)
|
|
|
|
|
2023-05-07 09:08:10 -04:00
|
|
|
@matches = []
|
2023-05-07 08:48:58 -04:00
|
|
|
if search_params[:file].present?
|
|
|
|
@matches = IqdbProxy.query_file(search_params[:file].tempfile, search_params[:score_cutoff])
|
|
|
|
elsif search_params[:url].present?
|
|
|
|
parsed_url = Addressable::URI.heuristic_parse(search_params[:url]) rescue nil
|
2023-04-24 08:46:26 -04:00
|
|
|
raise User::PrivilegeError, "Invalid URL" unless parsed_url
|
|
|
|
whitelist_result = UploadWhitelist.is_whitelisted?(parsed_url)
|
|
|
|
raise User::PrivilegeError, "Not allowed to request content from this URL" unless whitelist_result[0]
|
2023-05-07 08:48:58 -04:00
|
|
|
@matches = IqdbProxy.query_url(search_params[:url], search_params[:score_cutoff])
|
|
|
|
elsif search_params[:post_id].present?
|
|
|
|
@matches = IqdbProxy.query_post(Post.find_by(id: search_params[:post_id]), search_params[:score_cutoff])
|
|
|
|
elsif search_params[:hash].present?
|
|
|
|
@matches = IqdbProxy.query_hash(search_params[:hash], search_params[:score_cutoff])
|
2018-06-19 19:46:22 -04:00
|
|
|
end
|
|
|
|
|
2018-06-23 13:32:39 -04:00
|
|
|
respond_with(@matches) do |fmt|
|
|
|
|
fmt.json do
|
2023-04-17 16:36:29 -04:00
|
|
|
render json: @matches, root: "posts"
|
2018-06-23 13:32:39 -04:00
|
|
|
end
|
|
|
|
end
|
2023-04-24 08:46:26 -04:00
|
|
|
rescue IqdbProxy::Error => e
|
2020-09-11 21:17:44 -04:00
|
|
|
render_expected_error(500, e.message)
|
2018-06-22 18:56:15 -04:00
|
|
|
end
|
2018-06-23 15:01:44 -04:00
|
|
|
|
2023-04-17 16:36:29 -04:00
|
|
|
private
|
|
|
|
|
2023-05-07 08:48:58 -04:00
|
|
|
def throttle(search_params)
|
|
|
|
return if Danbooru.config.disable_throttles?
|
2023-04-17 17:01:45 -04:00
|
|
|
|
2023-05-07 08:48:58 -04:00
|
|
|
if %i[file url post_id hash].any? { |key| search_params[key].present? }
|
|
|
|
if RateLimiter.check_limit("img:#{CurrentUser.ip_addr}", 1, 2.seconds)
|
2023-04-17 16:36:29 -04:00
|
|
|
raise APIThrottled
|
2019-11-28 15:27:06 -05:00
|
|
|
else
|
2023-04-17 16:36:29 -04:00
|
|
|
RateLimiter.hit("img:#{CurrentUser.ip_addr}", 2.seconds)
|
2019-11-28 15:27:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-06-13 15:46:18 -04:00
|
|
|
|
|
|
|
def validate_enabled
|
2024-04-26 09:57:56 -04:00
|
|
|
raise FeatureUnavailable unless IqdbProxy.enabled?
|
2023-06-13 15:46:18 -04:00
|
|
|
end
|
2017-04-05 01:37:05 -04:00
|
|
|
end
|