Add route for adding external images

This commit is contained in:
Donovan Daniels 2024-10-21 19:16:52 -05:00
parent 3d1c50b3ce
commit f92c7370d8
Signed by: Donovan_DMC
GPG Key ID: 907D29CBFD6157BA
6 changed files with 59 additions and 12 deletions

View File

@ -18,6 +18,10 @@ module YiffRest
@categories = APIImage.categories @categories = APIImage.categories
end end
def new_external
@categories = APIImage.categories
end
def edit def edit
@image = APIImage.find(params[:id]) @image = APIImage.find(params[:id])
@categories = APIImage.categories @categories = APIImage.categories
@ -34,6 +38,13 @@ module YiffRest
redirect_to(yiff_rest_api_v2_manage_images_path(search: { category: @image.category }), notice: "Image added") redirect_to(yiff_rest_api_v2_manage_images_path(search: { category: @image.category }), notice: "Image added")
end 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 def update
@image = APIImage.find(params[:id]) @image = APIImage.find(params[:id])
@image.update(update_params) @image.update(update_params)
@ -124,6 +135,10 @@ module YiffRest
params.fetch(:api_image, {}).permit(:category, :original_url, :sources_string, :artists_string, :file) params.fetch(:api_image, {}).permit(:category, :original_url, :sources_string, :artists_string, :file)
end end
def create_external_params
params.fetch(:api_image, {}).permit(:site, :external_id, :category)
end
def update_params def update_params
params.fetch(:api_image, {}).permit(:category, :sources_string, :artists_string) params.fetch(:api_image, {}).permit(:category, :sources_string, :artists_string)
end end

View File

@ -28,11 +28,11 @@ class APICategory
end end
def unsourced_count def unsourced_count
APIImage.where(category: db, sources: []).count APIImage.internal.where(category: db, sources: []).count
end end
def sources def sources
r = APIImage.joins("CROSS JOIN unnest(sources) AS source").where(category: db).distinct r = APIImage.joins("CROSS JOIN unnest(sources) AS source").where(category: db).distinct.internal
other = r other = r
list = [] list = []
@ -44,6 +44,7 @@ class APICategory
list << { name: "Unsourced", count: unsourced_count } list << { name: "Unsourced", count: unsourced_count }
# noinspection RubyMismatchedArgumentType # noinspection RubyMismatchedArgumentType
list << { name: "Other", count: other.count } list << { name: "Other", count: other.count }
list << { name: "External", count: APIImage.where(category: db).external.count }
list list
end end

View File

@ -225,8 +225,10 @@ class APIImage < ApplicationRecord
ExternalAPIImage.create!(api_image: self, external_id: external_id, site: type) ExternalAPIImage.create!(api_image: self, external_id: external_id, site: type)
delete_files! delete_files!
update_columns(artists: [], sources: [], width: 0, height: 0, mime_type: "external", file_ext: "external", file_size: 0) update_columns(artists: [], sources: [], width: 0, height: 0, mime_type: "external", file_ext: "external", file_size: 0)
send_converted
end end
send_converted
reload_external_api_image
external_api_image
end end
def self.categories def self.categories
@ -321,9 +323,9 @@ class APIImage < ApplicationRecord
description: <<~DESC, description: <<~DESC,
ID: `#{md5}` ID: `#{md5}`
Category: `#{category}` Category: `#{category}`
Artists: `#{artists.join(', ')}` Artists: `#{artists.join(', ') || '[NONE]'}`
Sources: Sources:
#{sources.map { |s| "* #{s}" }.join("\n")} #{sources.map { |s| "* #{s}" }.join("\n") || '[NONE]'}}
Upload Type: #{original_url.present? ? "[url](#{original_url})" : 'file'} Upload Type: #{original_url.present? ? "[url](#{original_url})" : 'file'}
Uploaded By: <@#{creator.id}> (`#{creator.name}`) Uploaded By: <@#{creator.id}> (`#{creator.name}`)
Created At: <t:#{created_at.to_i}> Created At: <t:#{created_at.to_i}>
@ -340,9 +342,9 @@ class APIImage < ApplicationRecord
description: <<~DESC, description: <<~DESC,
ID: `#{md5}` ID: `#{md5}`
Category: `#{category}` Category: `#{category}`
Artists: `#{artists.join(', ')}` Artists: `#{artists.join(', ') || '[NONE]'}}`
Sources: Sources:
#{sources.map { |s| "* #{s}" }.join("\n")} #{sources.map { |s| "* #{s}" }.join("\n") || '[NONE]'}}
Upload Type: #{original_url.present? ? "[url](#{original_url})" : 'file'} Upload Type: #{original_url.present? ? "[url](#{original_url})" : 'file'}
Uploaded By: <@#{creator.id}> (`#{creator.name}`) Uploaded By: <@#{creator.id}> (`#{creator.name}`)
Reason: #{deletion_reason || 'None Provided'} Reason: #{deletion_reason || 'None Provided'}
@ -454,4 +456,11 @@ class APIImage < ApplicationRecord
return unless is_image? return unless is_image?
IqdbRemoveJob.perform_now(iqdb_id) IqdbRemoveJob.perform_now(iqdb_id)
end end
def self.create_external!(type, id, category)
site = ExternalAPIImage::SITES.find { |s| s.value == type }
raise(Error, "Invalid site: #{type}") if site.blank?
img = create!(width: 0, height: 0, mime_type: "external", file_ext: "external", file_size: 0, category: category)
img.convert_to_external(format(site.format, id))
end
end end

View File

@ -5,14 +5,17 @@ class ExternalAPIImage < ApplicationRecord
CACHE_TIME = 2.days CACHE_TIME = 2.days
IGNORED_ARTISTS = %w[avoid_posting conditional_dnp sound_warning epilepsy_warning].freeze IGNORED_ARTISTS = %w[avoid_posting conditional_dnp sound_warning epilepsy_warning].freeze
Site = Struct.new(:name, :value, :format)
SITES = [
Site.new("E621", "e621", "https://e621.net/posts/%s"),
Site.new("Femboy Fans", "femboyfans", "https://femboy.fan/posts/%s"),
].freeze
belongs_to :api_image belongs_to :api_image
validates :site, inclusion: { in: %w[e621 femboyfans] } validates :site, inclusion: { in: SITES.map(&:value) }
enum site: { enum site: SITES.to_h { |s| [s.value, s.value] }
"e621" => "e621",
"femboyfans" => "femboyfans",
}
module DataMethods module DataMethods
def url def url

View File

@ -0,0 +1,17 @@
<% content_for(:page_title) do %>
YiffyAPI - New External Image
<% end %>
<%= render "yiff_rest/api_v2/manage/nav" %>
<div class="w-100">
<div style="width: 40%; margin-left: 30%;">
<%= simple_form_for(:api_image, url: create_external_yiff_rest_api_v2_manage_images_path, method: :post) do |f| %>
<%= f.input(:site, label: "Site", as: :select, selected: params.dig(:api_image, :site), collection: ExternalAPIImage::SITES.map { |s| [s.name, s.value] }) %>
<%= f.input(:external_id, input_html: { value: params.dig(:api_image, :external_id) }) %>
<%= f.input(:category, as: :select, selected: params.dig(:api_image, :category), collection: @categories.map { |cat| [cat.name, cat.db] }) %>
<%= f.button(:submit, "Upload Image", name: nil) %>
<% end %>
</div>
</div>

View File

@ -62,6 +62,8 @@ module YiffRestRoutes
collection do collection do
get(:iqdb) get(:iqdb)
match("/iqdb/query", via: %i[get post], action: :query_iqdb) match("/iqdb/query", via: %i[get post], action: :query_iqdb)
post(:external, action: :create_external, as: "create_external")
get("/new/external", action: :new_external)
end end
end end
end end