From 68ba447494b238546c364932f9dca6ed161ba54c Mon Sep 17 00:00:00 2001 From: evazion Date: Mon, 21 Feb 2022 00:03:43 -0600 Subject: [PATCH] uploads: remove batch upload page. * Make /uploads/batch redirect to /uploads/new. * Remove /uploads/image_proxy. --- app/controllers/uploads_controller.rb | 13 ------- app/javascript/src/javascripts/uploads.js | 8 ---- .../src/styles/specific/uploads.scss | 24 ------------ app/logical/image_proxy.rb | 24 ------------ app/logical/sources/strategies/base.rb | 6 --- app/logical/sources/strategies/moebooru.rb | 4 -- app/logical/sources/strategies/pixiv.rb | 4 -- app/logical/sources/strategies/weibo.rb | 4 -- app/policies/upload_policy.rb | 8 ---- app/views/static/bookmarklet.html.erb | 1 - app/views/uploads/batch.html.erb | 37 ------------------- config/routes.rb | 3 +- test/functional/uploads_controller_test.rb | 34 ++--------------- 13 files changed, 4 insertions(+), 166 deletions(-) delete mode 100644 app/logical/image_proxy.rb delete mode 100644 app/views/uploads/batch.html.erb diff --git a/app/controllers/uploads_controller.rb b/app/controllers/uploads_controller.rb index febff59bf..1d3015850 100644 --- a/app/controllers/uploads_controller.rb +++ b/app/controllers/uploads_controller.rb @@ -15,19 +15,6 @@ class UploadsController < ApplicationController respond_with(@upload) end - def batch - authorize Upload - @url = params.dig(:batch, :url) || params[:url] - @source = Sources::Strategies.find(@url, params[:ref]) if @url.present? - respond_with(@source) - end - - def image_proxy - authorize Upload - resp = ImageProxy.get_image(params[:url]) - send_data resp.body, type: resp.mime_type, disposition: "inline" - end - def index @mode = params.fetch(:mode, "gallery") diff --git a/app/javascript/src/javascripts/uploads.js b/app/javascript/src/javascripts/uploads.js index e3d037cea..3731c758e 100644 --- a/app/javascript/src/javascripts/uploads.js +++ b/app/javascript/src/javascripts/uploads.js @@ -21,10 +21,6 @@ Upload.initialize_all = function() { }); } - if ($("#c-uploads #a-batch").length) { - $(document).on("click.danbooru", "#c-uploads #a-batch #link", Upload.batch_open_all); - } - Upload.loadAssets(); } @@ -71,10 +67,6 @@ Upload.toggle_translation = function() { $(".commentary-translation").slideToggle(); }; -Upload.batch_open_all = function() { - $(".upload-preview > a").each((_i, link) => window.open(link.href)); -}; - $(function() { Upload.initialize_all(); }); diff --git a/app/javascript/src/styles/specific/uploads.scss b/app/javascript/src/styles/specific/uploads.scss index 10920e39a..c05af3146 100644 --- a/app/javascript/src/styles/specific/uploads.scss +++ b/app/javascript/src/styles/specific/uploads.scss @@ -44,28 +44,4 @@ div#c-uploads { margin-right: 1.5em; } } - - div.upload-preview { - display: inline-block; - - > a { - width: 154px; - height: 154px; - margin: 0 10px 10px 0; - display: inline-block; - - img { - margin: auto; - border: 2px solid transparent; - - max-width: 154px; - max-height: 154px; - } - } - - .caption-top { - font-size: var(--text-sm); - margin-bottom: 0; - } - } } diff --git a/app/logical/image_proxy.rb b/app/logical/image_proxy.rb deleted file mode 100644 index 2dcc53acc..000000000 --- a/app/logical/image_proxy.rb +++ /dev/null @@ -1,24 +0,0 @@ -# frozen_string_literal: true - -class ImageProxy - class Error < StandardError; end - - def self.needs_proxy?(url) - fake_referer_for(url).present? - end - - def self.fake_referer_for(url) - Sources::Strategies.find(url).headers["Referer"] - end - - def self.get_image(url) - raise Error, "URL not present" unless url.present? - raise Error, "Proxy not allowed for this url (url=#{url})" unless needs_proxy?(url) - - referer = fake_referer_for(url) - response = Danbooru::Http.timeout(30).headers(Referer: referer).get(url) - raise Error, "Couldn't proxy image (code=#{response.status}, url=#{url})" unless response.status.success? - - response - end -end diff --git a/app/logical/sources/strategies/base.rb b/app/logical/sources/strategies/base.rb index 52b8630f5..9d68389fc 100644 --- a/app/logical/sources/strategies/base.rb +++ b/app/logical/sources/strategies/base.rb @@ -211,12 +211,6 @@ module Sources nil end - # Subclasses should merge in any required headers needed to access resources - # on the site. - def headers - {} - end - # Download the file at the given url, or at the main image url by default. def download_file!(download_url = image_url) response, file = http_downloader.download_media(download_url) diff --git a/app/logical/sources/strategies/moebooru.rb b/app/logical/sources/strategies/moebooru.rb index 573c20fa4..7838e9ecf 100644 --- a/app/logical/sources/strategies/moebooru.rb +++ b/app/logical/sources/strategies/moebooru.rb @@ -100,10 +100,6 @@ module Sources tags.map(&:first).flat_map(&method(:translate_tag)).uniq.sort end - def headers - { "Referer" => "http://#{site_name}" } - end - # Moebooru returns an empty array when doing an md5: search for a # deleted post. Because of this, api_response may be empty in some cases. def api_response diff --git a/app/logical/sources/strategies/pixiv.rb b/app/logical/sources/strategies/pixiv.rb index 7745a1f9e..64b3a99f5 100644 --- a/app/logical/sources/strategies/pixiv.rb +++ b/app/logical/sources/strategies/pixiv.rb @@ -177,10 +177,6 @@ module Sources api_illust[:description] end - def headers - { "Referer" => "https://www.pixiv.net" } - end - def normalize_for_source return nil if illust_id.blank? "https://www.pixiv.net/artworks/#{illust_id}" diff --git a/app/logical/sources/strategies/weibo.rb b/app/logical/sources/strategies/weibo.rb index 7887d7db9..ff8b55212 100644 --- a/app/logical/sources/strategies/weibo.rb +++ b/app/logical/sources/strategies/weibo.rb @@ -93,10 +93,6 @@ module Sources image_urls.map { |img| img.gsub(%r{.cn/\w+/(\w+)}, '.cn/orj360/\1') } end - def headers - { "Referer" => "https://weibo.com" } - end - def page_url if api_response.present? artist_id = api_response["user"]["id"] diff --git a/app/policies/upload_policy.rb b/app/policies/upload_policy.rb index 8c70c1606..3e493d916 100644 --- a/app/policies/upload_policy.rb +++ b/app/policies/upload_policy.rb @@ -9,14 +9,6 @@ class UploadPolicy < ApplicationPolicy user.is_admin? || record.uploader_id == user.id end - def batch? - unbanned? - end - - def image_proxy? - unbanned? - end - def permitted_attributes [:source, :referer_url, files: {}] end diff --git a/app/views/static/bookmarklet.html.erb b/app/views/static/bookmarklet.html.erb index 2b3014fbf..f5d2dcaac 100644 --- a/app/views/static/bookmarklet.html.erb +++ b/app/views/static/bookmarklet.html.erb @@ -6,7 +6,6 @@

<%= link_to "Post to #{Danbooru.config.app_name}", "javascript:location.href='#{request.protocol + request.host_with_port}/uploads/new?url='+encodeURIComponent(location.href)+'&ref='+encodeURIComponent(document.referrer)" %> - | <%= link_to "Batch to #{Danbooru.config.app_name}", "javascript:location.href='#{request.protocol + request.host_with_port}/uploads/batch?url='+encodeURIComponent(location.href)" %>

diff --git a/app/views/uploads/batch.html.erb b/app/views/uploads/batch.html.erb deleted file mode 100644 index fbc54c39d..000000000 --- a/app/views/uploads/batch.html.erb +++ /dev/null @@ -1,37 +0,0 @@ -<% page_title "Batch Upload" %> -<%= render "uploads/secondary_links" %> - -
-
-

Batch Upload

- - <%= simple_form_for(:batch, url: batch_uploads_path, method: :get, defaults: { required: false }, html: { class: "one-line-form" }) do |f| %> - <%= f.input :url, label: "URL", input_html: { size: 70, value: @url, placeholder: "https://www.pixiv.net/member_illust.php?mode=medium&illust_id=65981746" } %> - <%= f.submit "Fetch" %> - <% end %> - - <% if @source.present? %> -
- <% @source.image_urls.zip(@source.preview_urls).each.with_index do |(url, preview_url), i| %> -
-

- <%= link_to "Image ##{i}", new_upload_path(url: url, ref: @url), target: "_blank" %> -

- - <%= link_to new_upload_path(url: url, ref: @url), target: "_blank" do %> - <% if ImageProxy.needs_proxy?(url) %> - <%= image_tag(image_proxy_uploads_path(url: preview_url)) %> - <% elsif url.is_a?(String) %> - <%= image_tag preview_url %> - <% else %> - Direct Link - <% end %> - <% end %> -
- <% end %> - -

<%= link_to "Open all links in new tabs", "#", :id => "link" %>

-
- <% end %> -
-
diff --git a/config/routes.rb b/config/routes.rb index ed0a494f3..665c09cfa 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -249,8 +249,7 @@ Rails.application.routes.draw do resources :tag_implications, only: [:show, :index, :destroy] resources :uploads do collection do - get :batch - get :image_proxy + get :batch, to: redirect(path: "/uploads/new") end resources :upload_media_assets, only: [:show, :index], path: "assets" end diff --git a/test/functional/uploads_controller_test.rb b/test/functional/uploads_controller_test.rb index 00b251620..12721f61e 100644 --- a/test/functional/uploads_controller_test.rb +++ b/test/functional/uploads_controller_test.rb @@ -6,39 +6,11 @@ class UploadsControllerTest < ActionDispatch::IntegrationTest @user = create(:user) end - context "image proxy action" do - should "work" do - url = "https://i.pximg.net/img-original/img/2017/11/21/17/06/44/65985331_p0.png" - get_auth image_proxy_uploads_path, @user, params: { url: url } - - assert_response :success - assert_equal("image/png", response.media_type) - assert_equal(15_573, response.body.size) - end - end - context "batch action" do - context "for twitter galleries" do - should "render" do - skip "Twitter keys are not set" unless Danbooru.config.twitter_api_key - get_auth batch_uploads_path, @user, params: {:url => "https://twitter.com/lvlln/status/567054278486151168"} - assert_response :success - end - end + should "redirect to the new upload page" do + get batch_uploads_path(url: "https://twitter.com/lvlln/status/567054278486151168") - context "for pixiv ugoira galleries" do - should "render" do - get_auth batch_uploads_path, @user, params: {:url => "https://www.pixiv.net/member_illust.php?mode=medium&illust_id=59523577"} - assert_response :success - assert_no_match(/59523577_ugoira0\.jpg/, response.body) - end - end - - context "for a blank source" do - should "render" do - get_auth batch_uploads_path, @user - assert_response :success - end + assert_redirected_to new_upload_path(url: "https://twitter.com/lvlln/status/567054278486151168") end end