]> source.dussan.org Git - redmine.git/commitdiff
Fixes memory consumption on file upload (#3116).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Fri, 10 Apr 2009 16:37:42 +0000 (16:37 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Fri, 10 Apr 2009 16:37:42 +0000 (16:37 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@2670 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/attachment.rb
test/unit/attachment_test.rb

index bffd64bf9d31318d0407539cbea6a33f30225744..97471d7390e316d14699433d6533e2e5e39ca7b2 100644 (file)
@@ -67,14 +67,20 @@ class Attachment < ActiveRecord::Base
     nil
   end
 
-  # Copy temp file to its final location
+  # Copies the temporary file to its final location
+  # and computes its MD5 hash
   def before_save
     if @temp_file && (@temp_file.size > 0)
       logger.debug("saving '#{self.diskfile}'")
+      md5 = Digest::MD5.new
       File.open(diskfile, "wb") do |f| 
-        f.write(@temp_file.read)
+        buffer = ""
+        while (buffer = @temp_file.read(8192))
+          f.write(buffer)
+          md5.update(buffer)
+        end
       end
-      self.digest = self.class.digest(diskfile)
+      self.digest = md5.hexdigest
     end
     # Don't save the content type if it's longer than the authorized length
     if self.content_type && self.content_type.length > 255
@@ -143,11 +149,4 @@ private
     end
     df
   end
-  
-  # Returns the MD5 digest of the file at given path
-  def self.digest(filename)
-    File.open(filename, 'rb') do |f|
-      Digest::MD5.hexdigest(f.read)
-    end
-  end
 end
index a8c5332381eca0c87e030df0492efda106fbcb70..ae92991f85c24b8d109b73312c99977ffb1daf8a 100644 (file)
@@ -22,6 +22,19 @@ class AttachmentTest < Test::Unit::TestCase
   
   def setup
   end
+
+  def test_create
+    a = Attachment.new(:container => Issue.find(1),
+                       :file => test_uploaded_file("testfile.txt", "text/plain"),
+                       :author => User.find(1))
+    assert a.save
+    assert_equal 'testfile.txt', a.filename
+    assert_equal 59, a.filesize
+    assert_equal 'text/plain', a.content_type
+    assert_equal 0, a.downloads
+    assert_equal Digest::MD5.hexdigest(test_uploaded_file("testfile.txt", "text/plain").read), a.digest
+    assert File.exist?(a.diskfile)
+  end
   
   def test_diskfilename
     assert Attachment.disk_filename("test_file.txt") =~ /^\d{12}_test_file.txt$/
@@ -30,8 +43,4 @@ class AttachmentTest < Test::Unit::TestCase
     assert_equal 'f8139524ebb8f32e51976982cd20a85d', Attachment.disk_filename("test_accentué")[13..-1]
     assert_equal 'cbb5b0f30978ba03731d61f9f6d10011', Attachment.disk_filename("test_accentué.ça")[13..-1]
   end
-  
-  def test_digest
-    assert_equal '1478adae0d4eb06d35897518540e25d6', Attachment.digest(Test::Unit::TestCase.fixture_path + "/files/testfile.txt")
-  end
 end