diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2009-04-10 16:37:42 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2009-04-10 16:37:42 +0000 |
commit | a59854ef9d41d0ec79ed61f197fe4ebd7f3385fa (patch) | |
tree | 4ae7a4972c525cc25743247a0dbc41eeec676ae3 /app/models/attachment.rb | |
parent | 10cbdf5d96515ecfc74f84a7475327bcca69f28b (diff) | |
download | redmine-a59854ef9d41d0ec79ed61f197fe4ebd7f3385fa.tar.gz redmine-a59854ef9d41d0ec79ed61f197fe4ebd7f3385fa.zip |
Fixes memory consumption on file upload (#3116).
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@2670 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/models/attachment.rb')
-rw-r--r-- | app/models/attachment.rb | 19 |
1 files changed, 9 insertions, 10 deletions
diff --git a/app/models/attachment.rb b/app/models/attachment.rb index bffd64bf9..97471d739 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -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 |