summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2010-02-28 11:12:40 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2010-02-28 11:12:40 +0000
commit241f79ac06b2c66e2a4fb164c970263f491b22d6 (patch)
treefff931e9045372542253a799559aa602fa24a1c5
parent82310d3162ed4353dd37d16890eb691c38cd32d9 (diff)
downloadredmine-241f79ac06b2c66e2a4fb164c970263f491b22d6.tar.gz
redmine-241f79ac06b2c66e2a4fb164c970263f491b22d6.zip
Fixed: attachments with the same name at the same time overwrite (#3691).
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3511 e93f8b46-1217-0410-a6f0-8f06a7374b81
-rw-r--r--app/models/attachment.rb14
-rw-r--r--test/unit/attachment_test.rb10
2 files changed, 19 insertions, 5 deletions
diff --git a/app/models/attachment.rb b/app/models/attachment.rb
index e44b162ad..ace291ec1 100644
--- a/app/models/attachment.rb
+++ b/app/models/attachment.rb
@@ -147,14 +147,18 @@ private
# Returns an ASCII or hashed filename
def self.disk_filename(filename)
- df = DateTime.now.strftime("%y%m%d%H%M%S") + "_"
+ timestamp = DateTime.now.strftime("%y%m%d%H%M%S")
+ ascii = ''
if filename =~ %r{^[a-zA-Z0-9_\.\-]*$}
- df << filename
+ ascii = filename
else
- df << Digest::MD5.hexdigest(filename)
+ ascii = Digest::MD5.hexdigest(filename)
# keep the extension if any
- df << $1 if filename =~ %r{(\.[a-zA-Z0-9]+)$}
+ ascii << $1 if filename =~ %r{(\.[a-zA-Z0-9]+)$}
end
- df
+ while File.exist?(File.join(@@storage_path, "#{timestamp}_#{ascii}"))
+ timestamp.succ!
+ end
+ "#{timestamp}_#{ascii}"
end
end
diff --git a/test/unit/attachment_test.rb b/test/unit/attachment_test.rb
index 8c25adb41..7c6a9b4b1 100644
--- a/test/unit/attachment_test.rb
+++ b/test/unit/attachment_test.rb
@@ -46,6 +46,16 @@ class AttachmentTest < ActiveSupport::TestCase
assert_equal 'text/plain', a.content_type
end
+ def test_identical_attachments_at_the_same_time_should_not_overwrite
+ a1 = Attachment.create!(:container => Issue.find(1),
+ :file => uploaded_test_file("testfile.txt", ""),
+ :author => User.find(1))
+ a2 = Attachment.create!(:container => Issue.find(1),
+ :file => uploaded_test_file("testfile.txt", ""),
+ :author => User.find(1))
+ assert a1.disk_filename != a2.disk_filename
+ end
+
def test_diskfilename
assert Attachment.disk_filename("test_file.txt") =~ /^\d{12}_test_file.txt$/
assert_equal 'test_file.txt', Attachment.disk_filename("test_file.txt")[13..-1]