Module: Appydave::Tools::Dam::FileHelper
- Defined in:
- lib/appydave/tools/dam/file_helper.rb
Overview
File utility methods for DAM operations Provides reusable file and directory helpers
Class Method Summary collapse
-
.calculate_directory_size(path) ⇒ Integer
Calculate total size of directory in bytes.
-
.format_size(bytes) ⇒ String
Format bytes into human-readable size.
Class Method Details
.calculate_directory_size(path) ⇒ Integer
Calculate total size of directory in bytes
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/appydave/tools/dam/file_helper.rb', line 16 def calculate_directory_size(path) return 0 unless Dir.exist?(path) total = 0 Find.find(path) do |file_path| total += File.size(file_path) if File.file?(file_path) rescue StandardError # Skip files we can't read end total end |
.format_size(bytes) ⇒ String
Format bytes into human-readable size
31 32 33 34 35 36 37 38 39 |
# File 'lib/appydave/tools/dam/file_helper.rb', line 31 def format_size(bytes) return '0 B' if bytes.zero? units = %w[B KB MB GB TB] exp = (Math.log(bytes) / Math.log(1024)).to_i exp = [exp, units.length - 1].min format('%<size>.1f %<unit>s', size: bytes.to_f / (1024**exp), unit: units[exp]) end |