Add route for adding external images
This commit is contained in:
parent
3d1c50b3ce
commit
f92c7370d8
|
@ -18,6 +18,10 @@ module YiffRest
|
|||
@categories = APIImage.categories
|
||||
end
|
||||
|
||||
def new_external
|
||||
@categories = APIImage.categories
|
||||
end
|
||||
|
||||
def edit
|
||||
@image = APIImage.find(params[:id])
|
||||
@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")
|
||||
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)
|
||||
|
@ -124,6 +135,10 @@ module YiffRest
|
|||
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
|
||||
|
|
|
@ -28,11 +28,11 @@ class APICategory
|
|||
end
|
||||
|
||||
def unsourced_count
|
||||
APIImage.where(category: db, sources: []).count
|
||||
APIImage.internal.where(category: db, sources: []).count
|
||||
end
|
||||
|
||||
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
|
||||
list = []
|
||||
|
||||
|
@ -44,6 +44,7 @@ class APICategory
|
|||
list << { name: "Unsourced", count: unsourced_count }
|
||||
# noinspection RubyMismatchedArgumentType
|
||||
list << { name: "Other", count: other.count }
|
||||
list << { name: "External", count: APIImage.where(category: db).external.count }
|
||||
list
|
||||
end
|
||||
|
||||
|
|
|
@ -225,8 +225,10 @@ class APIImage < ApplicationRecord
|
|||
ExternalAPIImage.create!(api_image: self, external_id: external_id, site: type)
|
||||
delete_files!
|
||||
update_columns(artists: [], sources: [], width: 0, height: 0, mime_type: "external", file_ext: "external", file_size: 0)
|
||||
send_converted
|
||||
end
|
||||
send_converted
|
||||
reload_external_api_image
|
||||
external_api_image
|
||||
end
|
||||
|
||||
def self.categories
|
||||
|
@ -321,9 +323,9 @@ class APIImage < ApplicationRecord
|
|||
description: <<~DESC,
|
||||
ID: `#{md5}`
|
||||
Category: `#{category}`
|
||||
Artists: `#{artists.join(', ')}`
|
||||
Artists: `#{artists.join(', ') || '[NONE]'}`
|
||||
Sources:
|
||||
#{sources.map { |s| "* #{s}" }.join("\n")}
|
||||
#{sources.map { |s| "* #{s}" }.join("\n") || '[NONE]'}}
|
||||
Upload Type: #{original_url.present? ? "[url](#{original_url})" : 'file'}
|
||||
Uploaded By: <@#{creator.id}> (`#{creator.name}`)
|
||||
Created At: <t:#{created_at.to_i}>
|
||||
|
@ -340,9 +342,9 @@ class APIImage < ApplicationRecord
|
|||
description: <<~DESC,
|
||||
ID: `#{md5}`
|
||||
Category: `#{category}`
|
||||
Artists: `#{artists.join(', ')}`
|
||||
Artists: `#{artists.join(', ') || '[NONE]'}}`
|
||||
Sources:
|
||||
#{sources.map { |s| "* #{s}" }.join("\n")}
|
||||
#{sources.map { |s| "* #{s}" }.join("\n") || '[NONE]'}}
|
||||
Upload Type: #{original_url.present? ? "[url](#{original_url})" : 'file'}
|
||||
Uploaded By: <@#{creator.id}> (`#{creator.name}`)
|
||||
Reason: #{deletion_reason || 'None Provided'}
|
||||
|
@ -454,4 +456,11 @@ class APIImage < ApplicationRecord
|
|||
return unless is_image?
|
||||
IqdbRemoveJob.perform_now(iqdb_id)
|
||||
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
|
||||
|
|
|
@ -5,14 +5,17 @@ class ExternalAPIImage < ApplicationRecord
|
|||
CACHE_TIME = 2.days
|
||||
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
|
||||
|
||||
validates :site, inclusion: { in: %w[e621 femboyfans] }
|
||||
validates :site, inclusion: { in: SITES.map(&:value) }
|
||||
|
||||
enum site: {
|
||||
"e621" => "e621",
|
||||
"femboyfans" => "femboyfans",
|
||||
}
|
||||
enum site: SITES.to_h { |s| [s.value, s.value] }
|
||||
|
||||
module DataMethods
|
||||
def url
|
||||
|
|
17
app/views/yiff_rest/api_v2/images/new_external.html.erb
Normal file
17
app/views/yiff_rest/api_v2/images/new_external.html.erb
Normal 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>
|
|
@ -62,6 +62,8 @@ module YiffRestRoutes
|
|||
collection do
|
||||
get(: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
|
||||
|
|
Loading…
Reference in New Issue
Block a user