Class: Resizing::CarrierWave::Storage::File
- Inherits:
-
Object
- Object
- Resizing::CarrierWave::Storage::File
- Includes:
- CarrierWave::Utilities::Uri
- Defined in:
- lib/resizing/carrier_wave/storage/file.rb
Instance Attribute Summary collapse
-
#public_id ⇒ Object
readonly
Returns the value of attribute public_id.
Instance Method Summary collapse
- #attributes ⇒ Object
- #authenticated_url(_options = {}) ⇒ Object
- #content_type ⇒ Object
- #current_path ⇒ Object (also: #path)
- #delete ⇒ Object
- #exists? ⇒ Boolean
- #extension ⇒ Object
-
#initialize(uploader, identifier = nil) ⇒ File
constructor
A new instance of File.
- #name(options = {}) ⇒ Object
-
#read ⇒ Object
Read content of file from service.
-
#retrieve(identifier) ⇒ Object
def copy_to(new_path) CarrierWave::Storage::Fog::File.new(@uploader, @base, new_path) end.
- #size ⇒ Object
- #store(new_file) ⇒ Object
Constructor Details
#initialize(uploader, identifier = nil) ⇒ File
Returns a new instance of File.
11 12 13 14 15 16 17 |
# File 'lib/resizing/carrier_wave/storage/file.rb', line 11 def initialize(uploader, identifier = nil) @uploader = uploader @content_type = nil @public_id = Resizing::PublicId.new(identifier) @file = nil @metadata = nil end |
Instance Attribute Details
#public_id ⇒ Object (readonly)
Returns the value of attribute public_id.
9 10 11 |
# File 'lib/resizing/carrier_wave/storage/file.rb', line 9 def public_id @public_id end |
Instance Method Details
#attributes ⇒ Object
19 20 21 |
# File 'lib/resizing/carrier_wave/storage/file.rb', line 19 def attributes file.attributes end |
#authenticated_url(_options = {}) ⇒ Object
23 24 25 |
# File 'lib/resizing/carrier_wave/storage/file.rb', line 23 def authenticated_url( = {}) nil end |
#content_type ⇒ Object
27 28 29 |
# File 'lib/resizing/carrier_wave/storage/file.rb', line 27 def content_type @content_type || file.try(:content_type) end |
#current_path ⇒ Object Also known as: path
81 82 83 |
# File 'lib/resizing/carrier_wave/storage/file.rb', line 81 def current_path @current_path = model.send :read_attribute, serialization_column end |
#delete ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/resizing/carrier_wave/storage/file.rb', line 31 def delete @public_id = Resizing::PublicId.new(model.send :read_attribute, serialization_column) return if @public_id.empty? # do nothing resp = client.delete(@public_id.image_id) if resp['error'] == 'ActiveRecord::RecordNotFound' # 404 not found model.send :write_attribute, serialization_column, nil unless model.destroyed? return end if @public_id.image_id == resp['id'] model.send :write_attribute, serialization_column, nil unless model.destroyed? return end raise APIError, "raise someone error:#{resp.inspect}" end |
#exists? ⇒ Boolean
77 78 79 |
# File 'lib/resizing/carrier_wave/storage/file.rb', line 77 def exists? !!file end |
#extension ⇒ Object
49 50 51 |
# File 'lib/resizing/carrier_wave/storage/file.rb', line 49 def extension raise NotImplementedError, 'this method is do not used. maybe' end |
#name(options = {}) ⇒ Object
129 130 131 132 |
# File 'lib/resizing/carrier_wave/storage/file.rb', line 129 def name( = {}) @public_id = PublicId.new(model.send :read_attribute, serialization_column) CGI.unescape(@public_id.filename) end |
#read ⇒ Object
Read content of file from service
Returns
- String
-
contents of file
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/resizing/carrier_wave/storage/file.rb', line 59 def read file_body = file.body return if file_body.nil? return file_body unless file_body.is_a?(::File) # Fog::Storage::XXX::File#body could return the source file which was upoloaded to the remote server. read_source_file(file_body) if ::File.exist?(file_body.path) # If the source file doesn't exist, the remote content is read @file = nil file.body end |
#retrieve(identifier) ⇒ Object
138 139 140 |
# File 'lib/resizing/carrier_wave/storage/file.rb', line 138 def retrieve(identifier) @public_id = Resizing::PublicId.new(identifier) end |
#size ⇒ Object
73 74 75 |
# File 'lib/resizing/carrier_wave/storage/file.rb', line 73 def size file.nil? ? 0 : file.content_length end |
#store(new_file) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/resizing/carrier_wave/storage/file.rb', line 86 def store(new_file) if new_file.is_a?(self.class) # new_file.copy_to(path) raise NotImplementedError, 'new file is required duplicating' end if new_file.respond_to? :content_type @content_type ||= new_file.content_type else # guess content-type from extension @content_type ||= MIME::Types.type_for(new_file.path).first.content_type end original_filename = new_file.try(:original_filename) || new_file.try(:filename) || new_file.try(:path) if original_filename.present? original_filename = ::File.basename(original_filename) end content = if new_file.respond_to?(:to_io) new_file.to_io.tap(&:rewind) elsif new_file.respond_to?(:read) && new_file.respond_to?(:rewind) new_file.read.tap do new_file.rewind end else new_file end @response = Resizing.post(content, { content_type: @content_type, filename: original_filename }) @public_id = Resizing::PublicId.new(@response['public_id']) @content_type = @response['content_type'] # force update column # model_class # .where(primary_key_name => model.send(primary_key_name)) # .update_all(serialization_column=>@public_id) # save new value to model class model.send :write_attribute, serialization_column, @public_id.to_s true end |