module Shrine::InstanceMethods

  1. lib/shrine.rb

Methods

Public Class

  1. new

Public Instance

  1. extract_metadata
  2. generate_location
  3. opts
  4. storage
  5. storage_key
  6. upload

Attributes

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.

Shrine.new(:store)
[show source]
    # File lib/shrine.rb
184 def initialize(storage_key)
185   @storage_key = storage_key.to_sym
186 
187   storage # ensure storage is registered
188 end

Public Instance methods

extract_metadata(io, **options)

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

[show source]
    # File lib/shrine.rb
228 def extract_metadata(io, **options)
229   {
230     "filename"  => extract_filename(io),
231     "size"      => extract_size(io),
232     "mime_type" => extract_mime_type(io),
233   }
234 end
generate_location(io, metadata: {}, **options)

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
222 def generate_location(io, metadata: {}, **options)
223   basic_location(io, metadata: metadata)
224 end
opts()

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

[show source]
    # File lib/shrine.rb
238 def opts
239   self.class.opts
240 end
storage()

Returns the storage object referenced by the identifier.

[show source]
    # File lib/shrine.rb
191 def storage
192   self.class.find_storage(storage_key)
193 end
upload(io, **options)

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.

uploader.upload(io) uploader.upload(io, metadata: { “foo” => “bar” }) # add metadata uploader.upload(io, location: “path/to/file”) # specify location uploader.upload(io, upload_options: { acl: “public-read” }) # add upload options

[show source]
    # File lib/shrine.rb
204 def upload(io, **options)
205   _enforce_io(io)
206 
207   metadata = get_metadata(io, **options)
208   location = get_location(io, **options, metadata: metadata)
209 
210   _upload(io, **options, location: location, metadata: metadata)
211 
212   self.class::UploadedFile.new(
213     id:       location,
214     storage:  storage_key,
215     metadata: metadata,
216   )
217 end