Sha256: faf1d817a206fbd3d332adbad875acdb6a87ab14f7a8d7b28c92f858e4feea93

Contents?: true

Size: 713 Bytes

Versions: 2

Compression:

Stored size: 713 Bytes

Contents

# frozen_string_literal: true

require 'digest'

module Mnogootex
  module Utils
    def self.short_md5(input)
      [Digest::MD5.digest(input)]. # get 16 bytes of MD5
        pack('m0'). # pack them into 22+2 base64 bytes (w/o trailing newline)
        tr('+/', '-_'). # make then url/path-safe
        chomp('==') # drop last 2 padding bytes
    end

    def self.humanize_bytes(size)
      %w[b Kb Mb Gb Tb Pb Eb Zb Yb].reduce(size) do |magnitude, unit|
        break "#{magnitude}#{unit}" if magnitude < 1024
        magnitude / 1024
      end
    end

    def self.dir_size(mask)
      Dir.glob(Pathname.new(mask).join('**', '*')).
        map! { |f| Pathname.new(f).size }.inject(:+) || 0
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mnogootex-1.0.1 lib/mnogootex/utils.rb
mnogootex-1.0.0 lib/mnogootex/utils.rb