109 lines
3.2 KiB
Ruby
109 lines
3.2 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
|
|
|
|
def index
|
|
@sp = search_images_params
|
|
@sp[:id] ||= @sp[:md5]
|
|
@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 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 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 iqdb
|
|
@sp = {}
|
|
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
|
|
|
|
private
|
|
|
|
def load_category
|
|
@category = APIImage.categories.find { |c| c.db == params[:manage_id] }
|
|
raise(ActiveRecord::RecordNotFound) if @category.blank?
|
|
end
|
|
|
|
def site_title
|
|
"YiffyAPI V2 - Manage Images"
|
|
end
|
|
|
|
def search_images_params
|
|
permit_search_params(%i[category md5 original_url artist])
|
|
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 update_params
|
|
params.fetch(:api_image, {}).permit(:category, :sources_string, :artists_string)
|
|
end
|
|
end
|
|
end
|
|
end
|