Patch by Jens Krämer. git-svn-id: http://svn.redmine.org/redmine/trunk@16455 e93f8b46-1217-0410-a6f0-8f06a7374b81tags/3.4.0
@@ -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) |
@@ -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 |
@@ -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 |