Module: Common::FileHelpers

Defined in:
lib/common/file_helpers.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.delete_file_if_exists(path) ⇒ Object



7
8
9
# File 'lib/common/file_helpers.rb', line 7

def delete_file_if_exists(path)
  File.delete(path) if path && File.exist?(path)
end

.generate_clamav_temp_file(file_body, file_name = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/common/file_helpers.rb', line 25

def generate_clamav_temp_file(file_body, file_name = nil)
  file_name = SecureRandom.hex if file_name.nil?
  clamav_directory = Rails.root.join('clamav_tmp')
  unless Dir.exist?(clamav_directory)
    # Create the directory if it doesn't exist
    Dir.mkdir(clamav_directory)
  end

  file_path = "clamav_tmp/#{file_name}"

  File.open(file_path, 'wb') do |file|
    file.write(file_body)
  end

  file_path
end

.generate_random_file(file_body) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/common/file_helpers.rb', line 15

def generate_random_file(file_body)
  file_path = random_file_path

  File.open(file_path, 'wb') do |file|
    file.write(file_body)
  end

  file_path
end

.random_file_pathObject



11
12
13
# File 'lib/common/file_helpers.rb', line 11

def random_file_path
  "tmp/#{SecureRandom.hex}"
end

Instance Method Details

#delete_file_if_exists(path) ⇒ Object (private)



7
8
9
# File 'lib/common/file_helpers.rb', line 7

def delete_file_if_exists(path)
  File.delete(path) if path && File.exist?(path)
end

#generate_clamav_temp_file(file_body, file_name = nil) ⇒ Object (private)



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/common/file_helpers.rb', line 25

def generate_clamav_temp_file(file_body, file_name = nil)
  file_name = SecureRandom.hex if file_name.nil?
  clamav_directory = Rails.root.join('clamav_tmp')
  unless Dir.exist?(clamav_directory)
    # Create the directory if it doesn't exist
    Dir.mkdir(clamav_directory)
  end

  file_path = "clamav_tmp/#{file_name}"

  File.open(file_path, 'wb') do |file|
    file.write(file_body)
  end

  file_path
end

#generate_random_file(file_body) ⇒ Object (private)



15
16
17
18
19
20
21
22
23
# File 'lib/common/file_helpers.rb', line 15

def generate_random_file(file_body)
  file_path = random_file_path

  File.open(file_path, 'wb') do |file|
    file.write(file_body)
  end

  file_path
end

#random_file_pathObject (private)



11
12
13
# File 'lib/common/file_helpers.rb', line 11

def random_file_path
  "tmp/#{SecureRandom.hex}"
end