Class: RemoteFiles::FogStore

Inherits:
AbstractStore show all
Defined in:
lib/remote_files/fog_store.rb

Constant Summary collapse

MULTIPART_MAX_PARTS =
10000
MULTIPART_MIN_SIZE =
5 * 1024 * 1024

Instance Attribute Summary

Attributes inherited from AbstractStore

#identifier

Instance Method Summary collapse

Methods inherited from AbstractStore

#[]=, #file_from_url, #initialize, #options, #read_only?, #to_sym

Constructor Details

This class inherits a constructor from RemoteFiles::AbstractStore

Instance Method Details

#connectionObject



56
57
58
59
60
61
# File 'lib/remote_files/fog_store.rb', line 56

def connection
  connection_options = options.dup
  connection_options.delete(:directory)
  connection_options.delete(:public)
  @connection ||= Fog::Storage.new(connection_options)
end

#delete!(identifier) ⇒ Object



50
51
52
53
54
# File 'lib/remote_files/fog_store.rb', line 50

def delete!(identifier)
  connection.delete_object(directory.key, identifier)
rescue Fog::Errors::NotFound, Excon::Errors::NotFound
  raise NotFoundError, $!.message, $!.backtrace
end

#directoryObject



67
68
69
# File 'lib/remote_files/fog_store.rb', line 67

def directory
  @directory ||= lookup_directory || create_directory
end

#directory_nameObject



63
64
65
# File 'lib/remote_files/fog_store.rb', line 63

def directory_name
  options[:directory]
end

#retrieve!(identifier) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/remote_files/fog_store.rb', line 18

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]
  )
rescue Fog::Errors::Error, Excon::Errors::Error
  raise RemoteFiles::Error, $!.message, $!.backtrace
end

#store!(file) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/remote_files/fog_store.rb', line 8

def store!(file)
  success = directory.files.create(store_options(file))

  raise RemoteFiles::Error unless success

  true
rescue Fog::Errors::Error, Excon::Errors::Error
  raise RemoteFiles::Error, $!.message, $!.backtrace
end

#url(identifier) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/remote_files/fog_store.rb', line 32

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_matcherObject



41
42
43
44
45
46
47
48
# File 'lib/remote_files/fog_store.rb', line 41

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