Project

General

Profile

Patch #44186 ยป fix_shorten_filename_with_long_extension.patch

Kenta Kumojima, 2026-06-16 15:34

View differences:

app/models/attachment.rb
596 596
    def create_diskfile(filename, directory=nil, &)
597 597
      timestamp = DateTime.now.strftime("%y%m%d%H%M%S")
598 598
      ascii = ''
599
      if %r{^[a-zA-Z0-9_.-]*$}.match?(filename) && filename.length <= 50
599
      max_filename_length = 50
600
      if %r{^[a-zA-Z0-9_.-]*$}.match?(filename) && filename.length <= max_filename_length
600 601
        ascii = filename
601 602
      else
602 603
        ascii = ActiveSupport::Digest.hexdigest(filename)
603 604
        # keep the extension if any
604
        ascii << $1 if filename =~ %r{(\.[a-zA-Z0-9]+)$}
605
        if filename =~ %r{(\.[a-zA-Z0-9]+)$} && "#{ascii}#{$1}".length <= max_filename_length
606
          ascii << $1
607
        end
605 608
      end
606 609

  
607 610
      path = File.join storage_path, directory.to_s
test/unit/attachment_test.rb
81 81
    assert_equal 255, a.filename.length
82 82
  end
83 83

  
84
  def test_shorted_filename_if_too_long_extension
85
    file = mock_file_with_options(:original_filename => "file.#{'a'*250}")
86

  
87
    a = Attachment.new(:container => Issue.find(1),
88
                       :file => file,
89
                       :author => User.find(1))
90
    assert a.save
91
    a.reload
92
    # timestamp_hexdigest
93
    assert_equal 12 + 1 + 32, a.disk_filename.length
94
    assert_equal 255, a.filename.length
95
  end
96

  
84 97
  def test_copy_should_preserve_attributes
85 98
    # prevent re-use of data from other attachments with equal contents
86 99
    Attachment.where('id <> 1').destroy_all
    (1-1/1)