Commit efd9a717c10f1cafd05fb20729eafb61226d9c1d

Authored by Saito
1 parent e1d1673e

solve the binary problem

Showing 1 changed file with 7 additions and 3 deletions   Show diff stats
lib/gitlab/encode.rb
... ... @@ -8,17 +8,21 @@ module Gitlab
8 8 # return nil if message is nil
9 9 return nil unless message
10 10  
  11 + # return message if message type is binary
  12 + detect = CharlockHolmes::EncodingDetector.detect(message)
  13 + return message if detect[:type] == :binary
  14 +
11 15 # if message is utf-8 encoding, just return it
12 16 message.force_encoding("utf-8")
13 17 return message if message.valid_encoding?
14 18  
15   - # if message is not utf-8 encoding, detect and convert it
16   - detect = CharlockHolmes::EncodingDetector.detect(message)
17   - if detect[:encoding] && detect[:confidence] > 60
  19 + # if message is not utf-8 encoding, convert it
  20 + if detect[:encoding]
18 21 message.force_encoding(detect[:encoding])
19 22 message.encode!("utf-8", detect[:encoding], :undef => :replace, :replace => "", :invalid => :replace)
20 23 end
21 24  
  25 + # ensure message encoding is utf8
22 26 message.valid_encoding? ? message : raise
23 27  
24 28 # Prevent app from crash cause of encoding errors
... ...