From c6d4a13e7d07414ba0bf514072c988b1f2e3d219 Mon Sep 17 00:00:00 2001 From: albert Date: Tue, 9 Feb 2010 01:11:42 -0500 Subject: [PATCH] bug fixes with uploaded file pending posts --- app/models/pending_post.rb | 18 +++++++++++------- app/models/post.rb | 6 +++++- test/unit/pending_post_test.rb | 31 +++++++++++++++++++++++++++---- 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/app/models/pending_post.rb b/app/models/pending_post.rb index 22b9a9417..f83d8d749 100644 --- a/app/models/pending_post.rb +++ b/app/models/pending_post.rb @@ -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? + update_attribute(:status, "processing") + 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 diff --git a/app/models/post.rb b/app/models/post.rb index 20b6c07ef..af9fb82f3 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -2,5 +2,9 @@ class Post < ActiveRecord::Base class Deletion < 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 diff --git a/test/unit/pending_post_test.rb b/test/unit/pending_post_test.rb index 5fb0a352b..d1d0bad83 100644 --- a/test/unit/pending_post_test.rb +++ b/test/unit/pending_post_test.rb @@ -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