Class: SnapImage::Local::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/snapimage/adapters/local/storage.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Storage

Returns a new instance of Storage.



4
5
6
7
8
# File 'lib/snapimage/adapters/local/storage.rb', line 4

def initialize(config)
  @config = config
  @directory = config["directory"]
  @public_url = config["public_url"]
end

Instance Method Details

#store(filename, file, options = {}) ⇒ Object

Stores the file in the given directory and returns the publicly accessible URL. Options:

  • directory - directory to store the file in



14
15
16
17
18
19
20
21
22
23
# File 'lib/snapimage/adapters/local/storage.rb', line 14

def store(filename, file, options = {})
  file_directory = File.join(@directory, options[:directory])
  file_path = get_file_path(file_directory, filename)
  # Make sure the directory exists.
  FileUtils.mkdir_p(file_directory)
  # Save the file.
  File.open(file_path, "wb") { |f| f.write(file.read) } unless File.exists?(file_path)
  # Return the public URL.
  File.join(@public_url, options[:directory], File.basename(file_path))
end