# Returns true if the file is readable
def readable?
- File.readable?(diskfile)
+ disk_filename.present? && File.readable?(diskfile)
end
# Returns the attachment token
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)
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
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
else
puts '(ImageMagick convert not available)'
end
+
end