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.



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

def initialize(uploader, connection, path)
  @uploader    = uploader
  @connection  = connection
  @path        = path
  @aws_options = AWSOptions.new(uploader)
end

Instance Attribute Details

#aws_optionsObject

Returns the value of attribute aws_options.



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

def aws_options
  @aws_options
end

#connectionObject

Returns the value of attribute connection.



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

def connection
  @connection
end

#fileObject Also known as: to_file



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

def file
  @file ||= bucket.object(path)
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

#uploaderObject

Returns the value of attribute uploader.



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

def uploader
  @uploader
end

Instance Method Details

#attributesObject



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

def attributes
  file.data.to_h
end

#authenticated_url(options = {}) ⇒ Object



58
59
60
# File 'lib/carrierwave/storage/aws_file.rb', line 58

def authenticated_url(options = {})
  file.presigned_url(:get, aws_options.expiration_options(options))
end

#copy_to(new_path) ⇒ Object



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

def copy_to(new_path)
  bucket.object(new_path).copy_from(
    copy_source: "#{bucket.name}/#{file.key}",
    acl: uploader.aws_acl
  )
end

#extensionObject



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

def extension
  elements = path.split('.')
  elements.last if elements.size > 1
end

#filename(options = {}) ⇒ Object



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

def filename(options = {})
  file_url = url(options)

  CarrierWave::Support::UriFilename.filename(file_url) if file_url
end

#public_urlObject



62
63
64
65
66
67
68
# File 'lib/carrierwave/storage/aws_file.rb', line 62

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.get(aws_options.read_options).body.read
end

#signed_url(options = {}) ⇒ Object



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

def signed_url(options = {})
  signer.call(public_url, options)
end

#store(new_file) ⇒ Object



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

def store(new_file)
  file.put(aws_options.write_options(new_file))
end

#url(options = {}) ⇒ Object



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

def url(options = {})
  if signer
    signed_url(options)
  elsif uploader.aws_acl.to_s != 'public-read'
    authenticated_url(options)
  else
    public_url
  end
end