Allow ugoira to be uploaded directly from the zip URL.
This refactors things such that * Fetching the ugoira frame data happens at the end of rewrite, not rewrite_html_pages. This way uploading zip URLs should work. * The source data is cached in an instance variable in case it was indeed fetched during rewrite_html_pages. This way it doesn't get fetched a second time.
This commit is contained in:
@@ -13,7 +13,7 @@ class UploadsController < ApplicationController
|
||||
"User-Agent" => "#{Danbooru.config.safe_app_name}/#{Danbooru.config.version}"
|
||||
}
|
||||
Downloads::RewriteStrategies::Base.strategies.each do |strategy|
|
||||
@normalized_url, headers = strategy.new.rewrite(@normalized_url, headers)
|
||||
@normalized_url, headers = strategy.new(@normalized_url).rewrite(@normalized_url, headers)
|
||||
end
|
||||
|
||||
@post = Post.find_by_source(@normalized_url)
|
||||
|
||||
@@ -29,7 +29,7 @@ module Downloads
|
||||
|
||||
def before_download(url, headers, datums)
|
||||
RewriteStrategies::Base.strategies.each do |strategy|
|
||||
url, headers, datums = strategy.new.rewrite(url, headers, datums)
|
||||
url, headers, datums = strategy.new(url).rewrite(url, headers, datums)
|
||||
end
|
||||
|
||||
return [url, headers, datums]
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
module Downloads
|
||||
module RewriteStrategies
|
||||
class Base
|
||||
def initialize(url = nil)
|
||||
@url = url
|
||||
end
|
||||
|
||||
def self.strategies
|
||||
[Pixiv, NicoSeiga, Twitpic, DeviantArt, Tumblr, Moebooru]
|
||||
end
|
||||
|
||||
@@ -1,15 +1,26 @@
|
||||
module Downloads
|
||||
module RewriteStrategies
|
||||
class Pixiv < Base
|
||||
attr_accessor :url, :source
|
||||
|
||||
def initialize(url)
|
||||
@url = url
|
||||
end
|
||||
|
||||
def rewrite(url, headers, data = {})
|
||||
if url =~ /https?:\/\/(?:\w+\.)?pixiv\.net/
|
||||
url, headers = rewrite_headers(url, headers)
|
||||
url, headers = rewrite_cdn(url, headers)
|
||||
url, headers, data = rewrite_html_pages(url, headers, data)
|
||||
url, headers = rewrite_html_pages(url, headers)
|
||||
url, headers = rewrite_thumbnails(url, headers)
|
||||
url, headers = rewrite_old_small_manga_pages(url, headers)
|
||||
end
|
||||
|
||||
data[:ugoira_frame_data] = source.ugoira_frame_data
|
||||
data[:ugoira_width] = source.ugoira_width
|
||||
data[:ugoira_height] = source.ugoira_height
|
||||
data[:ugoira_content_type] = source.ugoira_content_type
|
||||
|
||||
return [url, headers, data]
|
||||
end
|
||||
|
||||
@@ -27,17 +38,11 @@ module Downloads
|
||||
# http://www.pixiv.net/member_illust.php?mode=manga_big&illust_id=18557054&page=1
|
||||
# Plus this:
|
||||
# i2.pixiv.net/img-inf/img/2014/09/25/00/57/24/46170939_64x64.jpg
|
||||
def rewrite_html_pages(url, headers, data)
|
||||
def rewrite_html_pages(url, headers)
|
||||
if url =~ /illust_id=\d+/i || url =~ %r!pixiv\.net/img-inf/img/!i
|
||||
source = ::Sources::Strategies::Pixiv.new(url)
|
||||
source.get
|
||||
data[:ugoira_frame_data] = source.ugoira_frame_data
|
||||
data[:ugoira_width] = source.ugoira_width
|
||||
data[:ugoira_height] = source.ugoira_height
|
||||
data[:ugoira_content_type] = source.ugoira_content_type
|
||||
return [source.file_url, headers, data]
|
||||
return [source.file_url, headers]
|
||||
else
|
||||
return [url, headers, data]
|
||||
return [url, headers]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -45,8 +50,7 @@ module Downloads
|
||||
# http://i2.pixiv.net/img04/img/syounen_no_uta/46170939_m.jpg
|
||||
# http://i1.pixiv.net/c/600x600/img-master/img/2014/09/24/23/25/08/46168376_p0_master1200.jpg
|
||||
def rewrite_thumbnails(url, headers)
|
||||
source = ::Sources::Strategies::Pixiv.new(url)
|
||||
url = source.rewrite_thumbnails(url)
|
||||
url = source.rewrite_thumbnails(url)
|
||||
return [url, headers]
|
||||
end
|
||||
|
||||
@@ -77,6 +81,16 @@ module Downloads
|
||||
|
||||
return [url, headers]
|
||||
end
|
||||
|
||||
# Cache the source data so it gets fetched at most once.
|
||||
def source
|
||||
@source ||= begin
|
||||
source = ::Sources::Strategies::Pixiv.new(url)
|
||||
source.get
|
||||
|
||||
source
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user