bug fixes with uploaded file pending posts

This commit is contained in:
albert 2010-02-09 01:11:42 -05:00
parent 39424ca52b
commit c6d4a13e7d
3 changed files with 43 additions and 12 deletions

View File

@ -6,10 +6,14 @@ class PendingPost < ActiveRecord::Base
attr_accessor :file, :image_width, :image_height, :file_ext, :md5, :file_size
belongs_to :uploader, :class_name => "User"
before_save :convert_cgi_file
def process!
update_attribute(:status, "processing")
download_from_source(temp_file_path) if is_downloadable?
if is_downloadable?
download_from_source(temp_file_path)
end
self.file_ext = content_type_to_file_ext(content_type)
calculate_hash(file_path)
calculate_file_size(file_path)
calculate_dimensions(file_path) if has_dimensions?
@ -150,18 +154,18 @@ class PendingPost < ActiveRecord::Base
end
module CgiFileMethods
# Moves the cgi file to file_path
def convert_cgi_file(destination_path)
def convert_cgi_file
return if file.blank? || file.size == 0
if file.local_path
FileUtils.mv(file.local_path, destination_path)
self.file_path = file.local_path
else
File.open(destination_path, 'wb') do |out|
self.file_path = temp_file_path
File.open(file_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

View File

@ -3,4 +3,8 @@ class Post < ActiveRecord::Base
set_table_name "deleted_posts"
end
def file_path
prefix = Rails.env == "test" ? "test." : ""
"#{Rails.root}/public/data/original/#{prefix}#{md5}.#{file_ext}"
end
end

View File

@ -66,11 +66,10 @@ class PendingPostTest < ActiveSupport::TestCase
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_nothing_raised {@pending_post.convert_cgi_file}
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(File.exists?(@pending_post.file_path))
assert_equal(28086, File.size(@pending_post.file_path))
assert_equal("jpg", @pending_post.file_ext)
end
end
@ -129,4 +128,28 @@ class PendingPostTest < ActiveSupport::TestCase
assert_equal("finished", @pending_post.status)
end
end
should "process completely for an uploaded image" do
@pending_post = Factory.create(:uploaded_jpg_pending_post,
:rating => "s",
:uploader_ip_addr => "127.0.0.1",
:tag_string => "hoge foo"
)
@pending_post.file = upload_jpeg("#{Rails.root}/test/files/test.jpg")
@pending_post.convert_cgi_file
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("jpg", post.file_ext)
assert(File.exists?(post.file_path))
assert_equal(post.id, @pending_post.post_id)
assert_equal("finished", @pending_post.status)
end
end