debian-mirror-gitlab/lib/gitlab/utils.rb

135 lines
3.5 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2015-04-26 12:48:37 +05:30
module Gitlab
module Utils
extend self
2018-12-13 13:39:08 +05:30
# 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
2015-09-11 14:41:01 +05:30
def force_utf8(str)
2019-10-12 21:52:04 +05:30
str.dup.force_encoding(Encoding::UTF_8)
2015-09-11 14:41:01 +05:30
end
2016-11-24 13:41:30 +05:30
2019-02-15 15:39:39 +05:30
def ensure_utf8_size(str, bytes:)
raise ArgumentError, 'Empty string provided!' if str.empty?
raise ArgumentError, 'Negative string size provided!' if bytes.negative?
truncated = str.each_char.each_with_object(+'') do |char, object|
if object.bytesize + char.bytesize > bytes
break object
else
object.concat(char)
end
end
truncated + ('0' * (bytes - truncated.bytesize))
end
2018-12-13 13:39:08 +05:30
# 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
2018-03-17 18:26:18 +05:30
# 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-05-09 12:01:36 +05:30
# Converts newlines into HTML line break elements
def nlbr(str)
2019-02-15 15:39:39 +05:30
ActionView::Base.full_sanitizer.sanitize(+str, tags: []).gsub(/\r?\n/, '<br>').html_safe
2018-05-09 12:01:36 +05:30
end
2018-03-17 18:26:18 +05:30
def remove_line_breaks(str)
str.gsub(/\r?\n/, '')
end
2016-11-24 13:41:30 +05:30
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-09-10 17:25:29 +05:30
def boolean_to_yes_no(bool)
if bool
'Yes'
else
'No'
end
end
2018-03-17 18:26:18 +05:30
def random_string
Random.rand(Float::MAX.to_i).to_s(36)
end
# 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-27 19:54:05 +05:30
2019-07-07 11:18:12 +05:30
def try_megabytes_to_bytes(size)
Integer(size).megabytes
rescue ArgumentError
size
end
2018-10-15 14:42:47 +05:30
def bytes_to_megabytes(bytes)
bytes.to_f / Numeric::MEGABYTE
end
2018-03-27 19:54:05 +05:30
# 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
2019-02-15 15:39:39 +05:30
def deep_indifferent_access(data)
if data.is_a?(Array)
data.map(&method(:deep_indifferent_access))
elsif data.is_a?(Hash)
data.with_indifferent_access
else
data
end
end
2019-10-12 21:52:04 +05:30
def string_to_ip_object(str)
return unless str
IPAddr.new(str)
rescue IPAddr::InvalidAddressError
end
2015-04-26 12:48:37 +05:30
end
end