module Shrine::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
164 def initialize(storage_key)
165   @storage = self.class.find_storage(storage_key)
166   @storage_key = storage_key.to_sym
167 end

Public Instance methods

delete (uploaded_file, context = {})

Deletes the given uploaded file and returns it.

[show source]
    # File lib/shrine.rb
213 def delete(uploaded_file, context = {})
214   _delete(uploaded_file, context)
215   uploaded_file
216 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
231 def extract_metadata(io, context = {})
232   {
233     "filename"  => extract_filename(io),
234     "size"      => extract_size(io),
235     "mime_type" => extract_mime_type(io),
236   }
237 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
221 def generate_location(io, context = {})
222   extension   = ".#{io.extension}" if io.is_a?(UploadedFile) && io.extension
223   extension ||= File.extname(extract_filename(io).to_s).downcase
224   basename    = generate_uid(io)
225 
226   basename + extension
227 end
opts ()

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

[show source]
    # File lib/shrine.rb
171 def opts
172   self.class.opts
173 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
193 def process(io, context = {})
194 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
202 def store(io, context = {})
203   _store(io, context)
204 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 afterwards it calls store. The io is closed after upload.

[show source]
    # File lib/shrine.rb
179 def upload(io, context = {})
180   io = processed(io, context) || io
181   store(io, context)
182 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
208 def uploaded?(uploaded_file)
209   uploaded_file.storage_key == storage_key.to_s
210 end