module Shrine::Plugins::Base::InstanceMethods

  1. lib/shrine.rb


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 195
def initialize(storage_key)
  @storage = self.class.find_storage(storage_key)
  @storage_key = storage_key.to_sym

Public Instance methods

delete (uploaded_file, context = {})

Deletes the given uploaded file and returns it.

[show source]
# File lib/shrine.rb, line 244
def delete(uploaded_file, context = {})
  _delete(uploaded_file, context)
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 262
def extract_metadata(io, context = {})
    "filename"  => extract_filename(io),
    "size"      => extract_size(io),
    "mime_type" => extract_mime_type(io),
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 252
def generate_location(io, context = {})
  extension   = ".#{io.extension}" if io.is_a?(UploadedFile) && io.extension
  extension ||= File.extname(extract_filename(io).to_s)
  basename  = generate_uid(io)

  basename + extension.to_s
opts ()

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

[show source]
# File lib/shrine.rb, line 202
def opts
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
[show source]
# File lib/shrine.rb, line 224
def process(io, context = {})
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.
[show source]
# File lib/shrine.rb, line 233
def store(io, context = {})
  _store(io, context)
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 210
def upload(io, context = {})
  io = processed(io, context) || io
  store(io, context)
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 239
def uploaded?(uploaded_file)
  uploaded_file.storage_key == storage_key.to_s