module Shrine::Plugins::Base::InstanceMethods

  1. lib/shrine.rb

Attributes

storage [R]

The storage object used by the uploader.

storage_key [R]

The symbol identifier for the storage used by the uploader.

Public Class methods

new (storage_key)

Accepts a storage symbol registered in Shrine.storages.

[show source]
# File lib/shrine.rb, line 194
def initialize(storage_key)
  @storage = self.class.find_storage(storage_key)
  @storage_key = storage_key.to_sym
end

Public Instance methods

delete (uploaded_file, context = {})

Deletes the given uploaded file and returns it.

[show source]
# File lib/shrine.rb, line 243
def delete(uploaded_file, context = {})
  _delete(uploaded_file, context)
  uploaded_file
end
extract_metadata (io, context = {})

Extracts filename, size and MIME type from the file, which is later accessible through UploadedFile#metadata.

[show source]
# File lib/shrine.rb, line 261
def extract_metadata(io, context = {})
  {
    "filename"  => extract_filename(io),
    "size"      => extract_size(io),
    "mime_type" => extract_mime_type(io),
  }
end
generate_location (io, context = {})

Generates a unique location for the uploaded file, preserving the file extension. Can be overriden in uploaders for generating custom location.

[show source]
# File lib/shrine.rb, line 251
def generate_location(io, context = {})
  extension   = ".#{io.extension}" if io.is_a?(UploadedFile) && io.extension
  extension ||= File.extname(extract_filename(io).to_s).downcase
  basename  = generate_uid(io)

  basename + extension.to_s
end
opts ()

The class-level options hash. This should probably not be modified at the instance level.

[show source]
# File lib/shrine.rb, line 201
def opts
  self.class.opts
end
process (io, context = {})

User is expected to perform processing inside this method, and return the processed files. Returning nil signals that no proccessing has been done and that the original file should be used.

class ImageUploader < Shrine
  def process(io, context)
    # do processing and return processed files
  end
end
[show source]
# File lib/shrine.rb, line 223
def process(io, context = {})
end
store (io, context = {})

Uploads the file and returns an instance of Shrine::UploadedFile. By default the location of the file is automatically generated by generate_location, but you can pass in :location to upload to a specific location.

uploader.store(io)
[show source]
# File lib/shrine.rb, line 232
def store(io, context = {})
  _store(io, context)
end
upload (io, context = {})

The main method for uploading files. Takes an IO-like object and an optional context hash (used internally by Shrine::Attacher). It calls user-defined process, and aferwards it calls store. The io is closed after upload.

[show source]
# File lib/shrine.rb, line 209
def upload(io, context = {})
  io = processed(io, context) || io
  store(io, context)
end
uploaded? (uploaded_file)

Returns true if the storage of the given uploaded file matches the storage of this uploader.

[show source]
# File lib/shrine.rb, line 238
def uploaded?(uploaded_file)
  uploaded_file.storage_key == storage_key.to_s
end