Class: CarrierWave::Storage::AWSFile

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uploader, connection, path) ⇒ AWSFile

Returns a new instance of AWSFile.



7
8
9
10
11
# File 'lib/carrierwave/storage/aws_file.rb', line 7

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

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



5
6
7
# File 'lib/carrierwave/storage/aws_file.rb', line 5

def connection
  @connection
end

#content_typeObject



17
18
19
# File 'lib/carrierwave/storage/aws_file.rb', line 17

def content_type
  @content_type || file.content_type
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/carrierwave/storage/aws_file.rb', line 5

def path
  @path
end

#uploaderObject (readonly)

Returns the value of attribute uploader.



5
6
7
# File 'lib/carrierwave/storage/aws_file.rb', line 5

def uploader
  @uploader
end

Instance Method Details

#attributesObject



13
14
15
# File 'lib/carrierwave/storage/aws_file.rb', line 13

def attributes
  file.head.data
end

#authenticated_url(options = {}) ⇒ Object



65
66
67
# File 'lib/carrierwave/storage/aws_file.rb', line 65

def authenticated_url(options = {})
  file.url_for(:read, { expires: uploader.aws_authenticated_url_expiration }.merge(options)).to_s
end

#copy_to(new_path) ⇒ Object



77
78
79
# File 'lib/carrierwave/storage/aws_file.rb', line 77

def copy_to(new_path)
  file.copy_to(bucket.objects[new_path], uploader_copy_options)
end

#deleteObject



21
22
23
# File 'lib/carrierwave/storage/aws_file.rb', line 21

def delete
  file.delete
end

#exists?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/carrierwave/storage/aws_file.rb', line 29

def exists?
  file.exists?
end

#extensionObject



25
26
27
# File 'lib/carrierwave/storage/aws_file.rb', line 25

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

#filename(options = {}) ⇒ Object



33
34
35
36
37
# File 'lib/carrierwave/storage/aws_file.rb', line 33

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

#public_urlObject



69
70
71
72
73
74
75
# File 'lib/carrierwave/storage/aws_file.rb', line 69

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

#readObject



39
40
41
# File 'lib/carrierwave/storage/aws_file.rb', line 39

def read
  file.read(uploader_read_options)
end

#sizeObject



43
44
45
# File 'lib/carrierwave/storage/aws_file.rb', line 43

def size
  file.content_length
end

#store(new_file) ⇒ Object



47
48
49
50
51
# File 'lib/carrierwave/storage/aws_file.rb', line 47

def store(new_file)
  @file = bucket.objects[path].write(uploader_write_options(new_file))

  true
end

#to_fileObject



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

def to_file
  file
end

#uploader_copy_optionsObject



95
96
97
98
99
100
101
102
103
# File 'lib/carrierwave/storage/aws_file.rb', line 95

def uploader_copy_options
  aws_write_options = uploader.aws_write_options || {}

  storage_options = aws_write_options.select do |key,_|
    [:reduced_redundancy, :storage_class, :server_side_encryption].include?(key)
  end

  { acl: uploader.aws_acl }.merge(storage_options)
end

#uploader_read_optionsObject



81
82
83
# File 'lib/carrierwave/storage/aws_file.rb', line 81

def uploader_read_options
  uploader.aws_read_options || {}
end

#uploader_write_options(new_file) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/carrierwave/storage/aws_file.rb', line 85

def uploader_write_options(new_file)
  aws_attributes    = uploader.aws_attributes    || {}
  aws_write_options = uploader.aws_write_options || {}

  { acl:          uploader.aws_acl,
    content_type: new_file.content_type,
    file:         new_file.path
  }.merge(aws_attributes).merge(aws_write_options)
end

#url(options = {}) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/carrierwave/storage/aws_file.rb', line 57

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