Module: Cifrado::Utils

Included in:
CLI, FileSplitter, Plugins::Saio, Progressbar, SwiftClient
Defined in:
lib/cifrado/utils.rb

Instance Method Summary collapse

Instance Method Details

#calculate_chunks(file) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cifrado/utils.rb', line 44

def calculate_chunks(file)
  # File size in bytes
  size = File.size(file)
  if size >= 1 and size <= 10485760
    1
  # if file >= 20MB and <= 50MB, split in chunks of 10MB
  elsif size > 10485760 and size <= 104857600
    size/10485760
  # split in chunk of 100 MB
  else
    size/104857600
  end
end

#clean_object_name(obj) ⇒ Object



16
17
18
# File 'lib/cifrado/utils.rb', line 16

def clean_object_name(obj)
  Pathname.new(obj.gsub(/^(\/+|.\/)/,'')).cleanpath.to_s
end

#decrypt_filename(name, password, options = {}) ⇒ Object



24
25
26
27
# File 'lib/cifrado/utils.rb', line 24

def decrypt_filename(name, password, options = {})
  cipher = CryptoEngineAES.new password
  cipher.decrypt name
end

#encrypt_filename(name, password, options = {}) ⇒ Object



29
30
31
32
# File 'lib/cifrado/utils.rb', line 29

def encrypt_filename(name, password, options = {})
  cipher = CryptoEngineAES.new password
  cipher.encrypt name
end

#humanize_bytes(bytes) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/cifrado/utils.rb', line 34

def humanize_bytes(bytes)
  m = bytes.to_i
  units = %w[Bytes KB MB GB TB PB]
  while (m/1024.0) >= 1
    m = m/1024.0
    units.shift
  end
  return "%.2f #{units[0]}" % m
end

#mime_type(file) ⇒ Object



11
12
13
14
# File 'lib/cifrado/utils.rb', line 11

def mime_type(file)
  mime = `/usr/bin/file -b --mime-type #{Shellwords.escape(file)} 2>/dev/null`.strip.chomp
  mime.empty? ? nil : mime
end

#prettify_backtrace(e) ⇒ Object



6
7
8
9
# File 'lib/cifrado/utils.rb', line 6

def prettify_backtrace(e)
  Log.debug e.class
  Log.debug e.backtrace.join("\nDEBUG: ")
end

#unix_time(secs) ⇒ Object



20
21
22
# File 'lib/cifrado/utils.rb', line 20

def unix_time(secs)
  (Time.at secs.to_i).to_s
end