Module: BulkCsvParser::Storage::ActiveStorageAdapter

Defined in:
lib/bulk_csv_parser/storage.rb

Overview

Uploads to whatever service config/storage.yml points at (S3, GCS, Azure, ...). The reference is an ActiveStorage signed_id: safe to hand to a job that runs on any machine with the same storage credentials.

Class Method Summary collapse

Class Method Details

.cleanup(reference) ⇒ Object



98
99
100
# File 'lib/bulk_csv_parser/storage.rb', line 98

def cleanup(reference)
  ::ActiveStorage::Blob.find_signed(reference)&.purge
end

.fetch(reference) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/bulk_csv_parser/storage.rb', line 88

def fetch(reference)
  blob = ::ActiveStorage::Blob.find_signed!(reference)

  tempfile = Tempfile.new(["bulk_csv_parser", ".csv"])
  tempfile.binmode
  blob.download { |chunk| tempfile.write(chunk) }
  tempfile.rewind
  tempfile
end

.store(file) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/bulk_csv_parser/storage.rb', line 77

def store(file)
  filename = file.respond_to?(:original_filename) ? file.original_filename : File.basename(file.path)

  blob = ::ActiveStorage::Blob.create_and_upload!(
    io: File.open(file.path),
    filename: filename,
    content_type: "text/csv"
  )
  blob.signed_id
end