Module: SmartCsvImport::FileStorage

Defined in:
lib/smart_csv_import/file_storage.rb

Class Method Summary collapse

Class Method Details

.compute_hash(file_path:) ⇒ Object



28
29
30
31
32
# File 'lib/smart_csv_import/file_storage.rb', line 28

def compute_hash(file_path:)
  raise SmartCsvImport::Error, "File not found: #{file_path}" unless File.exist?(file_path)

  Digest::SHA256.file(file_path).hexdigest
end

.store(source_path:, import_type:) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/smart_csv_import/file_storage.rb', line 10

def store(source_path:, import_type:)
  raise SmartCsvImport::Error, "Source file not found: #{source_path}" unless File.exist?(source_path)

  destination_dir = File.join(SmartCsvImport.configuration.storage_path, import_type)
  FileUtils.mkdir_p(destination_dir)

  extension = File.extname(source_path)
  basename = File.basename(source_path, extension)
  timestamp = Time.current.strftime("%Y%m%d%H%M%S")
  random_suffix = SecureRandom.hex(4)
  destination_filename = "#{timestamp}_#{random_suffix}_#{basename}#{extension}"
  destination_path = File.join(destination_dir, destination_filename)

  FileUtils.cp(source_path, destination_path)

  destination_path
end