Module: Shrine::InstanceMethods

Included in:
Shrine
Defined in:
lib/shrine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#storageObject (readonly)

The storage object used by the uploader.



179
180
181
# File 'lib/shrine.rb', line 179

def storage
  @storage
end

#storage_keyObject (readonly)

The symbol identifier for the storage used by the uploader.



176
177
178
# File 'lib/shrine.rb', line 176

def storage_key
  @storage_key
end

Instance Method Details

#delete(uploaded_file, context = {}) ⇒ Object

Deletes the given uploaded file and returns it.



238
239
240
241
# File 'lib/shrine.rb', line 238

def delete(uploaded_file, context = {})
  _delete(uploaded_file, context)
  uploaded_file
end

#extract_metadata(io, context = {}) ⇒ Object

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



252
253
254
255
256
257
258
# File 'lib/shrine.rb', line 252

def (io, context = {})
  {
    "filename"  => extract_filename(io),
    "size"      => extract_size(io),
    "mime_type" => extract_mime_type(io),
  }
end

#generate_location(io, context = {}) ⇒ Object

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



246
247
248
# File 'lib/shrine.rb', line 246

def generate_location(io, context = {})
  basic_location(io)
end

#initialize(storage_key) ⇒ Object

Accepts a storage symbol registered in ‘Shrine.storages`.

Shrine.new(:store)


184
185
186
187
# File 'lib/shrine.rb', line 184

def initialize(storage_key)
  @storage = self.class.find_storage(storage_key)
  @storage_key = storage_key.to_sym
end

#optsObject

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



191
192
193
# File 'lib/shrine.rb', line 191

def opts
  self.class.opts
end

#process(io, context = {}) ⇒ Object

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


218
219
# File 'lib/shrine.rb', line 218

def process(io, context = {})
end

#store(io, context = {}) ⇒ Object

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)


227
228
229
# File 'lib/shrine.rb', line 227

def store(io, context = {})
  _store(io, context)
end

#upload(io, context = {}) ⇒ Object

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


204
205
206
207
# File 'lib/shrine.rb', line 204

def upload(io, context = {})
  io = processed(io, context) || io
  store(io, context)
end

#uploaded?(uploaded_file) ⇒ Boolean

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

Returns:

  • (Boolean)


233
234
235
# File 'lib/shrine.rb', line 233

def uploaded?(uploaded_file)
  uploaded_file.storage_key == storage_key.to_s
end