summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorToshi MARUYAMA <marutosijp2@yahoo.co.jp>2011-11-20 11:46:52 +0000
committerToshi MARUYAMA <marutosijp2@yahoo.co.jp>2011-11-20 11:46:52 +0000
commit77710ece26d8503e365a5211b010eb9be81c1c99 (patch)
tree2dee610d4510b49b48a7d865ed62a81461a4b1b9
parent85db02d486d658c1d0e83f915a39fac5a6ed6b27 (diff)
downloadredmine-77710ece26d8503e365a5211b010eb9be81c1c99.tar.gz
redmine-77710ece26d8503e365a5211b010eb9be81c1c99.zip
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
-rw-r--r--app/helpers/attachments_helper.rb14
-rw-r--r--test/functional/attachments_controller_test.rb60
2 files changed, 33 insertions, 41 deletions
diff --git a/app/helpers/attachments_helper.rb b/app/helpers/attachments_helper.rb
index 640a40c21..3d98b95d3 100644
--- a/app/helpers/attachments_helper.rb
+++ b/app/helpers/attachments_helper.rb
@@ -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)
diff --git a/test/functional/attachments_controller_test.rb b/test/functional/attachments_controller_test.rb
index 1fce6c0c9..879f6edca 100644
--- a/test/functional/attachments_controller_test.rb
+++ b/test/functional/attachments_controller_test.rb
@@ -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
-
- 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/ }
+ 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 cr\?\?e avec succ\?s/ }
+ end
end
def test_show_text_file_should_send_if_too_big