class Resizing::CarrierWave::Storage::File

Attributes

public_id[R]
uploader[R]

def copy_to(new_path)

CarrierWave::Storage::Fog::File.new(@uploader, @base, new_path)

end

Public Class Methods

new(uploader, identifier = nil) click to toggle source
# File lib/resizing/carrier_wave/storage/file.rb, line 9
def initialize(uploader, identifier = nil)
  @uploader = uploader
  @file = nil
  @content_type = nil
  @public_id = Resizing::PublicId.new identifier
end

Public Instance Methods

attributes() click to toggle source
# File lib/resizing/carrier_wave/storage/file.rb, line 18
def attributes
  file.attributes
end
authenticated_url(_options = {}) click to toggle source
# File lib/resizing/carrier_wave/storage/file.rb, line 22
def authenticated_url(_options = {})
  nil
end
content_type() click to toggle source
# File lib/resizing/carrier_wave/storage/file.rb, line 26
def content_type
  @content_type || file.try(:content_type)
end
current_path() click to toggle source
# File lib/resizing/carrier_wave/storage/file.rb, line 80
def current_path
  @current_path = model.send :read_attribute, serialization_column
end
Also aliased as: path
delete() click to toggle source
# File lib/resizing/carrier_wave/storage/file.rb, line 30
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?() click to toggle source
# File lib/resizing/carrier_wave/storage/file.rb, line 76
def exists?
  !!file
end
extension() click to toggle source
# File lib/resizing/carrier_wave/storage/file.rb, line 48
def extension
  raise NotImplementedError, 'this method is do not used. maybe'
end
name(options = {}) click to toggle source
# File lib/resizing/carrier_wave/storage/file.rb, line 127
def name(options = {})
  @public_id = PublicId.new(model.send :read_attribute, serialization_column)
  CGI.unescape(@public_id.filename)
end
path()
Alias for: current_path
read() click to toggle source

Read content of file from service

Returns

String

contents of file

# File lib/resizing/carrier_wave/storage/file.rb, line 58
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
size() click to toggle source
# File lib/resizing/carrier_wave/storage/file.rb, line 72
def size
  file.nil? ? 0 : file.content_length
end
store(new_file) click to toggle source
# File lib/resizing/carrier_wave/storage/file.rb, line 85
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'])

  # 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

Private Instance Methods

client() click to toggle source

client of Resizing

# File lib/resizing/carrier_wave/storage/file.rb, line 158
def client
  @client ||= if Resizing.configure.enable_mock
                Resizing::MockClient.new
              else
                Resizing::Client.new
              end
end
model() click to toggle source
# File lib/resizing/carrier_wave/storage/file.rb, line 140
def model
  @model ||= uploader.model
end
model_class() click to toggle source
# File lib/resizing/carrier_wave/storage/file.rb, line 144
def model_class
  @model_class ||= model.class
end
primary_key_name() click to toggle source
# File lib/resizing/carrier_wave/storage/file.rb, line 148
def primary_key_name
  @primary_key_name ||= model_class.primary_key.to_sym
end
read_source_file(file_body) click to toggle source

lookup file

Returns

Fog::#{provider}::File

file data from remote service

def file

@file ||= directory.files.head(path)

end

# File lib/resizing/carrier_wave/storage/file.rb, line 177
def read_source_file(file_body)
  return unless ::File.exist?(file_body.path)

  begin
    file_body = ::File.open(file_body.path) if file_body.closed? # Reopen if it's already closed
    file_body.read
  ensure
    file_body.close
  end
end
retrieve!(identifier) click to toggle source
# File lib/resizing/carrier_wave/storage/file.rb, line 193
def retrieve!(identifier)
  raise NotImplementedError, "retrieve! #{identifier}"
end
serialization_column() click to toggle source
# File lib/resizing/carrier_wave/storage/file.rb, line 152
def serialization_column
  @serialization_column ||= model.send(:_mounter, uploader.mounted_as).send(:serialization_column)
end
url_options_supported?(local_file) click to toggle source
# File lib/resizing/carrier_wave/storage/file.rb, line 188
def url_options_supported?(local_file)
  parameters = local_file.method(:url).parameters
  parameters.count == 2 && parameters[1].include?(:options)
end