From f92c7370d85f53510ac8c530959023f903de8020 Mon Sep 17 00:00:00 2001 From: Donovan Daniels Date: Mon, 21 Oct 2024 19:16:52 -0500 Subject: [PATCH] Add route for adding external images --- .../yiff_rest/api_v2/images_controller.rb | 15 +++++++++++++++ app/logical/api_category.rb | 5 +++-- app/models/api_image.rb | 19 ++++++++++++++----- app/models/external_api_image.rb | 13 ++++++++----- .../api_v2/images/new_external.html.erb | 17 +++++++++++++++++ config/routes/yiff_rest_routes.rb | 2 ++ 6 files changed, 59 insertions(+), 12 deletions(-) create mode 100644 app/views/yiff_rest/api_v2/images/new_external.html.erb diff --git a/app/controllers/yiff_rest/api_v2/images_controller.rb b/app/controllers/yiff_rest/api_v2/images_controller.rb index 60c05e9..438d3e4 100644 --- a/app/controllers/yiff_rest/api_v2/images_controller.rb +++ b/app/controllers/yiff_rest/api_v2/images_controller.rb @@ -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 diff --git a/app/logical/api_category.rb b/app/logical/api_category.rb index dece7a9..0440c06 100644 --- a/app/logical/api_category.rb +++ b/app/logical/api_category.rb @@ -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 diff --git a/app/models/api_image.rb b/app/models/api_image.rb index 5e44c54..caf82a9 100644 --- a/app/models/api_image.rb +++ b/app/models/api_image.rb @@ -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: @@ -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 diff --git a/app/models/external_api_image.rb b/app/models/external_api_image.rb index d2fda1d..9a524ec 100644 --- a/app/models/external_api_image.rb +++ b/app/models/external_api_image.rb @@ -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 diff --git a/app/views/yiff_rest/api_v2/images/new_external.html.erb b/app/views/yiff_rest/api_v2/images/new_external.html.erb new file mode 100644 index 0000000..9cf5cbc --- /dev/null +++ b/app/views/yiff_rest/api_v2/images/new_external.html.erb @@ -0,0 +1,17 @@ +<% content_for(:page_title) do %> + YiffyAPI - New External Image +<% end %> + +<%= render "yiff_rest/api_v2/manage/nav" %> + + +
+
+ <%= 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 %> +
+
diff --git a/config/routes/yiff_rest_routes.rb b/config/routes/yiff_rest_routes.rb index a7f7c53..7063653 100644 --- a/config/routes/yiff_rest_routes.rb +++ b/config/routes/yiff_rest_routes.rb @@ -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