module Artifactory::Cleaner::Util

Public Class Methods

filesize(size) click to toggle source

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

# 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