Class: CarrierWave::Storage::GcloudFile

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uploader, connection, path) ⇒ GcloudFile

Returns a new instance of GcloudFile.



11
12
13
# File 'lib/carrierwave/storage/gcloud_file.rb', line 11

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

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



6
7
8
# File 'lib/carrierwave/storage/gcloud_file.rb', line 6

def connection
  @connection
end

#fileObject Also known as: to_file



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

def file
  by_verifying_existence { @file ||= bucket.file(path) }
  @file
end

#file_existsObject

Returns the value of attribute file_exists.



6
7
8
# File 'lib/carrierwave/storage/gcloud_file.rb', line 6

def file_exists
  @file_exists
end

#gcloud_optionsObject

Returns the value of attribute gcloud_options.



6
7
8
# File 'lib/carrierwave/storage/gcloud_file.rb', line 6

def gcloud_options
  @gcloud_options
end

#pathObject

Returns the value of attribute path.



6
7
8
# File 'lib/carrierwave/storage/gcloud_file.rb', line 6

def path
  @path
end

#uploaderObject

Returns the value of attribute uploader.



6
7
8
# File 'lib/carrierwave/storage/gcloud_file.rb', line 6

def uploader
  @uploader
end

Instance Method Details

#attributesObject



35
36
37
38
39
40
41
42
43
# File 'lib/carrierwave/storage/gcloud_file.rb', line 35

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

#authenticated_url(options = {}) ⇒ Object



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

def authenticated_url(options = {})
  file.signed_url
end

#by_verifying_existence(&block) ⇒ Object



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

def by_verifying_existence(&block)
  begin
    self.file_exists = true
    yield
  rescue Exception => exception
    self.file_exists = false if (exception.class == ::Gcloud::Storage::ApiError) && (exception.message == "Not Found")
  end
end

#copy_to(new_path) ⇒ Object



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

def copy_to(new_path)
  file.copy("#{uploader.store_dir}/#{new_path}")
end

#deleteObject



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

def delete
  deleted = file.delete
  self.file_exists = false if deleted
  deleted
end

#exists?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/carrierwave/storage/gcloud_file.rb', line 51

def exists?
  self.file_exists
end

#extensionObject



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

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

#filename(options = {}) ⇒ Object



60
61
62
# File 'lib/carrierwave/storage/gcloud_file.rb', line 60

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

#public_urlObject



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

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

#readObject



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

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

#retrieveObject



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

def retrieve
  by_verifying_existence { @file ||= bucket.file(path) }
  self
end

#store(new_file) ⇒ Object



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

def store(new_file)
  new_file_path = uploader.filename ?  uploader.filename : new_file.filename
  bucket.create_file new_file.path, "#{uploader.store_dir}/#{new_file_path}" 
  self
end

#url(options = {}) ⇒ Object



79
80
81
82
# File 'lib/carrierwave/storage/gcloud_file.rb', line 79

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