1
0
mirror of https://github.com/meineerde/redmine.git synced 2025-12-22 16:31:12 +00:00

Introduces an additional check before removing a deduplicated file (#34479).

Patch by Jens Krämer.


git-svn-id: http://svn.redmine.org/redmine/trunk@20812 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Go MAEDA 2021-03-17 13:46:18 +00:00
parent 7c824702c2
commit a8170e6a14

View File

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