added full pending post unit test
This commit is contained in:
87
app/logical/download.rb
Normal file
87
app/logical/download.rb
Normal file
@@ -0,0 +1,87 @@
|
||||
class Download
|
||||
class Error < Exception ; end
|
||||
|
||||
attr_accessor :source, :content_type
|
||||
|
||||
def initialize(source, file_path)
|
||||
@source = source
|
||||
@file_path = file_path
|
||||
end
|
||||
|
||||
# Downloads to @file_path
|
||||
def download!
|
||||
http_get_streaming(@source) do |response|
|
||||
self.content_type = response["Content-Type"]
|
||||
File.open(@file_path, "wb") do |out|
|
||||
response.read_body(out)
|
||||
end
|
||||
end
|
||||
@source = fix_image_board_sources(@source)
|
||||
end
|
||||
|
||||
# private
|
||||
def handle_pixiv(source, headers)
|
||||
if source =~ /pixiv\.net/
|
||||
headers["Referer"] = "http://www.pixiv.net"
|
||||
|
||||
# Don't download the small version
|
||||
if source =~ %r!(/img/.+?/.+?)_m.+$!
|
||||
match = $1
|
||||
source.sub!(match + "_m", match)
|
||||
end
|
||||
end
|
||||
|
||||
source
|
||||
end
|
||||
|
||||
def http_get_streaming(source, options = {})
|
||||
max_size = options[:max_size] || Danbooru.config.max_file_size
|
||||
max_size = nil if max_size == 0 # unlimited
|
||||
limit = 4
|
||||
|
||||
while true
|
||||
url = URI.parse(source)
|
||||
|
||||
unless url.is_a?(URI::HTTP)
|
||||
raise Error.new("URL must be HTTP")
|
||||
end
|
||||
|
||||
Net::HTTP.start(url.host, url.port) do |http|
|
||||
http.read_timeout = 10
|
||||
headers = {
|
||||
"User-Agent" => "#{Danbooru.config.safe_app_name}/#{Danbooru.config.version}"
|
||||
}
|
||||
source = handle_pixiv(source, headers)
|
||||
http.request_get(url.request_uri, headers) do |res|
|
||||
case res
|
||||
when Net::HTTPSuccess then
|
||||
if max_size
|
||||
len = res["Content-Length"]
|
||||
raise Error.new("File is too large (#{len} bytes)") if len && len.to_i > max_size
|
||||
end
|
||||
yield(res)
|
||||
return
|
||||
|
||||
when Net::HTTPRedirection then
|
||||
if limit == 0 then
|
||||
raise Error.new("Too many redirects")
|
||||
end
|
||||
source = res["location"]
|
||||
limit -= 1
|
||||
|
||||
else
|
||||
raise Error.new("HTTP error code: #{res.code} #{res.message}")
|
||||
end
|
||||
end # http.request_get
|
||||
end # http.start
|
||||
end # while
|
||||
end # def
|
||||
|
||||
def fix_image_board_sources(source)
|
||||
if source =~ /\/src\/\d{12,}|urnc\.yi\.org|yui\.cynthia\.bne\.jp/
|
||||
"Image board"
|
||||
else
|
||||
source
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,2 +1,205 @@
|
||||
require "danbooru_image_resizer/danbooru_image_resizer"
|
||||
require "tmpdir"
|
||||
|
||||
class PendingPost < ActiveRecord::Base
|
||||
end
|
||||
class Error < Exception ; end
|
||||
|
||||
attr_accessor :file, :image_width, :image_height, :file_ext, :md5, :file_size
|
||||
belongs_to :uploader, :class_name => "User"
|
||||
|
||||
def process!
|
||||
update_attribute(:status, "processing")
|
||||
download_from_source(temp_file_path) if is_downloadable?
|
||||
calculate_hash(file_path)
|
||||
calculate_file_size(file_path)
|
||||
calculate_dimensions(file_path) if has_dimensions?
|
||||
generate_resizes(file_path)
|
||||
move_file
|
||||
post = convert_to_post
|
||||
if post.save
|
||||
update_attributes(:status => "finished", :post_id => post.id)
|
||||
else
|
||||
update_attribute(:status, "error: " + post.errors.full_messages.join(", "))
|
||||
end
|
||||
end
|
||||
|
||||
# private
|
||||
module ResizerMethods
|
||||
def generate_resizes(source_path)
|
||||
generate_resize_for(Danbooru.config.small_image_width, source_path)
|
||||
generate_resize_for(Danbooru.config.medium_image_width, source_path)
|
||||
generate_resize_for(Danbooru.config.large_image_width, source_path)
|
||||
end
|
||||
|
||||
def generate_resize_for(width, source_path)
|
||||
return if width.nil?
|
||||
return unless image_width > width
|
||||
|
||||
unless File.exists?(source_path)
|
||||
raise Error.new("file not found")
|
||||
end
|
||||
|
||||
size = Danbooru.reduce_to({:width => image_width, :height => image_height}, {:width => width})
|
||||
|
||||
# If we're not reducing the resolution, only reencode if the source image larger than
|
||||
# 200 kilobytes.
|
||||
if size[:width] == image_width && size[:height] == image_height && File.size?(source_path) < 200.kilobytes
|
||||
return
|
||||
end
|
||||
|
||||
Danbooru.resize(file_ext, source_path, resized_file_path_for(width), size, 90)
|
||||
end
|
||||
end
|
||||
|
||||
module DimensionMethods
|
||||
# Figures out the dimensions of the image.
|
||||
def calculate_dimensions(file_path)
|
||||
image_size = ImageSize.new(File.open(file_path, "rb"))
|
||||
self.image_width = image_size.get_width
|
||||
self.image_height = image_size.get_height
|
||||
end
|
||||
|
||||
# Does this file have image dimensions?
|
||||
def has_dimensions?
|
||||
%w(jpg gif png swf).include?(file_ext)
|
||||
end
|
||||
end
|
||||
|
||||
module ContentTypeMethods
|
||||
def content_type_to_file_ext(content_type)
|
||||
case content_type
|
||||
when "image/jpeg"
|
||||
"jpg"
|
||||
|
||||
when "image/gif"
|
||||
"gif"
|
||||
|
||||
when "image/png"
|
||||
"png"
|
||||
|
||||
when "application/x-shockwave-flash"
|
||||
"swf"
|
||||
|
||||
else
|
||||
"bin"
|
||||
end
|
||||
end
|
||||
|
||||
# Converts a content type string to a file extension
|
||||
def file_ext_to_content_type(file_ext)
|
||||
case file_ext
|
||||
when /\.jpeg$|\.jpg$/
|
||||
"image/jpeg"
|
||||
|
||||
when /\.gif$/
|
||||
"image/gif"
|
||||
|
||||
when /\.png$/
|
||||
"image/png"
|
||||
|
||||
when /\.swf$/
|
||||
"application/x-shockwave-flash"
|
||||
|
||||
else
|
||||
"application/octet-stream"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module FilePathMethods
|
||||
def md5_file_path
|
||||
prefix = Rails.env == "test" ? "test." : ""
|
||||
"#{Rails.root}/public/data/original/#{prefix}#{md5}.#{file_ext}"
|
||||
end
|
||||
|
||||
def resized_file_path_for(width)
|
||||
prefix = Rails.env == "test" ? "test." : ""
|
||||
|
||||
case width
|
||||
when Danbooru.config.small_image_width
|
||||
"#{Rails.root}/public/data/thumb/#{prefix}#{md5}.jpg"
|
||||
|
||||
when Danbooru.config.medium_image_width
|
||||
"#{Rails.root}/public/data/medium/#{prefix}#{md5}.jpg"
|
||||
|
||||
when Danbooru.config.large_image_width
|
||||
"#{Rails.root}/public/data/large/#{prefix}#{md5}.jpg"
|
||||
end
|
||||
end
|
||||
|
||||
def temp_file_path
|
||||
File.join(Dir::tmpdir, "#{Time.now.to_f}.#{$PROCESS_ID}")
|
||||
end
|
||||
end
|
||||
|
||||
module DownloaderMethods
|
||||
# Determines whether the source is downloadable
|
||||
def is_downloadable?
|
||||
source =~ /^http:\/\// && file_path.blank?
|
||||
end
|
||||
|
||||
# Downloads the file to destination_path
|
||||
def download_from_source(destination_path)
|
||||
download = Download.new(source, destination_path)
|
||||
download.download!
|
||||
self.file_path = destination_path
|
||||
self.content_type = download.content_type || file_ext_to_content_type(source)
|
||||
self.file_ext = content_type_to_file_ext(content_type)
|
||||
self.source = download.source
|
||||
end
|
||||
end
|
||||
|
||||
module CgiFileMethods
|
||||
# Moves the cgi file to file_path
|
||||
def convert_cgi_file(destination_path)
|
||||
return if file.blank? || file.size == 0
|
||||
|
||||
if file.local_path
|
||||
FileUtils.mv(file.local_path, destination_path)
|
||||
else
|
||||
File.open(destination_path, 'wb') do |out|
|
||||
out.write(file.read)
|
||||
end
|
||||
end
|
||||
self.file_path = destination_path
|
||||
self.content_type = file.content_type || file_ext_to_content_type(file.original_filename)
|
||||
self.file_ext = content_type_to_file_ext(content_type)
|
||||
end
|
||||
end
|
||||
|
||||
include ResizerMethods
|
||||
include DimensionMethods
|
||||
include ContentTypeMethods
|
||||
include DownloaderMethods
|
||||
include FilePathMethods
|
||||
include CgiFileMethods
|
||||
|
||||
# private
|
||||
def convert_to_post
|
||||
returning Post.new do |p|
|
||||
p.tag_string = tag_string
|
||||
p.md5 = md5
|
||||
p.file_ext = file_ext
|
||||
p.image_width = image_width
|
||||
p.image_height = image_height
|
||||
p.uploader_id = uploader_id
|
||||
p.uploader_ip_addr = uploader_ip_addr
|
||||
p.rating = rating
|
||||
p.source = source
|
||||
p.file_size = file_size
|
||||
end
|
||||
end
|
||||
|
||||
def move_file
|
||||
FileUtils.mv(file_path, md5_file_path)
|
||||
end
|
||||
|
||||
def calculate_file_size(source_path)
|
||||
self.file_size = File.size(source_path)
|
||||
end
|
||||
|
||||
# Calculates the MD5 based on whatever is in temp_file_path
|
||||
def calculate_hash(source_path)
|
||||
self.md5 = Digest::MD5.file(source_path).hexdigest
|
||||
end
|
||||
end
|
||||
@@ -3,267 +3,4 @@ class Post < ActiveRecord::Base
|
||||
set_table_name "deleted_posts"
|
||||
end
|
||||
|
||||
class Pending < ActiveRecord::Base
|
||||
class Error < Exception ; end
|
||||
|
||||
class Download
|
||||
class Error < Exception ; end
|
||||
|
||||
attr_accessible :source, :content_type
|
||||
|
||||
def initialize(source, file_path)
|
||||
@source = source
|
||||
@file_path = file_path
|
||||
end
|
||||
|
||||
# Downloads to @file_path
|
||||
def download!
|
||||
http_get_streaming(@source) do |response|
|
||||
self.content_type = response["Content-Type"]
|
||||
File.open(@file_path, "wb") do |out|
|
||||
response.read_body(out)
|
||||
end
|
||||
end
|
||||
@source = fix_image_board_sources(@source)
|
||||
end
|
||||
|
||||
private
|
||||
def handle_pixiv(source, headers)
|
||||
if source =~ /pixiv\.net/
|
||||
headers["Referer"] = "http://www.pixiv.net"
|
||||
|
||||
# Don't download the small version
|
||||
if source =~ %r!(/img/.+?/.+?)_m.+$!
|
||||
match = $1
|
||||
source.sub!(match + "_m", match)
|
||||
end
|
||||
end
|
||||
|
||||
source
|
||||
end
|
||||
|
||||
def http_get_streaming(source, options = {})
|
||||
max_size = options[:max_size] || Danbooru.config.max_file_size
|
||||
max_size = nil if max_size == 0 # unlimited
|
||||
limit = 4
|
||||
|
||||
while true
|
||||
url = URI.parse(source)
|
||||
|
||||
unless url.is_a?(URI::HTTP)
|
||||
raise Error.new("URL must be HTTP")
|
||||
end
|
||||
|
||||
Net::HTTP.start(url.host, url.port) do |http|
|
||||
http.read_timeout = 10
|
||||
headers = {
|
||||
"User-Agent" => "#{Danbooru.config.safe_app_name}/#{Danbooru.config.version}"
|
||||
}
|
||||
source = handle_pixiv(source, headers)
|
||||
http.request_get(url.request_uri, headers) do |res|
|
||||
case res
|
||||
when Net::HTTPSuccess then
|
||||
if max_size
|
||||
len = res["Content-Length"]
|
||||
raise Error.new("File is too large (#{len} bytes)") if len && len.to_i > max_size
|
||||
end
|
||||
yield(res)
|
||||
return
|
||||
|
||||
when Net::HTTPRedirection then
|
||||
if limit == 0 then
|
||||
raise Error.new("Too many redirects")
|
||||
end
|
||||
source = res["location"]
|
||||
limit -= 1
|
||||
|
||||
else
|
||||
raise Error.new("HTTP error code: #{res.code} #{res.message}")
|
||||
end
|
||||
end # http.request_get
|
||||
end # http.start
|
||||
end # while
|
||||
end # def
|
||||
|
||||
def fix_image_board_sources(source)
|
||||
if source =~ /\/src\/\d{12,}|urnc\.yi\.org|yui\.cynthia\.bne\.jp/
|
||||
"Image board"
|
||||
else
|
||||
source
|
||||
end
|
||||
end
|
||||
end # download
|
||||
|
||||
set_table_name "pending_posts"
|
||||
belongs_to :post
|
||||
attr_accessible :file, :image_width, :image_height
|
||||
|
||||
def process!
|
||||
update_attribute(:status, "processing")
|
||||
|
||||
if file
|
||||
convert_cgi_file(temp_file_path)
|
||||
elsif is_downloadable?
|
||||
download_from_source(temp_file_path)
|
||||
end
|
||||
|
||||
calculate_hash(temp_file_path)
|
||||
move_file
|
||||
calculate_dimensions
|
||||
generate_resizes
|
||||
convert_to_post
|
||||
update_attribute(:status, "finished")
|
||||
end
|
||||
|
||||
private
|
||||
def generate_resizes
|
||||
generate_resize_for(Danbooru.config.small_image_width)
|
||||
generate_resize_for(Danbooru.config.medium_image_width)
|
||||
generate_resize_for(Danbooru.config.large_image_width)
|
||||
end
|
||||
|
||||
def create_resize_for(width)
|
||||
return if width.nil?
|
||||
return unless image_width > width
|
||||
|
||||
unless File.exists?(final_file_path)
|
||||
raise Error.new("file not found")
|
||||
end
|
||||
|
||||
size = Danbooru.reduce_to({:width => image_width, :height => image_height}, {:width => width})
|
||||
|
||||
# If we're not reducing the resolution, only reencode if the source image larger than
|
||||
# 200 kilobytes.
|
||||
if size[:width] == width && size[:height] == height && File.size?(path) > 200.kilobytes
|
||||
return true
|
||||
end
|
||||
|
||||
begin
|
||||
Danbooru.resize(file_ext, final_file_path, resize_file_path_for(width), size, 90)
|
||||
rescue Exception => x
|
||||
errors.add "sample", "couldn't be created: #{x}"
|
||||
return false
|
||||
end
|
||||
|
||||
self.sample_width = size[:width]
|
||||
self.sample_height = size[:height]
|
||||
return true
|
||||
end
|
||||
|
||||
def resize_file_path_for(width)
|
||||
case width
|
||||
when Danbooru.config.small_image_width
|
||||
"#{Rails.root}/public/data/preview"
|
||||
|
||||
when Danbooru.config.medium_image_width
|
||||
"#{Rails.root}/public/data/medium"
|
||||
|
||||
when Danbooru.config.large_image_width
|
||||
"#{Rails.root}/public/data/large"
|
||||
end
|
||||
end
|
||||
|
||||
def convert_to_post
|
||||
returning Post.new do |p|
|
||||
p.tag_string = tag_string
|
||||
p.md5 = md5
|
||||
p.file_ext = file_ext
|
||||
p.image_width = image_width
|
||||
p.image_height = image_height
|
||||
p.uploader_id = uploader_id
|
||||
p.uploader_ip_addr = uploader_ip_addr
|
||||
p.rating = rating
|
||||
p.source = source
|
||||
end
|
||||
end
|
||||
|
||||
def calculate_dimensions(post)
|
||||
if has_dimensions?
|
||||
image_size = ImageSize.new(File.open(final_file_path, "rb"))
|
||||
self.image_width = image_size.get_width
|
||||
self.image_hegiht = image_hegiht.get_height
|
||||
end
|
||||
end
|
||||
|
||||
def has_dimensions?
|
||||
%w(jpg gif png swf).include?(file_ext)
|
||||
end
|
||||
|
||||
def move_file
|
||||
FileUtils.mv(temp_file_path, final_file_path)
|
||||
end
|
||||
|
||||
def final_file_path
|
||||
"#{Rails.root}/public/data/original/#{md5}.#{file_ext}"
|
||||
end
|
||||
|
||||
# Calculates the MD5 based on whatever is in temp_file_path
|
||||
def calculate_hash(file_path)
|
||||
self.md5 = Digest::MD5.file(file_path).hexdigest
|
||||
end
|
||||
|
||||
# Moves the cgi file to file_path
|
||||
def convert_cgi_file(file_path)
|
||||
return if file.blank? || file.size == 0
|
||||
|
||||
if file.local_path
|
||||
FileUtils.mv(file.local_path, file_path)
|
||||
else
|
||||
File.open(file_path, 'wb') do |out|
|
||||
out.write(file.read)
|
||||
end
|
||||
end
|
||||
self.file_ext = content_type_to_file_ext(file.content_type) || find_ext(file.original_filename)
|
||||
end
|
||||
|
||||
# Determines whether the source is downloadable
|
||||
def is_downloadable?
|
||||
source =~ /^http:\/\// && file.blank?
|
||||
end
|
||||
|
||||
# Downloads the file to file_path
|
||||
def download_from_source(file_path)
|
||||
download = Download.new(source, file_path)
|
||||
download.download!
|
||||
self.file_ext = content_type_to_file_ext(download.content_type) || find_ext(source)
|
||||
end
|
||||
|
||||
# Converts a content type string to a file extension
|
||||
def content_type_to_file_ext(content_type)
|
||||
case content_type
|
||||
when /jpeg/
|
||||
return "jpg"
|
||||
|
||||
when /gif/
|
||||
return "gif"
|
||||
|
||||
when /png/
|
||||
return "png"
|
||||
|
||||
when /x-shockwave-flash/
|
||||
return "swf"
|
||||
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
# Determines the file extention based on a path, normalizing if necessary
|
||||
def find_ext(file_path)
|
||||
ext = File.extname(file_path)
|
||||
if ext.blank?
|
||||
return "txt"
|
||||
else
|
||||
ext = ext[1..-1].downcase
|
||||
ext = "jpg" if ext == "jpeg"
|
||||
return ext
|
||||
end
|
||||
end
|
||||
|
||||
# Path to a temporary file
|
||||
def temp_file_path
|
||||
@temp_file ||= Tempfile.new("danbooru-upload-#{$PROCESS_ID}")
|
||||
@temp_file.path
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user