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.



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

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

Instance Attribute Details

#aws_options ⇒ Object

Returns the value of attribute aws_options.



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

def aws_options
  @aws_options
end

#connection ⇒ Object

Returns the value of attribute connection.



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

def connection
  @connection
end

#file ⇒ Object Also known as: to_file



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

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

#path ⇒ Object

Returns the value of attribute path.



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

def path
  @path
end

#uploader ⇒ Object

Returns the value of attribute uploader.



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

def uploader
  @uploader
end

Instance Method Details

#attributes ⇒ Object



32
33
34
# File 'lib/carrierwave/storage/aws_file.rb', line 32

def attributes
  file.data.to_h
end

#authenticated_url(options = {}) ⇒ Object



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

def authenticated_url(options = {})
  if asset_host && asset_host == bucket.name
    # Can't use https, since plain S3 doesn't support custom TLS certificates
    options = options.reverse_merge(secure: false, virtual_host: true)
  end
  file.presigned_url(:get, aws_options.expiration_options(options))
end

#copy_to(new_path) ⇒ Object



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

def copy_to(new_path)
  file.copy_to(
    bucket.object(new_path),
    aws_options.copy_options(self)
  )
end

#extension ⇒ Object



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

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

#filename(options = {}) ⇒ Object



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

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

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

#move_to(new_path) ⇒ Object



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

def move_to(new_path)
  file.move_to(
    "#{bucket.name}/#{new_path}",
    aws_options.move_options(self)
  )
end

#public_url ⇒ Object



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

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

#read ⇒ Object



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

def read
  read_options = aws_options.read_options
  if block_given?
    file.get(read_options) { |chunk| yield chunk }
    nil
  else
    file.get(read_options).body.read
  end
end

#signed_url(options = {}) ⇒ Object



84
85
86
# File 'lib/carrierwave/storage/aws_file.rb', line 84

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

#size ⇒ Object



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

def size
  file.size
rescue Aws::S3::Errors::NotFound
  nil
end

#store(new_file) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/carrierwave/storage/aws_file.rb', line 57

def store(new_file)
  if new_file.is_a?(self.class)
    new_file.move_to(path)
  elsif Aws::S3.const_defined?(:TransferManager)
    options = aws_options.write_options(new_file).except(:body)
    options[:multipart_threshold] = AWSOptions::MULTIPART_THRESHOLD
    Aws::S3::TransferManager.new(client: connection.client).upload_file(new_file.path, bucket: bucket.name,
                                                                                       key: path, **options)
  else
    file.upload_file(new_file.path, aws_options.write_options(new_file))
  end
end

#url(options = {}) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/carrierwave/storage/aws_file.rb', line 104

def url(options = {})
  if signer
    signed_url(options)
  elsif public?
    public_url
  else
    authenticated_url(options)
  end
end