Class: GovFakeNotify::AttachmentStore

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/gov_fake_notify/attachment_store.rb

Overview

A central store for storing all state in the app - uses a basic PStore

Instance Method Summary collapse

Instance Method Details

#fetch(id) ⇒ Hash, Nil

Fetch a file from the store

Parameters:

  • id (String)

    The id of the file - which happens to be the filename

Returns:

  • (Hash, Nil)

    A hash containing 'file' (the file path) OR nil if not found



35
36
37
38
# File 'lib/gov_fake_notify/attachment_store.rb', line 35

def fetch(id)
  file_path = File.join(attachments_path, id.gsub(/[^a-zA-Z0-9\-]/, ''))
  File.exist?(file_path) ? { 'file' => file_path } : nil
end

#store(file_data) ⇒ Hash

Given a hash containing the data that comes from the client side 'prepare_upload' method, this method stores the data in a file and returns the same hash but with the base64 data replaced with a file path where the data is stored.

Parameters:

  • file_data (Hash)

    The data as prepared by 'prepare_upload'

Options Hash (file_data):

  • :file (String)

    The base64 encoded file

  • :is_csv (Boolean)

    Indicates if the file is csv or not

Returns:

  • (Hash)

    A copy of the file_data param but with the contents of file replaced with the path of where the file is stored.



22
23
24
25
26
27
28
# File 'lib/gov_fake_notify/attachment_store.rb', line 22

def store(file_data)
  file_path = File.join(attachments_path, SecureRandom.uuid)
  File.open(file_path, 'wb') do |file|
    file.write(Base64.decode64(file_data['file']))
  end
  file_data.merge('file' => file_path)
end