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 | |
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
-rw-r--r-- | app/models/attachment.rb | 23 | ||||
-rw-r--r-- | lib/tasks/redmine.rake | 5 | ||||
-rw-r--r-- | test/unit/attachment_test.rb | 9 |
3 files changed, 36 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) diff --git a/lib/tasks/redmine.rake b/lib/tasks/redmine.rake index f1827bb65..734cad0a4 100644 --- a/lib/tasks/redmine.rake +++ b/lib/tasks/redmine.rake @@ -26,6 +26,11 @@ namespace :redmine do task :move_to_subdirectories => :environment do Attachment.move_from_root_to_target_directory end + + desc 'Updates attachment digests to SHA256' + task :update_digests => :environment do + Attachment.update_digests_to_sha256 + end end namespace :tokens do diff --git a/test/unit/attachment_test.rb b/test/unit/attachment_test.rb index a70009d23..16f02e51a 100644 --- a/test/unit/attachment_test.rb +++ b/test/unit/attachment_test.rb @@ -332,6 +332,14 @@ class AttachmentTest < ActiveSupport::TestCase assert_equal 'text/plain', attachment.content_type end + def test_update_digest_to_sha256_should_update_digest + set_fixtures_attachments_directory + attachment = Attachment.find 6 + assert attachment.readable? + attachment.update_digest_to_sha256! + assert_equal 'ac5c6e99a21ae74b2e3f5b8e5b568be1b9107cd7153d139e822b9fe5caf50938', attachment.digest + end + def test_update_attachments attachments = Attachment.where(:id => [2, 3]).to_a @@ -430,4 +438,5 @@ class AttachmentTest < ActiveSupport::TestCase else puts '(ImageMagick convert not available)' end + end |