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



67
68
69
70
71
72
73
# 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) ⇒ Object



19
20
21
# 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) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# 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

#directoryObject



96
97
98
# File 'lib/remote_files/fog_store.rb', line 96

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

#directory_nameObject



75
76
77
# File 'lib/remote_files/fog_store.rb', line 75

def directory_name
  options[:directory]
end

#files(prefix = '') ⇒ Object

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# 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) ⇒ Object



38
39
40
41
42
43
44
45
# 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_matcherObject



47
48
49
50
51
52
53
54
# 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