Module: Artifactory::Cleaner::Util

Defined in:
lib/artifactory/cleaner/util.rb

Class Method Summary collapse

Class Method Details

.filesize(size) ⇒ Object

Given a size in bytes, return a “human readable” size with a unit suffix (MiB, GiB)

Taken from stackoverflow.com/a/47486815/75801 Used instead of a gem due to simplicity and speed



9
10
11
12
13
14
15
16
17
18
# File 'lib/artifactory/cleaner/util.rb', line 9

def self.filesize(size)
  units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'Pib', 'EiB']

  return '0.0 B' if size == 0
  exp = (Math.log(size) / Math.log(1024)).to_i
  exp += 1 if (size.to_f / 1024 ** exp >= 1024 - 0.05)
  exp = 6 if exp > 6

  '%.1f %s' % [size.to_f / 1024 ** exp, units[exp]]
end