diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2013-01-31 16:10:02 +0200 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2013-01-31 16:10:02 +0200 |
commit | 16326c4170b82787f66cc146fa7a71439068588c (patch) | |
tree | f75c157407085261fcb7ccd12c2ebb86de229203 | |
parent | 01088eb84cfad17505d8431da582acdb55b885df (diff) | |
download | gitlab-ci-16326c4170b82787f66cc146fa7a71439068588c.tar.gz |
GitlabCi::Encode lib
-rw-r--r-- | app/models/build.rb | 14 | ||||
-rw-r--r-- | lib/gitlab_ci/encode.rb | 31 |
2 files changed, 38 insertions, 7 deletions
diff --git a/app/models/build.rb b/app/models/build.rb index e3ba323..813d585 100644 --- a/app/models/build.rb +++ b/app/models/build.rb @@ -94,15 +94,15 @@ class Build < ActiveRecord::Base end def sanitize_build_output(output) - output.detect_encoding!.encode!('utf-8', invalid: :replace) + GitlabCi::Encode.encode!(output) end def read_tmp_file - if tmp_file && File.readable?(tmp_file) - File.read(tmp_file) - else - '' - end + content = if tmp_file && File.readable?(tmp_file) + File.read(tmp_file) + end + + content ||= '' end def compose_output @@ -110,7 +110,7 @@ class Build < ActiveRecord::Base if running? sanitized_output = sanitize_build_output(read_tmp_file) - output << sanitized_output + output << sanitized_output if sanitized_output.present? end output diff --git a/lib/gitlab_ci/encode.rb b/lib/gitlab_ci/encode.rb new file mode 100644 index 0000000..6cf84fd --- /dev/null +++ b/lib/gitlab_ci/encode.rb @@ -0,0 +1,31 @@ +module GitlabCi + module Encode + extend self + + def encode!(message) + return nil unless message.respond_to? :force_encoding + + # if message is utf-8 encoding, just return it + message.force_encoding("UTF-8") + return message if message.valid_encoding? + + # return message if message type is binary + detect = CharlockHolmes::EncodingDetector.detect(message) + return message if detect[:type] == :binary + + # 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 + rescue + encoding = detect ? detect[:encoding] : "unknown" + "--broken encoding: #{encoding}" + end + end +end |