Defect #44216 ยป fix_shorten_filename_with_long_extension.patch
| app/models/attachment.rb | ||
|---|---|---|
|
def create_diskfile(filename, directory=nil, &)
|
||
|
timestamp = DateTime.now.strftime("%y%m%d%H%M%S")
|
||
|
ascii = ''
|
||
|
if %r{^[a-zA-Z0-9_.-]*$}.match?(filename) && filename.length <= 50
|
||
|
max_filename_length = 50
|
||
|
if %r{^[a-zA-Z0-9_.-]*$}.match?(filename) && filename.length <= max_filename_length
|
||
|
ascii = filename
|
||
|
else
|
||
|
ascii = ActiveSupport::Digest.hexdigest(filename)
|
||
|
# keep the extension if any
|
||
|
ascii << $1 if filename =~ %r{(\.[a-zA-Z0-9]+)$}
|
||
|
if filename =~ %r{(\.[a-zA-Z0-9]+)$} && "#{ascii}#{$1}".length <= max_filename_length
|
||
|
ascii << $1
|
||
|
end
|
||
|
end
|
||
|
path = File.join storage_path, directory.to_s
|
||
| test/unit/attachment_test.rb | ||
|---|---|---|
|
assert_equal 255, a.filename.length
|
||
|
end
|
||
|
def test_shorted_filename_if_too_long_extension
|
||
|
file = mock_file_with_options(:original_filename => "file.#{'a'*250}")
|
||
|
a = Attachment.new(:container => Issue.find(1),
|
||
|
:file => file,
|
||
|
:author => User.find(1))
|
||
|
assert a.save
|
||
|
a.reload
|
||
|
# timestamp_hexdigest
|
||
|
assert_equal 12 + 1 + 32, a.disk_filename.length
|
||
|
assert_equal 255, a.filename.length
|
||
|
end
|
||
|
def test_copy_should_preserve_attributes
|
||
|
# prevent re-use of data from other attachments with equal contents
|
||
|
Attachment.where('id <> 1').destroy_all
|
||