Class: CarrierWave::Storage::GcloudFile

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

Constant Summary collapse

GCLOUD_STORAGE_URL =
'https://storage.googleapis.com'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uploader, connection, path) ⇒ GcloudFile

Returns a new instance of GcloudFile.



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

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

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



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

def connection
  @connection
end

#fileObject Also known as: to_file



19
20
21
# File 'lib/carrierwave/storage/gcloud_file.rb', line 19

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

#file_existsObject

Returns the value of attribute file_exists.



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

def file_exists
  @file_exists
end

#gcloud_optionsObject

Returns the value of attribute gcloud_options.



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

def gcloud_options
  @gcloud_options
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

#uploaderObject

Returns the value of attribute uploader.



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

def uploader
  @uploader
end

Instance Method Details

#attributesObject



24
25
26
27
28
29
30
31
32
# File 'lib/carrierwave/storage/gcloud_file.rb', line 24

def attributes
  return unless exists?
  {
    content_type: file.content_type,
    size: file.size,
    updated_at: file.updated_at.to_s,
    etag: file.etag
  }
end

#authenticated_url(options = {}) ⇒ Object



86
87
88
# File 'lib/carrierwave/storage/gcloud_file.rb', line 86

def authenticated_url(options = {})
  bucket.signed_url(path, options)
end

#copy_to(new_path) ⇒ Object



75
76
77
78
79
80
# File 'lib/carrierwave/storage/gcloud_file.rb', line 75

def copy_to(new_path)
  file.copy(
    new_path,
    acl: uploader.gcloud_bucket_is_public ? 'publicRead' : nil
  )
end

#deleteObject



34
35
36
37
38
# File 'lib/carrierwave/storage/gcloud_file.rb', line 34

def delete
  deleted = file ? file.delete : true
  @file = nil if deleted
  deleted
end

#exists?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/carrierwave/storage/gcloud_file.rb', line 40

def exists?
  !file.nil?
end

#extensionObject



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

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

#filename(options = {}) ⇒ Object



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

def filename(options = {})
  CarrierWave::Support::UriFilename.filename(file.url)
end

#public_urlObject



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

def public_url
  if uploader.asset_host
    "#{uploader.asset_host}/#{path}"
  else
    "#{GCLOUD_STORAGE_URL}/#{uploader.gcloud_bucket}/#{@path}"
  end
end

#readObject



53
54
55
56
57
58
# File 'lib/carrierwave/storage/gcloud_file.rb', line 53

def read
  tmp_file = Tempfile.new(
    CarrierWave::Support::UriFilename.filename(file.name)
  )
  (file.download tmp_file.path, verify: :all).read
end

#store(new_file) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/carrierwave/storage/gcloud_file.rb', line 60

def store(new_file)
  if new_file.is_a?(self.class)
    new_file.copy_to(path)
  else
    @file = bucket.create_file(
      new_file.path,
      path,
      acl: uploader.gcloud_bucket_is_public ? 'publicRead' : nil,
      content_type: new_file.content_type,
      content_disposition: uploader.gcloud_content_disposition
    )
  end
  self
end

#url(options = {}) ⇒ Object



82
83
84
# File 'lib/carrierwave/storage/gcloud_file.rb', line 82

def url(options = {})
  uploader.gcloud_bucket_is_public ? public_url : authenticated_url(options)
end