Websites/app/controllers/yiff_rest/api_v2/images_controller.rb
2024-10-24 02:25:31 -05:00

155 lines
5.0 KiB
Ruby

# frozen_string_literal: true
module YiffRest
module APIV2
class ImagesController < ApplicationController
before_action :validate_discord
before_action :prepare_user
before_action :admin_only
before_action :load_image, only: %i[edit update destroy delete_with_reason toggle_viewable convert update_cache]
def index
@sp = search_images_params
@pagy, @images = pagy(APIImage.search(@sp).order(created_at: :desc), size: [1, 2, 2, 1])
end
def new
@image = APIImage.new(create_params)
@categories = APIImage.categories
end
def new_external
@categories = APIImage.categories
end
def edit
@image = APIImage.find(params[:id])
@categories = APIImage.categories
end
def create
@service = APIImageUploadService.new(create_params)
@image = @service.start!
@categories = APIImage.categories
if @image.invalid?
flash.now[:alert] = @image.errors.full_messages.join(", ")
return render(:new)
end
redirect_to(yiff_rest_api_v2_manage_images_path(search: { category: @image.category }), notice: "Image added")
end
def create_external
pp = create_external_params
@external = APIImage.create_external!(pp[:site], pp[:external_id], pp[:category])
@image = @external.api_image
redirect_to(yiff_rest_api_v2_manage_images_path(search: { category: @image.category }), notice: "Image added")
end
def update
@image = APIImage.find(params[:id])
@image.update(update_params)
@categories = APIImage.categories
if @image.invalid?
flash.now[:alert] = @image.errors.full_messages.join(", ")
return render(:edit)
end
redirect_to(yiff_rest_api_v2_manage_images_path(search: { category: @image.category }), notice: "Image updated")
end
def destroy
@image = APIImage.find(params[:id])
@image.deletion_reason = params.dig(:api_image, :deletion_reason)
@image.destroy
redirect_to(yiff_rest_api_v2_manage_images_path(search: { category: @image.category }), notice: "Image deleted")
end
def delete_with_reason
# deletion_reason
@image = APIImage.find(params[:id])
end
def toggle_viewable
@image.update(is_viewable: !@image.is_viewable?)
redirect_back(fallback_location: yiff_rest_api_v2_manage_images_path, notice: "Image is #{@image.is_viewable? ? 'now' : 'no longer'} visible.")
end
def convert
return render_expected_error(400, "Image is already external") if @image.is_external?
return if request.get?
url = params.dig(:api_image, :external_url)
@image.convert_to_external(url)
redirect_to(yiff_rest_api_v2_manage_images_path(search: { id: @image.id }), notice: "Image converted")
end
def update_cache
return render_expected_error(400, "Image is not external") unless @image.is_external?
@image.external_api_image.update_cache
redirect_back(fallback_location: yiff_rest_api_v2_manage_images_path, notice: "Cache will be updated soon")
end
def iqdb
end
def query_iqdb
@sp = search_iqdb_params
@sp[:image_id] ||= @sp[:md5]
@sc = (@sp[:score_cutoff].presence || 60).to_i
if @sp[:file].present?
@results = Iqdb.query_file(@sp[:file].tempfile, @sc)
elsif @sp[:url].present?
@results = Iqdb.query_url(@sp[:url], @sc)
elsif @sp[:image_id].present?
@results = Iqdb.query_image(APIImage.find(@sp[:image_id]), @sc)
end
@results ||= []
@pagy, @images = pagy_array(@results.pluck("image"), max_items: @results.length)
render(:index)
end
def leaderboard
@list = APIImage.all_seen.sort_by(&:second).reverse.to_h
@keys = @list.map(&:first)
@results = APIImage.where(id: @keys).sort_by { |img| @keys.index(img.md5) }
@pagy, @images = pagy_array(@results, size: [1, 2, 2, 1])
render(:index)
end
private
def load_category
@category = APIImage.categories.find { |c| c.db == params[:manage_id] }
raise(ActiveRecord::RecordNotFound) if @category.blank?
end
def load_image
@image = APIImage.find(params[:id])
end
def site_title
"YiffyAPI V2 - Manage Images"
end
def search_images_params
permit_search_params(%i[category md5 original_url artist viewable external])
end
def search_iqdb_params
permit_search_params(%i[file url image_id md5 score_cutoff])
end
def create_params
params.fetch(:api_image, {}).permit(:category, :original_url, :sources_string, :artists_string, :file)
end
def create_external_params
params.fetch(:api_image, {}).permit(:site, :external_id, :category)
end
def update_params
params.fetch(:api_image, {}).permit(:category, :sources_string, :artists_string)
end
end
end
end