Class: RailsAi::Security::SecureFileHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_ai/security/secure_file_handler.rb

Class Method Summary collapse

Class Method Details

.safe_file_exists?(path) ⇒ Boolean

Returns:



33
34
35
36
37
38
39
40
41
42
# File 'lib/rails_ai/security/secure_file_handler.rb', line 33

def self.safe_file_exists?(path)
  return false if path.nil?
  
  begin
    InputValidator.validate_file_path(path)
    true
  rescue ValidationError
    false
  end
end

.safe_file_read(path) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rails_ai/security/secure_file_handler.rb', line 8

def self.safe_file_read(path)
  validate_path = InputValidator.validate_file_path(path)
  
  File.open(validate_path, 'rb') do |file|
    content = file.read(InputValidator::MAX_FILE_SIZE)
    if file.eof?
      content
    else
      raise ValidationError, "File too large"
    end
  end
end

.safe_file_size(path) ⇒ Object



29
30
31
# File 'lib/rails_ai/security/secure_file_handler.rb', line 29

def self.safe_file_size(path)
  File.size(InputValidator.validate_file_path(path))
end

.safe_temp_file(content, extension = '.tmp') ⇒ Object



21
22
23
24
25
26
27
# File 'lib/rails_ai/security/secure_file_handler.rb', line 21

def self.safe_temp_file(content, extension = '.tmp')
  temp_file = Tempfile.new(['rails_ai', extension])
  temp_file.binmode
  temp_file.write(content)
  temp_file.rewind
  temp_file
end