Class: Shutterbug::Storage::S3Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/shutterbug/storage/s3_storage.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(long_path, filetype) ⇒ S3Storage

Returns a new instance of S3Storage.



54
55
56
57
58
59
# File 'lib/shutterbug/storage/s3_storage.rb', line 54

def initialize(long_path, filetype)
  @filename = File.basename(long_path)
  @source = long_path
  @stream_file = S3Storage.write(@filename, long_path)
  @url = @stream_file.public_url
end

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



6
7
8
# File 'lib/shutterbug/storage/s3_storage.rb', line 6

def filename
  @filename
end

#stream_fileObject

Returns the value of attribute stream_file.



8
9
10
# File 'lib/shutterbug/storage/s3_storage.rb', line 8

def stream_file
  @stream_file
end

#urlObject

Returns the value of attribute url.



7
8
9
# File 'lib/shutterbug/storage/s3_storage.rb', line 7

def url
  @url
end

Class Method Details

.connect!Object



10
11
12
13
14
15
16
# File 'lib/shutterbug/storage/s3_storage.rb', line 10

def self.connect!
  Fog::Storage.new({
    :provider                 => 'AWS',
    :aws_access_key_id        => Configuration.instance.s3_key,
    :aws_secret_access_key    => Configuration.instance.s3_secret
  })
end

.connectionObject



18
19
20
# File 'lib/shutterbug/storage/s3_storage.rb', line 18

def self.connection
  @connection ||= self.connect!
end

.create_binObject



22
23
24
25
26
# File 'lib/shutterbug/storage/s3_storage.rb', line 22

def self.create_bin
  self.connection.directories.create(
    :key    => Configuration.instance.s3_bin,
    :public => true)
end

.find(path) ⇒ Object



42
43
44
# File 'lib/shutterbug/storage/s3_storage.rb', line 42

def self.find(path)
  self.s3_bin.files.get(path)
end

.fs_path_exists?(long_path) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/shutterbug/storage/s3_storage.rb', line 46

def self.fs_path_exists?(long_path)
  File.exists? long_path
end

.handler_for(type) ⇒ Object



50
51
52
# File 'lib/shutterbug/storage/s3_storage.rb', line 50

def self.handler_for(type)
  return self.handlers[type]
end

.s3_binObject



28
29
30
# File 'lib/shutterbug/storage/s3_storage.rb', line 28

def self.s3_bin
  @s3_bin ||= self.create_bin
end

.write(name, filename) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/shutterbug/storage/s3_storage.rb', line 32

def self.write(name, filename)
  full_path = Configuration.instance.fs_path_for(filename)
  if self.fs_path_exists? full_path
    self.s3_bin.files.create(
      :key    => name,
      :body   => File.open(full_path),
      :public => true)
  end
end

Instance Method Details

#get_contentObject



61
62
63
# File 'lib/shutterbug/storage/s3_storage.rb', line 61

def get_content
  @stream_file.body
end

#redirect_s3Object



69
70
71
# File 'lib/shutterbug/storage/s3_storage.rb', line 69

def redirect_s3
  return [301, {"Location" => self.url}, []]
end

#sizeObject



65
66
67
# File 'lib/shutterbug/storage/s3_storage.rb', line 65

def size
  @stream_file.content_length
end