From 65b94d2014a4402d0feae01c4376ce99443949d3 Mon Sep 17 00:00:00 2001 From: Kolan Sh Date: Sat, 24 Mar 2012 23:39:46 +0400 Subject: [PATCH] Character encoding detection in attachments is now automatic --- app/helpers/attachments_helper.rb | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/app/helpers/attachments_helper.rb b/app/helpers/attachments_helper.rb index 666f3cc0..2b3e2b51 100644 --- a/app/helpers/attachments_helper.rb +++ b/app/helpers/attachments_helper.rb @@ -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