Class: Attachs::Storages::S3

Inherits:
Object
  • Object
show all
Defined in:
lib/attachs/storages/s3.rb

Instance Method Summary collapse

Constructor Details

#initialize(default) ⇒ S3

Returns a new instance of S3.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/attachs/storages/s3.rb', line 7

def initialize(default)
  if (Rails.env.test? and !default)
    env = 'test'
  elsif default
    env = 'production'
  else
    env = Rails.env
  end
  config = self.class.config[env]
  AWS.config access_key_id: config['access_key_id'], secret_access_key: config['secret_access_key']
  @bucket = AWS::S3.new.buckets[config['bucket']]
end

Instance Method Details

#delete(path) ⇒ Object



37
38
39
# File 'lib/attachs/storages/s3.rb', line 37

def delete(path)
  object(path).delete
end

#exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/attachs/storages/s3.rb', line 20

def exists?(path)
  object(path).exists?
end

#magick(source, output, upload) {|Attachs::Magick::Image.new(cache[source], tmp.path)| ... } ⇒ Object

Yields:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/attachs/storages/s3.rb', line 41

def magick(source, output, upload)
  if cache[source].blank?
    if upload.present?
      cache[source] = upload.path
    else
      remote = create_tmp_file
      remote.binmode
      object(source).read { |chunk| remote.write(chunk) }
      remote.close
      remote.open
      cache[source] = remote.path
    end
  end
  tmp = create_tmp_file
  yield Attachs::Magick::Image.new(cache[source], tmp.path)
  store tmp, output
end

#size(path) ⇒ Object



24
25
26
# File 'lib/attachs/storages/s3.rb', line 24

def size(path)
  object(path).content_length
end

#store(upload, path) ⇒ Object



33
34
35
# File 'lib/attachs/storages/s3.rb', line 33

def store(upload, path)
  bucket.objects.create(path, Pathname.new(upload.path)).acl = :public_read
end

#url(path) ⇒ Object



28
29
30
31
# File 'lib/attachs/storages/s3.rb', line 28

def url(path)
  base_url = Rails.application.config.attachs.base_url
  base_url.present? ? ::File.join(base_url, path) : object(path).public_url(secure: false)
end