diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2017-04-03 11:38:06 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2017-04-03 11:38:06 +0000 |
commit | ee84b6b24ce29905f5e28613fd59e0cfd637dfcc (patch) | |
tree | 358204ab558e829df49756832686253b79f54424 /app/models/attachment.rb | |
parent | 30f7be9c19777d2b8ec88507a466bd35ffa523e3 (diff) | |
download | redmine-ee84b6b24ce29905f5e28613fd59e0cfd637dfcc.tar.gz redmine-ee84b6b24ce29905f5e28613fd59e0cfd637dfcc.zip |
Adds a rake task to update attachments digests to SHA256 (#25240).
Patch by Jens Krämer.
git-svn-id: http://svn.redmine.org/redmine/trunk@16455 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/models/attachment.rb')
-rw-r--r-- | app/models/attachment.rb | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/app/models/attachment.rb b/app/models/attachment.rb index 3bfecfc7b..d68a050c1 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -252,7 +252,7 @@ class Attachment < ActiveRecord::Base # Returns true if the file is readable def readable? - File.readable?(diskfile) + disk_filename.present? && File.readable?(diskfile) end # Returns the attachment token @@ -352,6 +352,27 @@ class Attachment < ActiveRecord::Base end end + # Updates digests to SHA256 for all attachments that have a MD5 digest + # (ie. created before Redmine 3.4) + def self.update_digests_to_sha256 + Attachment.where("length(digest) < 64").find_each do |attachment| + attachment.update_digest_to_sha256! + end + end + + # Updates attachment digest to SHA256 + def update_digest_to_sha256! + if readable? + sha = Digest::SHA256.new + File.open(diskfile, 'rb') do |f| + while buffer = f.read(8192) + sha.update(buffer) + end + end + update_column :digest, sha.hexdigest + end + end + # Returns true if the extension is allowed regarding allowed/denied # extensions defined in application settings, otherwise false def self.valid_extension?(extension) |