From 4972c998f86ac14c2dcb6344ee45c491c08169ee Mon Sep 17 00:00:00 2001 From: Albert Yi Date: Tue, 11 Sep 2018 14:57:18 -0700 Subject: [PATCH] rely on preview urls if available for gallery --- app/logical/sources/strategies/base.rb | 10 ++++++++++ app/logical/sources/strategies/pixiv.rb | 6 ++++++ app/logical/sources/strategies/tumblr.rb | 6 ++++++ app/logical/sources/strategies/twitter.rb | 6 ++++++ app/views/uploads/batch.html.erb | 4 ++-- test/unit/sources/pixiv_test.rb | 4 ++++ test/unit/sources/tumblr_test.rb | 4 ++++ test/unit/sources/twitter_test.rb | 4 ++++ 8 files changed, 42 insertions(+), 2 deletions(-) diff --git a/app/logical/sources/strategies/base.rb b/app/logical/sources/strategies/base.rb index d0036ff86..2404e2f40 100644 --- a/app/logical/sources/strategies/base.rb +++ b/app/logical/sources/strategies/base.rb @@ -53,6 +53,16 @@ module Sources image_urls.first end + # A smaller representation of the image that's suitable for + # displaying previews. + def preview_urls + image_urls + end + + def preview_url + preview_urls.first + end + # Whatever url is, this method should return a link to the HTML # page containing the resource. It should not be a binary file. It will # eventually be assigned as the source for the post, but it does not diff --git a/app/logical/sources/strategies/pixiv.rb b/app/logical/sources/strategies/pixiv.rb index 94fa34274..e44950461 100644 --- a/app/logical/sources/strategies/pixiv.rb +++ b/app/logical/sources/strategies/pixiv.rb @@ -50,6 +50,12 @@ module Sources [url] end + def preview_urls + image_urls.map do |x| + x.sub(%r!pximg\.net/img-original/img!, "pximg.net/c/240x240/img-master/img") + end + end + def page_url if novel_id.present? return "https://www.pixiv.net/novel/show.php?id=#{novel_id}&mode=cover" diff --git a/app/logical/sources/strategies/tumblr.rb b/app/logical/sources/strategies/tumblr.rb index c1f48b51e..3ab70b205 100644 --- a/app/logical/sources/strategies/tumblr.rb +++ b/app/logical/sources/strategies/tumblr.rb @@ -36,6 +36,12 @@ module Sources::Strategies .uniq end + def preview_urls + image_urls.map do |x| + x.sub(%r!_1280\.(jpg|png|gif|jpeg)\z!, '_250.\1') + end + end + def page_url [url, referer_url].each do |x| if x =~ POST diff --git a/app/logical/sources/strategies/twitter.rb b/app/logical/sources/strategies/twitter.rb index 723664170..a789d6cf3 100644 --- a/app/logical/sources/strategies/twitter.rb +++ b/app/logical/sources/strategies/twitter.rb @@ -36,6 +36,12 @@ module Sources::Strategies end memoize :image_urls + def preview_urls + image_urls.map do |x| + x.sub(%r!\.(jpg|jpeg|png|gif)(?::orig)?\z!i, '.\1:small') + end + end + def page_url [url, referer_url].each do |x| if self.class.status_id_from_url(x).present? diff --git a/app/views/uploads/batch.html.erb b/app/views/uploads/batch.html.erb index 476a3e0d9..617466eaf 100644 --- a/app/views/uploads/batch.html.erb +++ b/app/views/uploads/batch.html.erb @@ -9,7 +9,7 @@ <% if @source.present? %>
- <% @source.image_urls.each.with_index do |url, i| %> + <% @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" %> @@ -17,7 +17,7 @@ <%= link_to new_upload_path(url: url, ref: @url), target: "_blank" do %> <% if ImageProxy.needs_proxy?(url) %> - <%= image_tag(image_proxy_uploads_path(url: url)) %> + <%= image_tag(image_proxy_uploads_path(url: preview_url)) %> <% elsif url.is_a?(String) %> <%= image_tag url %> <% else %> diff --git a/test/unit/sources/pixiv_test.rb b/test/unit/sources/pixiv_test.rb index 926a2fbeb..9581d6dbe 100644 --- a/test/unit/sources/pixiv_test.rb +++ b/test/unit/sources/pixiv_test.rb @@ -102,6 +102,10 @@ module Sources assert_equal("https://i.pximg.net/img-original/img/2017/11/21/05/12/37/65981735_p0.jpg", @site.image_url) end + should "get the preview size image url" do + assert_equal("https://i.pximg.net/c/240x240/img-master/img/2017/11/21/05/12/37/65981735_p0.jpg", @site.preview_url) + end + should "get the page count" do assert_equal(1, @site.image_urls.size) end diff --git a/test/unit/sources/tumblr_test.rb b/test/unit/sources/tumblr_test.rb index bfc02c94d..7d5104f96 100644 --- a/test/unit/sources/tumblr_test.rb +++ b/test/unit/sources/tumblr_test.rb @@ -70,6 +70,10 @@ module Sources assert_equal("https://media.tumblr.com/3bbfcbf075ddf969c996641b264086fd/tumblr_os2buiIOt51wsfqepo1_1280.png", @site.image_url) end + should "get the preview url" do + assert_equal("https://media.tumblr.com/3bbfcbf075ddf969c996641b264086fd/tumblr_os2buiIOt51wsfqepo1_250.png", @site.preview_url) + end + should "get the artist" do CurrentUser.user = FactoryBot.create(:user) CurrentUser.ip_addr = "127.0.0.1" diff --git a/test/unit/sources/twitter_test.rb b/test/unit/sources/twitter_test.rb index 91089f0b4..9ce76fec8 100644 --- a/test/unit/sources/twitter_test.rb +++ b/test/unit/sources/twitter_test.rb @@ -78,6 +78,10 @@ module Sources assert_equal("https://pbs.twimg.com/media/C1kt72yVEAEGpOv.jpg:orig", @site.image_url) end + should "get the preview url" do + assert_equal("https://pbs.twimg.com/media/C1kt72yVEAEGpOv.jpg:small", @site.preview_url) + end + should "get the canonical url" do assert_equal("https://twitter.com/aranobu/status/817736083567820800", @site.canonical_url) end