]> source.dussan.org Git - redmine.git/commitdiff
Merged r20812 from trunk to 4.1-stable (#34479).
authorGo MAEDA <maeda@farend.jp>
Wed, 17 Mar 2021 13:53:24 +0000 (13:53 +0000)
committerGo MAEDA <maeda@farend.jp>
Wed, 17 Mar 2021 13:53:24 +0000 (13:53 +0000)
git-svn-id: http://svn.redmine.org/redmine/branches/4.1-stable@20813 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/attachment.rb

index cf0ef576507a2287d68e6d446e149eeac2f979bd..e91d7f8c4b75ad3a9fc9d9dd8a9323868d4c127f 100644 (file)
@@ -436,16 +436,15 @@ class Attachment < ActiveRecord::Base
   private
 
   def reuse_existing_file_if_possible
-    original_diskfile = nil
+    original_diskfile = diskfile
+    original_filename = disk_filename
     reused = with_lock do
       if existing = Attachment
                       .where(digest: self.digest, filesize: self.filesize)
-                      .where('id <> ? and disk_filename <> ?',
-                             self.id, self.disk_filename)
+                      .where.not(disk_filename: original_filename)
                       .order(:id)
                       .last
         existing.with_lock do
-          original_diskfile = self.diskfile
           existing_diskfile = existing.diskfile
           if File.readable?(original_diskfile) &&
             File.readable?(existing_diskfile) &&
@@ -456,7 +455,7 @@ class Attachment < ActiveRecord::Base
         end
       end
     end
-    if reused
+    if reused && Attachment.where(disk_filename: original_filename).none?
       File.delete(original_diskfile)
     end
   rescue ActiveRecord::StatementInvalid, ActiveRecord::RecordNotFound