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
def to_utf8(str)
if str.respond_to?(:force_encoding)
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
Redmine::CodesetUtil.to_utf8_by_setting(str)
end
def render_api_attachment(attachment, api)

View File

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