class RemoteFiles::FogStore

Constants

MULTIPART_MAX_PARTS
MULTIPART_MIN_SIZE

Public Instance Methods

connection() click to toggle source
# File lib/remote_files/fog_store.rb, line 67
def connection
  opts = options.dup
  opts.delete(:directory)
  opts.delete(:public)
  opts[:connection_options] ||= default_connection_options
  @connection ||= Fog::Storage.new(opts)
end
copy_to_store!(file, target_store) click to toggle source
# File lib/remote_files/fog_store.rb, line 19
def copy_to_store!(file, target_store)
  target_store.connection.copy_object(directory_name, file.identifier, target_store.directory_name, file.identifier)
end
delete!(identifier) click to toggle source
# File lib/remote_files/fog_store.rb, line 56
def delete!(identifier)
  if identifier.to_s.chomp.empty?
    message = "Empty identifier is not supported"
    raise RemoteFiles::Error, message
  end

  connection.delete_object(directory_name, identifier)
rescue Fog::Errors::NotFound, Excon::Errors::NotFound
  raise NotFoundError, $!.message, $!.backtrace
end
directory() click to toggle source
# File lib/remote_files/fog_store.rb, line 96
def directory
  @directory ||= lookup_directory || create_directory
end
directory_name() click to toggle source
# File lib/remote_files/fog_store.rb, line 75
def directory_name
  options[:directory]
end
files(prefix = '') click to toggle source

TODO: Add file bodies if we think it’s worth it

# File lib/remote_files/fog_store.rb, line 80
def files(prefix = '')
  full_list = []

  directory.files.all(:prefix => prefix).each do |file|
    full_list.push(
      File.new(file.identity,
             :content_type => file.content_type,
             :stored_in => [self],
             :last_update_ts => file.last_modified
      )
    )
  end

  full_list
end
retrieve!(identifier) click to toggle source
# File lib/remote_files/fog_store.rb, line 23
def retrieve!(identifier)
  fog_file = directory.files.get(identifier)

  raise NotFoundError, "#{identifier} not found in #{self.identifier} store" if fog_file.nil?

  File.new(identifier,
    :content      => fog_file.body,
    :content_type => fog_file.content_type,
    :stored_in    => [self],
    :last_update_ts => fog_file.last_modified
  )
rescue Fog::Errors::Error, Excon::Errors::Error
  raise RemoteFiles::Error, $!.message, $!.backtrace
end
store!(file) click to toggle source
# File lib/remote_files/fog_store.rb, line 8
def store!(file)
  success = directory.files.create(store_options(file))
  # TODO: Probably get the last modified time

  raise RemoteFiles::Error unless success

  true
rescue Fog::Errors::Error, Excon::Errors::Error
  raise RemoteFiles::Error, $!.message, $!.backtrace
end
url(identifier) click to toggle source
# File lib/remote_files/fog_store.rb, line 38
def url(identifier)
  case options[:provider]
  when 'AWS'
    "https://#{aws_host}/#{directory_name}/#{Fog::AWS.escape(identifier)}"
  else
    raise "#{self.class.name}#url was not implemented for the #{options[:provider]} provider"
  end
end
url_matcher() click to toggle source
# File lib/remote_files/fog_store.rb, line 47
def url_matcher
  @url_matcher ||= case options[:provider]
  when 'AWS'
    /https?:\/\/s3[^\.]*.amazonaws.com\/#{directory_name}\/(.*)/
  else
    raise "#{self.class.name}#url_matcher was not implemented for the #{options[:provider]} provider"
  end
end

Protected Instance Methods

aws_host() click to toggle source
# File lib/remote_files/fog_store.rb, line 109
def aws_host
  case options[:region]
  when nil, 'us-east-1'
    's3.amazonaws.com'
  else
    "s3-#{options[:region]}.amazonaws.com"
  end
end
create_directory() click to toggle source
# File lib/remote_files/fog_store.rb, line 122
def create_directory
  connection.directories.new(
    :key => directory_name,
    :public => options[:public]
  ).tap do |dir|
    dir.save
  end
end
default_connection_options() click to toggle source
# File lib/remote_files/fog_store.rb, line 102
def default_connection_options
  {
    retry_limit: 0,
    retry_interval: 0
  }
end
lookup_directory() click to toggle source
# File lib/remote_files/fog_store.rb, line 118
def lookup_directory
  connection.directories.get(directory_name)
end
store_options(file) click to toggle source
# File lib/remote_files/fog_store.rb, line 131
def store_options(file)
  store_options =
    {
      :body => file.content,
      :content_type => file.content_type,
      :key => file.identifier,
      :encryption => options[:encryption]
    }
  store_options[:public] = options[:public] if options.key?(:public)
  if file.options[:multipart_chunk_size]
    raise RemoteFiles::Error.new("Only S3 supports the multipart_chunk_size option") unless options[:provider] == 'AWS'
    chunk_size = file.options[:multipart_chunk_size]
    store_options[:multipart_chunk_size] = chunk_size
    raise RemoteFiles::Error.new("Minimum chunk size is #{MULTIPART_MIN_SIZE}") if chunk_size < MULTIPART_MIN_SIZE
    if !file.content.respond_to?(:read) || !file.content.respond_to?(:size)
      raise RemoteFiles::Error.new(':content must be a stream if chunking enabled')
    end
    if file.content.size / chunk_size > MULTIPART_MAX_PARTS
      raise RemoteFiles::Error.new("Increase chunk size so that there are less then #{10000}{MULTIPART_MAX_PARTS} parts")
    end
  end
  store_options
end