0002-adds-a-rake-task-to-convert-the-digests-of-existing-.patch

updated version of patch 2 that doesn't error on missing files - Jens Krämer, 2017-03-02 07:16

Download (2.66 KB)

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
    if readable?
366
      sha = Digest::SHA256.new
367
      File.open(diskfile, 'rb') do |f|
368
        while buffer = f.read(8192)
369
          sha.update(buffer)
370
        end
371
      end
372
      update_column :digest, sha.hexdigest
373
    end
374
  end
375

  
355 376
  # Returns true if the extension is allowed regarding allowed/denied
356 377
  # extensions defined in application settings, otherwise false
357 378
  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
434
-