Class: CarrierWave::Storage::AWS::File

Inherits:
Object
  • Object
show all
Defined in:
lib/carrierwave/storage/aws.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uploader, connection, path) ⇒ File

Returns a new instance of File.



35
36
37
38
39
# File 'lib/carrierwave/storage/aws.rb', line 35

def initialize(uploader, connection, path)
  @uploader   = uploader
  @connection = connection
  @path       = path
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



33
34
35
# File 'lib/carrierwave/storage/aws.rb', line 33

def connection
  @connection
end

#content_typeObject



45
46
47
# File 'lib/carrierwave/storage/aws.rb', line 45

def content_type
  @content_type || file.content_type
end

#pathObject (readonly)

Returns the value of attribute path.



33
34
35
# File 'lib/carrierwave/storage/aws.rb', line 33

def path
  @path
end

#uploaderObject (readonly)

Returns the value of attribute uploader.



33
34
35
# File 'lib/carrierwave/storage/aws.rb', line 33

def uploader
  @uploader
end

Instance Method Details

#attributesObject



41
42
43
# File 'lib/carrierwave/storage/aws.rb', line 41

def attributes
  file.attributes
end

#authenticated_urlObject



97
98
99
# File 'lib/carrierwave/storage/aws.rb', line 97

def authenticated_url
  file.url_for(:read, expires: uploader.aws_authenticated_url_expiration).to_s
end

#deleteObject



49
50
51
# File 'lib/carrierwave/storage/aws.rb', line 49

def delete
  file.delete
end

#exists?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/carrierwave/storage/aws.rb', line 57

def exists?
  !!file
end

#extensionObject



53
54
55
# File 'lib/carrierwave/storage/aws.rb', line 53

def extension
  path.split('.').last
end

#filename(options = {}) ⇒ Object



61
62
63
64
65
# File 'lib/carrierwave/storage/aws.rb', line 61

def filename(options = {})
  if file_url = url(options)
    file_url.gsub(/.*\/(.*?$)/, '\1')
  end
end

#public_urlObject



101
102
103
104
105
106
107
# File 'lib/carrierwave/storage/aws.rb', line 101

def public_url
  if uploader.asset_host
    "#{uploader.asset_host}/#{path}"
  else
    file.public_url.to_s
  end
end

#readObject



67
68
69
# File 'lib/carrierwave/storage/aws.rb', line 67

def read
  file.read
end

#sizeObject



71
72
73
# File 'lib/carrierwave/storage/aws.rb', line 71

def size
  file.content_length
end

#store(new_file) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/carrierwave/storage/aws.rb', line 75

def store(new_file)
  @file = bucket.objects[path].write({
    acl:          uploader.aws_acl,
    content_type: new_file.content_type,
    file:         new_file.path
  }.merge(uploader.aws_attributes || {}))

  true
end

#to_fileObject



85
86
87
# File 'lib/carrierwave/storage/aws.rb', line 85

def to_file
  file
end

#url(options = {}) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/carrierwave/storage/aws.rb', line 89

def url(options = {})
  if uploader.aws_acl != :public_read
    authenticated_url
  else
    public_url
  end
end