diff options
author | Saito <saitowu@gmail.com> | 2012-05-30 11:44:36 +0800 |
---|---|---|
committer | Saito <saitowu@gmail.com> | 2012-05-30 11:44:36 +0800 |
commit | efd9a717c10f1cafd05fb20729eafb61226d9c1d (patch) | |
tree | c6d3035f8b983f9e840b0d92a8e15fc96296fa60 | |
parent | e1d1673e74c35fc2d64e71320668d981d634b02a (diff) | |
download | gitlab-ce-efd9a717c10f1cafd05fb20729eafb61226d9c1d.tar.gz |
solve the binary problem
-rw-r--r-- | lib/gitlab/encode.rb | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/lib/gitlab/encode.rb b/lib/gitlab/encode.rb index cee3ace202a..ba7fe27cb62 100644 --- a/lib/gitlab/encode.rb +++ b/lib/gitlab/encode.rb @@ -8,17 +8,21 @@ module Gitlab # return nil if message is nil return nil unless message + # return message if message type is binary + detect = CharlockHolmes::EncodingDetector.detect(message) + return message if detect[:type] == :binary + # if message is utf-8 encoding, just return it message.force_encoding("utf-8") return message if message.valid_encoding? - # if message is not utf-8 encoding, detect and convert it - detect = CharlockHolmes::EncodingDetector.detect(message) - if detect[:encoding] && detect[:confidence] > 60 + # if message is not utf-8 encoding, convert it + if detect[:encoding] message.force_encoding(detect[:encoding]) message.encode!("utf-8", detect[:encoding], :undef => :replace, :replace => "", :invalid => :replace) end + # ensure message encoding is utf8 message.valid_encoding? ? message : raise # Prevent app from crash cause of encoding errors |