forked from e621ng/e621ng
added full pending post unit test
This commit is contained in:
parent
07b0e213f3
commit
39424ca52b
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
|
||||
|
@ -1,6 +1,6 @@
|
||||
require 'fileutils'
|
||||
|
||||
FileUtils.mkdir_p("#{Rails.root}/public/data/size_thumbnail")
|
||||
FileUtils.mkdir_p("#{Rails.root}/public/data/size_medium")
|
||||
FileUtils.mkdir_p("#{Rails.root}/public/data/size_large")
|
||||
FileUtils.mkdir_p("#{Rails.root}/public/data/size_original")
|
||||
FileUtils.mkdir_p("#{Rails.root}/public/data/thumb")
|
||||
FileUtils.mkdir_p("#{Rails.root}/public/data/medium")
|
||||
FileUtils.mkdir_p("#{Rails.root}/public/data/large")
|
||||
FileUtils.mkdir_p("#{Rails.root}/public/data/original")
|
||||
|
@ -83,6 +83,8 @@ CREATE TABLE pending_posts (
|
||||
created_at timestamp without time zone,
|
||||
updated_at timestamp without time zone,
|
||||
source character varying(255),
|
||||
file_path character varying(255),
|
||||
content_type character varying(255),
|
||||
rating character(1) NOT NULL,
|
||||
uploader_id integer NOT NULL,
|
||||
uploader_ip_addr inet NOT NULL,
|
||||
@ -452,8 +454,8 @@ CREATE TRIGGER trigger_posts_on_tag_index_update
|
||||
|
||||
INSERT INTO schema_migrations (version) VALUES ('20100204211522');
|
||||
|
||||
INSERT INTO schema_migrations (version) VALUES ('20100205162521');
|
||||
|
||||
INSERT INTO schema_migrations (version) VALUES ('20100204214746');
|
||||
|
||||
INSERT INTO schema_migrations (version) VALUES ('20100205162521');
|
||||
|
||||
INSERT INTO schema_migrations (version) VALUES ('20100205224030');
|
@ -3,6 +3,9 @@ class CreatePendingPosts < ActiveRecord::Migration
|
||||
create_table :pending_posts do |t|
|
||||
t.timestamps
|
||||
t.column :source, :string
|
||||
t.column :file_path, :string
|
||||
t.column :content_type, :string
|
||||
|
||||
t.column :rating, :character, :null => false
|
||||
t.column :uploader_id, :integer, :null => false
|
||||
t.column :uploader_ip_addr, "inet", :null => false
|
||||
|
43
test/factories/pending_post.rb
Normal file
43
test/factories/pending_post.rb
Normal file
@ -0,0 +1,43 @@
|
||||
require 'fileutils'
|
||||
|
||||
Factory.define(:pending_post) do |f|
|
||||
f.rating "s"
|
||||
f.uploader {|x| x.association(:user)}
|
||||
f.uploader_ip_addr "127.0.0.1"
|
||||
f.tag_string "special"
|
||||
f.status "pending"
|
||||
end
|
||||
|
||||
Factory.define(:downloadable_pending_post, :parent => :pending_post) do |f|
|
||||
f.source "http://www.google.com/intl/en_ALL/images/logo.gif"
|
||||
end
|
||||
|
||||
Factory.define(:uploaded_jpg_pending_post, :parent => :pending_post) do |f|
|
||||
f.file_path do
|
||||
FileUtils.cp("#{Rails.root}/test/files/test.jpg", "#{Rails.root}/tmp")
|
||||
"#{Rails.root}/tmp/test.jpg"
|
||||
end
|
||||
end
|
||||
|
||||
Factory.define(:uploaded_large_jpg_pending_post, :parent => :pending_post) do |f|
|
||||
f.file_ext "jpg"
|
||||
f.content_type "image/jpeg"
|
||||
f.file_path do
|
||||
FileUtils.cp("#{Rails.root}/test/files/test-large.jpg", "#{Rails.root}/tmp")
|
||||
"#{Rails.root}/tmp/test-large.jpg"
|
||||
end
|
||||
end
|
||||
|
||||
Factory.define(:uploaded_png_pending_post, :parent => :pending_post) do |f|
|
||||
f.file_path do
|
||||
FileUtils.cp("#{Rails.root}/test/files/test.png", "#{Rails.root}/tmp")
|
||||
"#{Rails.root}/tmp/test.png"
|
||||
end
|
||||
end
|
||||
|
||||
Factory.define(:uploaded_gif_pending_post, :parent => :pending_post) do |f|
|
||||
f.file_path do
|
||||
FileUtils.cp("#{Rails.root}/test/files/test.gif", "#{Rails.root}/tmp")
|
||||
"#{Rails.root}/tmp/test.gif"
|
||||
end
|
||||
end
|
BIN
test/files/test-large.jpg
Normal file
BIN
test/files/test-large.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 89 KiB |
BIN
test/files/test.gif
Normal file
BIN
test/files/test.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 50 KiB |
BIN
test/files/test.jpg
Normal file
BIN
test/files/test.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 27 KiB |
BIN
test/files/test.png
Normal file
BIN
test/files/test.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 436 KiB |
41
test/unit/download_test.rb
Normal file
41
test/unit/download_test.rb
Normal file
@ -0,0 +1,41 @@
|
||||
require File.dirname(__FILE__) + '/../test_helper'
|
||||
|
||||
class DownloadTest < ActiveSupport::TestCase
|
||||
context "A post download" do
|
||||
setup do
|
||||
@source = "http://www.google.com/intl/en_ALL/images/logo.gif"
|
||||
@tempfile = Tempfile.new("danbooru-test")
|
||||
@download = Download.new(@source, @tempfile.path)
|
||||
end
|
||||
|
||||
teardown do
|
||||
@tempfile.close
|
||||
end
|
||||
|
||||
should "stream a file from an HTTP source" do
|
||||
@download.http_get_streaming(@download.source) do |resp|
|
||||
assert_equal("200", resp.code)
|
||||
assert(resp["Content-Length"].to_i > 0, "File should be larger than 0 bytes")
|
||||
end
|
||||
end
|
||||
|
||||
should "throw an exception when the file is larger than the maximum" do
|
||||
assert_raise(Download::Error) do
|
||||
@download.http_get_streaming(@download.source, :max_size => 1) do |resp|
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
should "store the file in the tempfile path" do
|
||||
@download.download!
|
||||
assert_equal(@source, @download.source)
|
||||
assert(File.exists?(@tempfile.path), "temp file should exist")
|
||||
assert(File.size(@tempfile.path) > 0, "should have data")
|
||||
end
|
||||
|
||||
should "initialize the content type" do
|
||||
@download.download!
|
||||
assert_match(/image\/gif/, @download.content_type)
|
||||
end
|
||||
end
|
||||
end
|
@ -1,8 +1,132 @@
|
||||
require 'test_helper'
|
||||
require File.dirname(__FILE__) + '/../test_helper'
|
||||
|
||||
class PendingPostTest < ActiveSupport::TestCase
|
||||
# Replace this with your real tests.
|
||||
test "the truth" do
|
||||
assert true
|
||||
context "A pending post" do
|
||||
teardown do
|
||||
FileUtils.rm_f(Dir.glob("#{Rails.root}/tmp/test.*"))
|
||||
end
|
||||
|
||||
context "image size calculator" do
|
||||
should "discover the dimensions for a JPG" do
|
||||
@pending_post = Factory.create(:uploaded_jpg_pending_post)
|
||||
assert_nothing_raised {@pending_post.calculate_dimensions(@pending_post.file_path)}
|
||||
assert_equal(500, @pending_post.image_width)
|
||||
assert_equal(335, @pending_post.image_height)
|
||||
end
|
||||
|
||||
should "discover the dimensions for a PNG" do
|
||||
@pending_post = Factory.create(:uploaded_png_pending_post)
|
||||
assert_nothing_raised {@pending_post.calculate_dimensions(@pending_post.file_path)}
|
||||
assert_equal(768, @pending_post.image_width)
|
||||
assert_equal(1024, @pending_post.image_height)
|
||||
end
|
||||
|
||||
should "discover the dimensions for a GIF" do
|
||||
@pending_post = Factory.create(:uploaded_gif_pending_post)
|
||||
assert_nothing_raised {@pending_post.calculate_dimensions(@pending_post.file_path)}
|
||||
assert_equal(400, @pending_post.image_width)
|
||||
assert_equal(400, @pending_post.image_height)
|
||||
end
|
||||
end
|
||||
|
||||
context "content type calculator" do
|
||||
should "know how to parse jpeg, png, gif, and swf file extensions" do
|
||||
@pending_post = Factory.create(:uploaded_jpg_pending_post)
|
||||
assert_equal("image/jpeg", @pending_post.file_ext_to_content_type("test.jpeg"))
|
||||
assert_equal("image/gif", @pending_post.file_ext_to_content_type("test.gif"))
|
||||
assert_equal("image/png", @pending_post.file_ext_to_content_type("test.png"))
|
||||
assert_equal("application/x-shockwave-flash", @pending_post.file_ext_to_content_type("test.swf"))
|
||||
assert_equal("application/octet-stream", @pending_post.file_ext_to_content_type(""))
|
||||
end
|
||||
|
||||
should "know how to parse jpeg, png, gif, and swf content types" do
|
||||
@pending_post = Factory.create(:uploaded_jpg_pending_post)
|
||||
assert_equal("jpg", @pending_post.content_type_to_file_ext("image/jpeg"))
|
||||
assert_equal("gif", @pending_post.content_type_to_file_ext("image/gif"))
|
||||
assert_equal("png", @pending_post.content_type_to_file_ext("image/png"))
|
||||
assert_equal("swf", @pending_post.content_type_to_file_ext("application/x-shockwave-flash"))
|
||||
assert_equal("bin", @pending_post.content_type_to_file_ext(""))
|
||||
end
|
||||
end
|
||||
|
||||
context "downloader" do
|
||||
should "initialize the final path and content type after downloading a file" do
|
||||
@pending_post = Factory.create(:downloadable_pending_post)
|
||||
path = "#{Rails.root}/tmp/test.download.jpg"
|
||||
assert_nothing_raised {@pending_post.download_from_source(path)}
|
||||
assert(File.exists?(path))
|
||||
assert_equal(8558, File.size(path))
|
||||
assert_equal("image/gif", @pending_post.content_type)
|
||||
assert_equal(path, @pending_post.file_path)
|
||||
assert_equal("gif", @pending_post.file_ext)
|
||||
end
|
||||
end
|
||||
|
||||
context "file processor" do
|
||||
should "parse and process a cgi file representation" do
|
||||
FileUtils.cp("#{Rails.root}/test/files/test.jpg", "#{Rails.root}/tmp")
|
||||
@pending_post = PendingPost.new(:file => upload_jpeg("#{Rails.root}/tmp/test.jpg"))
|
||||
assert_nothing_raised {@pending_post.convert_cgi_file("#{Rails.root}/tmp/test.converted.jpg")}
|
||||
assert_equal("image/jpeg", @pending_post.content_type)
|
||||
assert_equal("#{Rails.root}/tmp/test.converted.jpg", @pending_post.file_path)
|
||||
assert(File.exists?("#{Rails.root}/tmp/test.converted.jpg"))
|
||||
assert_equal(28086, File.size("#{Rails.root}/tmp/test.converted.jpg"))
|
||||
assert_equal("jpg", @pending_post.file_ext)
|
||||
end
|
||||
end
|
||||
|
||||
context "hash calculator" do
|
||||
should "caculate the hash" do
|
||||
@pending_post = Factory.create(:uploaded_jpg_pending_post)
|
||||
@pending_post.calculate_hash(@pending_post.file_path)
|
||||
assert_equal("ecef68c44edb8a0d6a3070b5f8e8ee76", @pending_post.md5)
|
||||
end
|
||||
end
|
||||
|
||||
context "resizer" do
|
||||
teardown do
|
||||
FileUtils.rm_f(Dir.glob("#{Rails.root}/public/data/thumb/test.*.jpg"))
|
||||
FileUtils.rm_f(Dir.glob("#{Rails.root}/public/data/medium/test.*.jpg"))
|
||||
FileUtils.rm_f(Dir.glob("#{Rails.root}/public/data/large/test.*.jpg"))
|
||||
FileUtils.rm_f(Dir.glob("#{Rails.root}/public/data/original/test.*.jpg"))
|
||||
end
|
||||
|
||||
should "generate several resized versions of the image" do
|
||||
@pending_post = Factory.create(:uploaded_large_jpg_pending_post)
|
||||
@pending_post.calculate_hash(@pending_post.file_path)
|
||||
@pending_post.calculate_dimensions(@pending_post.file_path)
|
||||
assert_nothing_raised {@pending_post.generate_resizes(@pending_post.file_path)}
|
||||
assert(File.exists?(@pending_post.resized_file_path_for(Danbooru.config.small_image_width)))
|
||||
assert_equal(6556, File.size(@pending_post.resized_file_path_for(Danbooru.config.small_image_width)))
|
||||
assert(File.exists?(@pending_post.resized_file_path_for(Danbooru.config.medium_image_width)))
|
||||
assert_equal(39411, File.size(@pending_post.resized_file_path_for(Danbooru.config.medium_image_width)))
|
||||
assert(File.exists?(@pending_post.resized_file_path_for(Danbooru.config.large_image_width)))
|
||||
assert_equal(179324, File.size(@pending_post.resized_file_path_for(Danbooru.config.large_image_width)))
|
||||
end
|
||||
end
|
||||
|
||||
should "process completely for a downloaded image" do
|
||||
@pending_post = Factory.create(:downloadable_pending_post,
|
||||
:rating => "s",
|
||||
:uploader_ip_addr => "127.0.0.1",
|
||||
:tag_string => "hoge foo"
|
||||
)
|
||||
assert_difference("Post.count") do
|
||||
assert_nothing_raised {@pending_post.process!}
|
||||
end
|
||||
|
||||
post = Post.last
|
||||
assert_equal("hoge foo", post.tag_string)
|
||||
assert_equal("s", post.rating)
|
||||
assert_equal(@pending_post.uploader_id, post.uploader_id)
|
||||
assert_equal("127.0.0.1", post.uploader_ip_addr)
|
||||
assert_equal(@pending_post.md5, post.md5)
|
||||
assert_equal("gif", post.file_ext)
|
||||
assert_equal(276, post.image_width)
|
||||
assert_equal(110, post.image_height)
|
||||
assert_equal(8558, post.file_size)
|
||||
assert_equal(post.id, @pending_post.post_id)
|
||||
assert_equal("finished", @pending_post.status)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -1,41 +1,5 @@
|
||||
require File.dirname(__FILE__) + '/../test_helper'
|
||||
|
||||
class PostTest < ActiveSupport::TestCase
|
||||
context "A post download" do
|
||||
setup do
|
||||
@source = "http://www.google.com/intl/en_ALL/images/logo.gif"
|
||||
@tempfile = Tempfile.new("danbooru-test")
|
||||
@download = ::Post::Pending::Download.new(@source, @tempfile.path)
|
||||
end
|
||||
|
||||
teardown do
|
||||
@tempfile.close
|
||||
end
|
||||
|
||||
should "stream a file from an HTTP source" do
|
||||
@download.http_get_streaming(@download.source) do |resp|
|
||||
assert_equal("200", resp.code)
|
||||
assert(resp["Content-Length"].to_i > 0, "File should be larger than 0 bytes")
|
||||
end
|
||||
end
|
||||
|
||||
should "throw an exception when the file is larger than the maximum" do
|
||||
assert_raise(Post::Pending::Download::Error) do
|
||||
@download.http_get_streaming(@download.source, :max_size => 1) do |resp|
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
should "store the file in the tempfile path" do
|
||||
@download.download!
|
||||
assert_equal(@source, @download.source)
|
||||
assert(File.exists?(@tempfile.path), "temp file should exist")
|
||||
assert(File.size(@tempfile.path) > 0, "should have data")
|
||||
end
|
||||
|
||||
should "initialize the content type" do
|
||||
@download.download!
|
||||
assert_match(/text\/html/, @download.content_type)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user