attachment: use repositories setting to convert contents character encoding (#2371)

This commit results replacing invalid encoding instead to stripping.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@7866 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Toshi MARUYAMA 2011-11-20 11:46:52 +00:00
parent 85db02d486
commit 77710ece26
2 changed files with 32 additions and 40 deletions

View File

@ -29,19 +29,7 @@ module AttachmentsHelper
end end
def to_utf8(str) def to_utf8(str)
if str.respond_to?(:force_encoding) Redmine::CodesetUtil.to_utf8_by_setting(str)
str.force_encoding('UTF-8')
return str if str.valid_encoding?
else
return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
end
begin
Iconv.conv('UTF-8//IGNORE', 'UTF-8', str + ' ')[0..-3]
rescue Iconv::InvalidEncoding
# "UTF-8//IGNORE" is not supported on some OS
str
end
end end
def render_api_attachment(attachment, api) def render_api_attachment(attachment, api)

View File

@ -52,19 +52,21 @@ class AttachmentsControllerTest < ActionController::TestCase
end end
end end
def test_show_diff_should_strip_non_utf8_content def test_show_diff_replcace_cannot_convert_content
['inline', 'sbs'].each do |dt| with_settings :repositories_encodings => 'UTF-8' do
# 060719210727_changeset_iso8859-1.diff ['inline', 'sbs'].each do |dt|
get :show, :id => 5, :type => dt # 060719210727_changeset_iso8859-1.diff
assert_response :success get :show, :id => 5
assert_template 'diff' assert_response :success
assert_equal 'text/html', @response.content_type assert_template 'diff'
assert_tag 'th', assert_equal 'text/html', @response.content_type
:attributes => {:class => /filename/}, assert_tag 'th',
:content => /issues_controller.rb\t\(rvision 1484\)/ :attributes => {:class => "filename"},
assert_tag 'td', :content => /issues_controller.rb\t\(r\?vision 1484\)/
:attributes => {:class => /line-code/}, assert_tag 'td',
:content => /Demande cre avec succs/ :attributes => {:class => /line-code/},
:content => /Demande cr\?\?e avec succ\?s/
end
end end
end end
@ -96,22 +98,24 @@ class AttachmentsControllerTest < ActionController::TestCase
:sibling => { :tag => 'td', :content => /#{str_japanese}/ } :sibling => { :tag => 'td', :content => /#{str_japanese}/ }
end end
def test_show_text_file_should_strip_non_utf8_content def test_show_text_file_replcace_cannot_convert_content
set_tmp_attachments_directory set_tmp_attachments_directory
a = Attachment.new(:container => Issue.find(1), with_settings :repositories_encodings => 'UTF-8' do
:file => uploaded_test_file("iso8859-1.txt", "text/plain"), a = Attachment.new(:container => Issue.find(1),
:author => User.find(1)) :file => uploaded_test_file("iso8859-1.txt", "text/plain"),
assert a.save :author => User.find(1))
assert_equal 'iso8859-1.txt', a.filename assert a.save
assert_equal 'iso8859-1.txt', a.filename
get :show, :id => a.id get :show, :id => a.id
assert_response :success assert_response :success
assert_template 'file' assert_template 'file'
assert_equal 'text/html', @response.content_type assert_equal 'text/html', @response.content_type
assert_tag :tag => 'th', assert_tag :tag => 'th',
:content => '7', :content => '7',
:attributes => { :class => 'line-num' }, :attributes => { :class => 'line-num' },
:sibling => { :tag => 'td', :content => /Demande cre avec succs/ } :sibling => { :tag => 'td', :content => /Demande cr\?\?e avec succ\?s/ }
end
end end
def test_show_text_file_should_send_if_too_big def test_show_text_file_should_send_if_too_big