Class: CarrierWave::Storage::GcloudFile
- Inherits:
-
Object
- Object
- CarrierWave::Storage::GcloudFile
- Defined in:
- lib/carrierwave/storage/gcloud_file.rb
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
- #by_verifying_existence(&block) ⇒ 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
- #retrieve ⇒ Object
- #store(new_file) ⇒ Object
- #url(options = {}) ⇒ Object
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
#connection ⇒ Object
Returns the value of attribute connection.
6 7 8 |
# File 'lib/carrierwave/storage/gcloud_file.rb', line 6 def connection @connection end |
#file ⇒ Object Also known as: to_file
15 16 17 18 |
# File 'lib/carrierwave/storage/gcloud_file.rb', line 15 def file { @file ||= bucket.file(path) } @file end |
#file_exists ⇒ Object
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_options ⇒ Object
Returns the value of attribute gcloud_options.
6 7 8 |
# File 'lib/carrierwave/storage/gcloud_file.rb', line 6 def end |
#path ⇒ Object
Returns the value of attribute path.
6 7 8 |
# File 'lib/carrierwave/storage/gcloud_file.rb', line 6 def path @path end |
#uploader ⇒ Object
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
#attributes ⇒ Object
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( = {}) 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 (&block) begin self.file_exists = true yield rescue Exception => exception self.file_exists = false if (exception.class == ::Gcloud::Storage::ApiError) && (exception. == "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 |
#delete ⇒ Object
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
51 52 53 |
# File 'lib/carrierwave/storage/gcloud_file.rb', line 51 def exists? self.file_exists end |
#extension ⇒ Object
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( = {}) CarrierWave::Support::UriFilename.filename(file.url) end |
#public_url ⇒ Object
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 |
#read ⇒ Object
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 |
#retrieve ⇒ Object
21 22 23 24 |
# File 'lib/carrierwave/storage/gcloud_file.rb', line 21 def retrieve { @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( = {}) return unless file_exists uploader.gcloud_bucket_is_public ? public_url : authenticated_url end |