class FileComposer::Stores::Local

File copier from local file system to the local file system but with a sharded path with YYYY/MM/DD using the passed in date. The date will default to the current date in UTC unless specified otherwise. The filename passed in will be used to determine the extension but the destination will create a new GUID to use as the filename. For example:

The reason it returns a completely random name is to ensure the file truly has a unique place to live, independent of what was passed in. You are free to change these assumptions by creating and using your own store if any of these implementation details do not fit your specific use case.

Attributes

date[R]
root[R]

Public Class Methods

new(date: Time.now.utc.to_date, root: '') click to toggle source
# File lib/file_composer/stores/local.rb, line 26
def initialize(date: Time.now.utc.to_date, root: '')
  @date = date
  @root = root.to_s

  freeze
end

Public Instance Methods

move!(filename) click to toggle source
# File lib/file_composer/stores/local.rb, line 33
def move!(filename)
  make_final_filename(filename).tap do |final_filename|
    ensure_directory_exists(final_filename)
    FileUtils.mv(filename, final_filename)
  end
end

Private Instance Methods

ensure_directory_exists(filename) click to toggle source
# File lib/file_composer/stores/local.rb, line 58
def ensure_directory_exists(filename)
  FileUtils.mkdir_p(File.dirname(filename))
end
make_final_filename(filename) click to toggle source
# File lib/file_composer/stores/local.rb, line 42
def make_final_filename(filename)
  parts = random_filename_parts(File.extname(filename))

  File.join(*parts)
end
random_filename_parts(extension) click to toggle source
# File lib/file_composer/stores/local.rb, line 48
def random_filename_parts(extension)
  [
    root,
    date.year.to_s,
    date.month.to_s,
    date.day.to_s,
    "#{SecureRandom.uuid}#{extension}"
  ].compact
end