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. # See doc/COPYRIGHT.rdoc for more details.
#++ #++
require 'rubygems'
require 'UniversalDetector'
module AttachmentsHelper module AttachmentsHelper
# Displays view/delete links to the attachments of the given object # Displays view/delete links to the attachments of the given object
# Options: # Options:
@ -26,18 +29,16 @@ module AttachmentsHelper
end end
def to_utf8_for_attachments(str) def to_utf8_for_attachments(str)
if str.respond_to?(:force_encoding) return nil if str.nil?
str.force_encoding('UTF-8') iconv_str = str
return str if str.valid_encoding? detected = UniversalDetector::chardet(str)
else enc = detected['encoding']
return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii if !enc.nil?
end begin
iconv_str = Iconv.conv('UTF-8', enc, str)
begin rescue Iconv::Failure => err
Iconv.conv('UTF-8//IGNORE', 'UTF-8', str + ' ')[0..-3] end
rescue Iconv::InvalidEncoding end
# "UTF-8//IGNORE" is not supported on some OS iconv_str
str
end
end end
end end