Character encoding detection in attachments is now automatic

This commit is contained in:
Kolan Sh 2012-03-24 23:39:46 +04:00
parent 786e92349b
commit 65b94d2014
1 changed files with 14 additions and 13 deletions

View File

@ -12,6 +12,9 @@
# See doc/COPYRIGHT.rdoc for more details.
#++
require 'rubygems'
require 'UniversalDetector'
module AttachmentsHelper
# Displays view/delete links to the attachments of the given object
# Options:
@ -26,18 +29,16 @@ module AttachmentsHelper
end
def to_utf8_for_attachments(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
return nil if str.nil?
iconv_str = str
detected = UniversalDetector::chardet(str)
enc = detected['encoding']
if !enc.nil?
begin
iconv_str = Iconv.conv('UTF-8', enc, str)
rescue Iconv::Failure => err
end
end
iconv_str
end
end