From e818b29ff83bc771d514587e0f0abd7ad7d981fe Mon Sep 17 00:00:00 2001 From: Toks Date: Wed, 20 Nov 2013 12:07:07 -0500 Subject: [PATCH] Redirect more source links to work page when possible Related to #1866. Fixes #2021. --- app/models/post.rb | 23 ++++++++++++++++++++--- test/unit/post_test.rb | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 8c83d6517..4f1799e59 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -293,10 +293,27 @@ class Post < ActiveRecord::Base end def normalized_source - if source =~ /pixiv\.net\/img(?:\d+\/img)?\// - img_id = source[/(\d+)(_s|_m|(_big)?_p\d+)?\.[\w\?]+\s*$/, 1] + case source + when %r{\Ahttp://img\d+\.pixiv\.net/img/[^\/]+/(\d+)}i, %r{\Ahttp://i\d\.pixiv\.net/img\d+/img/[^\/]+/(\d+)}i + "http://www.pixiv.net/member_illust.php?mode=medium&illust_id=#{$1}" + + when %r{\Ahttp://lohas\.nicoseiga\.jp/priv/(\d+)\?e=\d+&h=[a-f0-9]+}i, %r{\Ahttp://lohas\.nicoseiga\.jp/priv/[a-f0-9]+/\d+/(\d+)}i + "http://seiga.nicovideo.jp/seiga/im#{$1}" + + when %r{\Ahttp://d3j5vwomefv46c\.cloudfront\.net/photos/large/(\d+)\.}i + base_10_id = $1.to_i + base_36_id = base_10_id.to_s(36) + "http://twitpic.com/#{base_36_id}" + + when %r{\Ahttp://fc\d+\.deviantart\.net/.+/[a-z0-9_]+_by_([a-z0-9_]+)-d([a-z0-9]+)\.}i + "http://#{$1}.deviantart.com/gallery/#/d#{$2}" + + when %r{\Ahttp://www\.karabako\.net/images/karabako_(\d+)\.}i + "http://www.karabako.net/post/view/#{$1}" + + when %r{\Ahttp://p\.twpl\.jp/show/orig/([a-z0-9]+)}i + "http://p.twipple.jp/#{$1}" - "http://www.pixiv.net/member_illust.php?mode=medium&illust_id=#{img_id}" else source end diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb index 5db0d50d5..8903b57f1 100644 --- a/test/unit/post_test.rb +++ b/test/unit/post_test.rb @@ -739,6 +739,42 @@ class PostTest < ActiveSupport::TestCase assert_equal("aaa", post.tag_string) end end + + context "normalizing its source" do + should "normalize pixiv links" do + @post.source = "http://i2.pixiv.net/img12/img/zenze/39749565.png" + assert_equal("http://www.pixiv.net/member_illust.php?mode=medium&illust_id=39749565", @post.normalized_source) + @post.source = "http://i1.pixiv.net/img53/img/themare/39735353_big_p1.jpg" + assert_equal("http://www.pixiv.net/member_illust.php?mode=medium&illust_id=39735353", @post.normalized_source) + end + + should "normalize nicoseiga links" do + @post.source = "http://lohas.nicoseiga.jp/priv/3521156?e=1382558156&h=f2e089256abd1d453a455ec8f317a6c703e2cedf" + assert_equal("http://seiga.nicovideo.jp/seiga/im3521156", @post.normalized_source) + @post.source = "http://lohas.nicoseiga.jp/priv/b80f86c0d8591b217e7513a9e175e94e00f3c7a1/1384936074/3583893" + assert_equal("http://seiga.nicovideo.jp/seiga/im3583893", @post.normalized_source) + end + + should "normalize twitpic links" do + @post.source = "http://d3j5vwomefv46c.cloudfront.net/photos/large/820960031.jpg?1384107199" + assert_equal("http://twitpic.com/dks0tb", @post.normalized_source) + end + + should "normalize deviantart links" do + @post.source = "http://fc06.deviantart.net/fs71/f/2013/295/d/7/you_are_already_dead__by_mar11co-d6rgm0e.jpg" + assert_equal("http://mar11co.deviantart.com/gallery/#/d6rgm0e", @post.normalized_source) + end + + should "normalize karabako links" do + @post.source = "http://www.karabako.net/images/karabako_38835.jpg" + assert_equal("http://www.karabako.net/post/view/38835", @post.normalized_source) + end + + should "normalize twipple links" do + @post.source = "http://p.twpl.jp/show/orig/mI2c3" + assert_equal("http://p.twipple.jp/mI2c3", @post.normalized_source) + end + end end end