Project

General

Profile

Patch #25240 » 0002-adds-a-rake-task-to-convert-the-digests-of-existing-.patch

Jens Krämer, 2017-03-02 04:27

View differences:

app/models/attachment.rb
352 352
    end
353 353
  end
354 354

  
355
  # Updates attachments' digests to SHA256
356
  def self.update_digest_to_sha256
357
    Attachment.where("length(digest) < 64").find_in_batches do |group|
358
      group.each do |attachment|
359
        attachment.update_digest_to_sha256!
360
      end
361
    end
362
  end
363

  
364
  def update_digest_to_sha256!
365
    sha = Digest::SHA256.new
366
    File.open(diskfile, 'rb') do |f|
367
      while buffer = f.read(8192)
368
        sha.update(buffer)
369
      end
370
    end
371
    update_column :digest, sha.hexdigest
372
  end
373

  
355 374
  # Returns true if the extension is allowed regarding allowed/denied
356 375
  # extensions defined in application settings, otherwise false
357 376
  def self.valid_extension?(extension)
lib/tasks/redmine.rake
26 26
    task :move_to_subdirectories => :environment do
27 27
      Attachment.move_from_root_to_target_directory
28 28
    end
29

  
30
    desc 'Updates attachment digests to SHA256'
31
    task :update_digest_to_sha256 => :environment do
32
      Attachment.update_digest_to_sha256
33
    end
29 34
  end
30 35

  
31 36
  namespace :tokens do
test/unit/attachment_test.rb
332 332
    assert_equal 'text/plain', attachment.content_type
333 333
  end
334 334

  
335
  test 'should update digest to sha256' do
336
    set_fixtures_attachments_directory
337
    attachment = Attachment.find 4
338
    assert File.readable? attachment.diskfile
339
    attachment.update_digest_to_sha256!
340
    assert_equal 'c2db49ac68eaebb1703db09411dcd8186b395f4ae8482e449a5d5abff96c7fb2', attachment.digest
341
  end
342

  
335 343
  def test_update_attachments
336 344
    attachments = Attachment.where(:id => [2, 3]).to_a
337 345

  
......
430 438
  else
431 439
    puts '(ImageMagick convert not available)'
432 440
  end
441

  
433 442
end
(1-1/6)