Module: RailsAiContext::SafeFile
- Defined in:
- lib/rails_ai_context/safe_file.rb
Overview
Safe file reading with size limits and error handling. Returns String on success, nil on any failure (missing, too large, unreadable). Designed as a drop-in replacement for unguarded File.read calls across introspectors and tools where nil is already handled.
Class Method Summary collapse
Class Method Details
.read(path, max_size: nil) ⇒ Object
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/rails_ai_context/safe_file.rb', line 9 def self.read(path, max_size: nil) return nil unless path && File.file?(path) limit = max_size || RailsAiContext.configuration.max_file_size return nil if File.size(path) > limit File.read(path, encoding: "UTF-8", invalid: :replace, undef: :replace) rescue Errno::ENOENT, Errno::EACCES, Errno::EISDIR, Errno::ENAMETOOLONG, SystemCallError nil end |