class Dragonfly::MantaDataStore

Constants

REGIONS
VERSION

Attributes

directory[RW]
durability_level[RW]
key[RW]
region[RW]
root_path[RW]
storage_headers[RW]
url[RW]
url_host[RW]
url_scheme[RW]
user[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/dragonfly/manta_data_store.rb, line 17
def initialize(options = {})
  @directory        = options[:directory]
  @url              = options[:url]
  @user             = options[:user]
  @key              = options[:key]
  @durability_level = options[:durability_level] || 2
  @region           = options[:region] || REGIONS.keys.first
  @url_scheme       = options[:url_scheme] || 'http'
  @url_host         = options[:url_host]
  @root_path        = options[:root_path]
  @storage_headers  = options[:storage_headers] || {}
end

Public Instance Methods

destroy(uid) click to toggle source
# File lib/dragonfly/manta_data_store.rb, line 47
def destroy(uid)
  storage.delete_object(full_path(uid))
rescue => e
  Dragonfly.warn("#{self.class.name} destroy error: #{e}")
end
directory_exists?() click to toggle source
# File lib/dragonfly/manta_data_store.rb, line 73
def directory_exists?
  storage.list_directory(public_directory, :head => true)
  true
rescue RubyManta::MantaClient::UnknownError => e
  false
end
domain() click to toggle source
# File lib/dragonfly/manta_data_store.rb, line 80
def domain
  REGIONS[get_region]
end
read(uid) click to toggle source
# File lib/dragonfly/manta_data_store.rb, line 38
def read(uid)
  ensure_configured
  response, headers = storage.get_object(full_path(uid))

  [ response, headers_to_meta(headers) ]
rescue RubyManta::MantaClient::ResourceNotFound => e
  nil
end
storage() click to toggle source
# File lib/dragonfly/manta_data_store.rb, line 67
def storage
  @storage ||= begin
    RubyManta::MantaClient.new(url, user, key)
  end
end
url_for(uid, options = {}) click to toggle source
# File lib/dragonfly/manta_data_store.rb, line 53
def url_for(uid, options = {})
  scheme = options[:scheme] || url_scheme

  if options[:expires]
    url_without_scheme = storage.gen_signed_url(options[:expires], :get, full_path(uid))
  else
    host = options[:host] || url_host || region_host

    url_without_scheme = "#{host}#{full_path(uid)}"
  end

  "#{scheme}://#{url_without_scheme}"
end
write(content, options = {}) click to toggle source
# File lib/dragonfly/manta_data_store.rb, line 32
def write(content, options = {})
  ensure_configured
  ensure_directory
  store_content(content, options)
end

Private Instance Methods

ensure_configured() click to toggle source
# File lib/dragonfly/manta_data_store.rb, line 122
def ensure_configured
  unless @configured
    [:directory, :url, :user, :key].each do |attr|
      raise NotConfigured, "You need to configure #{self.class.name} with #{attr}" if send(attr).nil?
    end

    @configured = true
  end
end
ensure_directory() click to toggle source
# File lib/dragonfly/manta_data_store.rb, line 132
def ensure_directory
  unless @directory_created
    mkdir(public_directory) unless directory_exists?

    @directory_created = true
  end
end
full_path(uid) click to toggle source
# File lib/dragonfly/manta_data_store.rb, line 165
def full_path(uid)
  File.join *[public_directory, root_path, uid].compact
end
full_storage_headers(options = {}) click to toggle source
# File lib/dragonfly/manta_data_store.rb, line 159
def full_storage_headers(options = {})
  {
    "durability_level" => durability_level
  }.merge(storage_headers).merge(options)
end
generate_uid(name) click to toggle source
# File lib/dragonfly/manta_data_store.rb, line 153
def generate_uid(name)
  # S3 was using subdirectories but Manta is ZFS and it can handle up to
  # 281,474,976,710,656 files in a directory
  "#{Time.now.strftime '%Y_%m_%d_%H_%M_%S'}_#{rand(1000)}_#{name.gsub(/[^\w.]+/, '_')}"
end
get_region() click to toggle source
# File lib/dragonfly/manta_data_store.rb, line 148
def get_region
  raise "Invalid region #{region} - should be one of #{valid_regions.join(', ')}" unless valid_regions.include?(region)
  region
end
headers_to_meta(headers) click to toggle source
# File lib/dragonfly/manta_data_store.rb, line 86
def headers_to_meta(headers)
  begin
    Serializer.json_decode(headers["m-dragonfly"])
  rescue => e
    nil
  end
end
meta_to_header(meta = {}) click to toggle source
# File lib/dragonfly/manta_data_store.rb, line 94
def meta_to_header(meta = {})
  { :m_dragonfly => Serializer.json_encode(meta) }
end
mkdir(path) click to toggle source
# File lib/dragonfly/manta_data_store.rb, line 189
def mkdir(path)
  storage.put_directory(path)
end
mkdir_for_file_path(file_with_path) click to toggle source
# File lib/dragonfly/manta_data_store.rb, line 173
def mkdir_for_file_path(file_with_path)
  mkdir_with_intermediates file_with_path.split("/")[0..-2].join("/")
end
mkdir_with_intermediates(path) click to toggle source
# File lib/dragonfly/manta_data_store.rb, line 177
def mkdir_with_intermediates(path)
  path_components = path.split("/")

  path_components.length.times do |index|
    path_to_make = path_components[0..index].join("/")

    if path_to_make.start_with?(public_directory) and not path_to_make.empty?
      mkdir path_to_make
    end
  end
end
public_directory() click to toggle source
# File lib/dragonfly/manta_data_store.rb, line 140
def public_directory
  "/#{@user}/public/#{@directory}"
end
region_host() click to toggle source
# File lib/dragonfly/manta_data_store.rb, line 144
def region_host
  REGIONS[get_region]
end
store_content(content, options = {}) click to toggle source
# File lib/dragonfly/manta_data_store.rb, line 98
def store_content(content, options = {})
  uid = options[:path] || generate_uid(content.name || 'file')

  headers = {
    :content_type => content.mime_type
  }

  headers.merge!(meta_to_header(content.meta))
  headers.merge!(options[:headers]) if options[:headers]

  path = full_path(uid)
  mkdir_for_file_path(path)

  content.file do |file|
    storage.put_object(
      path,
      file.read,
      full_storage_headers(headers)
    )
  end

  uid
end
valid_regions() click to toggle source
# File lib/dragonfly/manta_data_store.rb, line 169
def valid_regions
  REGIONS.keys
end