Class: CarrierWave::Storage::GcloudFile
- Inherits:
-
Object
- Object
- CarrierWave::Storage::GcloudFile
- Defined in:
- lib/carrierwave/storage/gcloud_file.rb
Constant Summary collapse
- GCLOUD_STORAGE_URL =
'https://storage.googleapis.com'
Instance Attribute Summary collapse
-
#connection ⇒ Object
Returns the value of attribute connection.
- #file ⇒ Object (also: #to_file)
-
#file_exists ⇒ Object
Returns the value of attribute file_exists.
-
#gcloud_options ⇒ Object
Returns the value of attribute gcloud_options.
-
#path ⇒ Object
Returns the value of attribute path.
-
#uploader ⇒ Object
Returns the value of attribute uploader.
Instance Method Summary collapse
- #attributes ⇒ Object
- #authenticated_url(options = {}) ⇒ Object
- #copy_to(new_path) ⇒ Object
- #delete ⇒ Object
- #exists? ⇒ Boolean
- #extension ⇒ Object
- #filename(options = {}) ⇒ Object
-
#initialize(uploader, connection, path) ⇒ GcloudFile
constructor
A new instance of GcloudFile.
- #public_url ⇒ Object
- #read ⇒ Object
- #store(new_file) ⇒ Object
- #url(options = {}) ⇒ Object
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
#connection ⇒ Object
Returns the value of attribute connection.
9 10 11 |
# File 'lib/carrierwave/storage/gcloud_file.rb', line 9 def connection @connection end |
#file ⇒ Object 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_exists ⇒ Object
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_options ⇒ Object
Returns the value of attribute gcloud_options.
9 10 11 |
# File 'lib/carrierwave/storage/gcloud_file.rb', line 9 def @gcloud_options end |
#path ⇒ Object
Returns the value of attribute path.
9 10 11 |
# File 'lib/carrierwave/storage/gcloud_file.rb', line 9 def path @path end |
#uploader ⇒ Object
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
#attributes ⇒ Object
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( = {}) bucket.signed_url(path, ) 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 |
#delete ⇒ Object
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
40 41 42 |
# File 'lib/carrierwave/storage/gcloud_file.rb', line 40 def exists? !file.nil? end |
#extension ⇒ Object
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( = {}) CarrierWave::Support::UriFilename.filename(file.url) end |
#public_url ⇒ Object
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 |
#read ⇒ Object
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( = {}) uploader.gcloud_bucket_is_public ? public_url : authenticated_url() end |