Skip to content
Snippets Groups Projects
Commit 16326c41 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets
Browse files

GitlabCi::Encode lib

parent 01088eb8
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -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
Loading
Loading
@@ -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
Loading
Loading
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment