2018-10-22 07:00:50 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-11-06 11:07:16 +00:00
|
|
|
module Gitlab
|
|
|
|
module Utils
|
|
|
|
extend self
|
|
|
|
|
2018-12-04 15:59:01 +00:00
|
|
|
# Ensure that the relative path will not traverse outside the base directory
|
|
|
|
def check_path_traversal!(path)
|
|
|
|
raise StandardError.new("Invalid path") if path.start_with?("..#{File::SEPARATOR}") ||
|
|
|
|
path.include?("#{File::SEPARATOR}..#{File::SEPARATOR}") ||
|
|
|
|
path.end_with?("#{File::SEPARATOR}..")
|
|
|
|
|
|
|
|
path
|
|
|
|
end
|
|
|
|
|
2014-11-06 11:07:16 +00:00
|
|
|
# Run system command without outputting to stdout.
|
|
|
|
#
|
|
|
|
# @param cmd [Array<String>]
|
2014-11-06 09:47:38 +00:00
|
|
|
# @return [Boolean]
|
2014-11-06 11:07:16 +00:00
|
|
|
def system_silent(cmd)
|
2016-08-15 13:32:36 +00:00
|
|
|
Popen.popen(cmd).last.zero?
|
2014-11-06 11:07:16 +00:00
|
|
|
end
|
2015-05-02 21:43:46 +00:00
|
|
|
|
|
|
|
def force_utf8(str)
|
|
|
|
str.force_encoding(Encoding::UTF_8)
|
|
|
|
end
|
2016-10-28 20:56:13 +00:00
|
|
|
|
2018-11-22 14:35:49 +00:00
|
|
|
def ensure_utf8_size(str, bytes:)
|
2018-11-27 13:34:05 +00:00
|
|
|
raise ArgumentError, 'Empty string provided!' if str.empty?
|
|
|
|
raise ArgumentError, 'Negative string size provided!' if bytes.negative?
|
2018-11-22 14:35:49 +00:00
|
|
|
|
2018-11-23 09:03:43 +00:00
|
|
|
truncated = str.each_char.each_with_object(+'') do |char, object|
|
2018-11-22 14:35:49 +00:00
|
|
|
if object.bytesize + char.bytesize > bytes
|
|
|
|
break object
|
|
|
|
else
|
|
|
|
object.concat(char)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-28 11:00:17 +00:00
|
|
|
truncated + ('0' * (bytes - truncated.bytesize))
|
2018-11-22 14:35:49 +00:00
|
|
|
end
|
|
|
|
|
2018-11-06 23:54:55 +00:00
|
|
|
# Append path to host, making sure there's one single / in between
|
|
|
|
def append_path(host, path)
|
|
|
|
"#{host.to_s.sub(%r{\/+$}, '')}/#{path.to_s.sub(%r{^\/+}, '')}"
|
|
|
|
end
|
|
|
|
|
2017-08-07 18:00:11 +00:00
|
|
|
# A slugified version of the string, suitable for inclusion in URLs and
|
|
|
|
# domain names. Rules:
|
|
|
|
#
|
|
|
|
# * Lowercased
|
|
|
|
# * Anything not matching [a-z0-9-] is replaced with a -
|
|
|
|
# * Maximum length is 63 bytes
|
|
|
|
# * First/Last Character is not a hyphen
|
|
|
|
def slugify(str)
|
|
|
|
return str.downcase
|
|
|
|
.gsub(/[^a-z0-9]/, '-')[0..62]
|
|
|
|
.gsub(/(\A-+|-+\z)/, '')
|
|
|
|
end
|
|
|
|
|
2018-04-04 17:11:55 +00:00
|
|
|
# Converts newlines into HTML line break elements
|
|
|
|
def nlbr(str)
|
2018-12-10 15:08:42 +00:00
|
|
|
ActionView::Base.full_sanitizer.sanitize(+str, tags: []).gsub(/\r?\n/, '<br>').html_safe
|
2018-04-04 17:11:55 +00:00
|
|
|
end
|
|
|
|
|
2018-01-05 21:36:18 +00:00
|
|
|
def remove_line_breaks(str)
|
|
|
|
str.gsub(/\r?\n/, '')
|
|
|
|
end
|
|
|
|
|
2016-10-28 20:56:13 +00:00
|
|
|
def to_boolean(value)
|
|
|
|
return value if [true, false].include?(value)
|
|
|
|
return true if value =~ /^(true|t|yes|y|1|on)$/i
|
|
|
|
return false if value =~ /^(false|f|no|n|0|off)$/i
|
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
2017-05-25 11:31:21 +00:00
|
|
|
|
|
|
|
def boolean_to_yes_no(bool)
|
|
|
|
if bool
|
|
|
|
'Yes'
|
|
|
|
else
|
|
|
|
'No'
|
|
|
|
end
|
|
|
|
end
|
2017-07-20 15:32:17 +00:00
|
|
|
|
|
|
|
def random_string
|
|
|
|
Random.rand(Float::MAX.to_i).to_s(36)
|
|
|
|
end
|
2017-11-08 20:53:10 +00:00
|
|
|
|
|
|
|
# See: http://stackoverflow.com/questions/2108727/which-in-ruby-checking-if-program-exists-in-path-from-ruby
|
|
|
|
# Cross-platform way of finding an executable in the $PATH.
|
|
|
|
#
|
|
|
|
# which('ruby') #=> /usr/bin/ruby
|
|
|
|
def which(cmd, env = ENV)
|
|
|
|
exts = env['PATHEXT'] ? env['PATHEXT'].split(';') : ['']
|
|
|
|
|
|
|
|
env['PATH'].split(File::PATH_SEPARATOR).each do |path|
|
|
|
|
exts.each do |ext|
|
|
|
|
exe = File.join(path, "#{cmd}#{ext}")
|
|
|
|
return exe if File.executable?(exe) && !File.directory?(exe)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
2018-03-06 21:16:55 +00:00
|
|
|
|
2018-04-12 03:05:07 +00:00
|
|
|
def bytes_to_megabytes(bytes)
|
|
|
|
bytes.to_f / Numeric::MEGABYTE
|
|
|
|
end
|
|
|
|
|
2018-03-06 21:16:55 +00:00
|
|
|
# Used in EE
|
|
|
|
# Accepts either an Array or a String and returns an array
|
|
|
|
def ensure_array_from_string(string_or_array)
|
|
|
|
return string_or_array if string_or_array.is_a?(Array)
|
|
|
|
|
|
|
|
string_or_array.split(',').map(&:strip)
|
|
|
|
end
|
2014-11-06 11:07:16 +00:00
|
|
|
end
|
|
|
|
end
|