summaryrefslogtreecommitdiffstats
path: root/app/models/attachment.rb
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2017-04-03 11:54:29 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2017-04-03 11:54:29 +0000
commitf0e5437d27f008379b8523ce2b54d697e5e9be94 (patch)
tree94dcd61f23892e2118af3b75aace179c0d540265 /app/models/attachment.rb
parentd65e920307c89e9ad7a1e7f85865ab7198f89cd7 (diff)
downloadredmine-f0e5437d27f008379b8523ce2b54d697e5e9be94.tar.gz
redmine-f0e5437d27f008379b8523ce2b54d697e5e9be94.zip
Reuse existing identical disk files for new attachments (#25215).
Patch by Jens Kraemer. git-svn-id: http://svn.redmine.org/redmine/trunk@16458 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/models/attachment.rb')
-rw-r--r--app/models/attachment.rb19
1 files changed, 19 insertions, 0 deletions
diff --git a/app/models/attachment.rb b/app/models/attachment.rb
index 1e4f731e7..8e8cc0ac1 100644
--- a/app/models/attachment.rb
+++ b/app/models/attachment.rb
@@ -56,6 +56,7 @@ class Attachment < ActiveRecord::Base
before_create :files_to_final_location
after_rollback :delete_from_disk, :on => :create
after_commit :delete_from_disk, :on => :destroy
+ after_commit :reuse_existing_file_if_possible, :on => :create
safe_attributes 'filename', 'content_type', 'description'
@@ -411,6 +412,24 @@ class Attachment < ActiveRecord::Base
private
+ def reuse_existing_file_if_possible
+ with_lock do
+ if existing = Attachment
+ .lock
+ .where(digest: self.digest, filesize: self.filesize)
+ .where('id <> ? and disk_filename <> ?',
+ self.id, self.disk_filename)
+ .first
+
+ original_diskfile = self.diskfile
+ self.update_columns disk_directory: existing.disk_directory,
+ disk_filename: existing.disk_filename
+ File.delete(original_diskfile) if File.exist?(original_diskfile)
+ end
+ end
+ end
+
+
# Physically deletes the file from the file system
def delete_from_disk!
if disk_filename.present? && File.exist?(diskfile)