From 6db66f71836862931d46f0f04e7aa4955b383e6e Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Fri, 29 Jul 2011 15:28:59 +0000 Subject: [PATCH] Fixed: file uploads broken by r6312 (#8912). git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@6320 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/attachment.rb | 3 ++- test/unit/attachment_test.rb | 23 ++++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/app/models/attachment.rb b/app/models/attachment.rb index 7e3a91581..d16d5cddf 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -74,7 +74,7 @@ class Attachment < ActiveRecord::Base # and computes its MD5 hash def before_save if @temp_file && (@temp_file.size > 0) - logger.debug("saving '#{self.diskfile}'") + logger.info("Saving attachment '#{self.diskfile}' (#{@temp_file.size} bytes)") md5 = Digest::MD5.new File.open(diskfile, "wb") do |f| buffer = "" @@ -85,6 +85,7 @@ class Attachment < ActiveRecord::Base end self.digest = md5.hexdigest end + @temp_file = nil # Don't save the content type if it's longer than the authorized length if self.content_type && self.content_type.length > 255 self.content_type = nil diff --git a/test/unit/attachment_test.rb b/test/unit/attachment_test.rb index 4a57669c1..d433ae757 100644 --- a/test/unit/attachment_test.rb +++ b/test/unit/attachment_test.rb @@ -36,6 +36,7 @@ class AttachmentTest < ActiveSupport::TestCase assert_equal 0, a.downloads assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest assert File.exist?(a.diskfile) + assert_equal 59, File.size(a.diskfile) end def test_create_should_auto_assign_content_type @@ -64,7 +65,27 @@ class AttachmentTest < ActiveSupport::TestCase assert_equal 'cbb5b0f30978ba03731d61f9f6d10011', Attachment.disk_filename("test_accentué.ça")[13..-1] end - context "Attachmnet#attach_files" do + context "Attachmnet.attach_files" do + should "attach the file" do + issue = Issue.first + assert_difference 'Attachment.count' do + Attachment.attach_files(issue, + '1' => { + 'file' => uploaded_test_file('testfile.txt', 'text/plain'), + 'description' => 'test' + }) + end + + attachment = Attachment.first(:order => 'id DESC') + assert_equal issue, attachment.container + assert_equal 'testfile.txt', attachment.filename + assert_equal 59, attachment.filesize + assert_equal 'test', attachment.description + assert_equal 'text/plain', attachment.content_type + assert File.exists?(attachment.diskfile) + assert_equal 59, File.size(attachment.diskfile) + end + should "add unsaved files to the object as unsaved attachments" do # Max size of 0 to force Attachment creation failures with_settings(:attachment_max_size => 0) do -- 2.39.5