module Shrine::Plugins::Processing

  1. lib/shrine/plugins/processing.rb

Shrine uploaders can define the #process method, which will get called whenever a file is uploaded. It is given the original file, and is expected to return the processed files.

def process(io, context)
  # you can process the original file `io` and return processed file(s)

However, when handling files as attachments, the same file is uploaded to temporary and permanent storage. Since we only want to apply the same processing once, we need to branch based on the context.

def process(io, context)
  if context[:action] == :store # promote phase
    # ...

The processing plugin simplifies this by allowing us to declaratively define file processing for specified actions.

plugin :processing

process(:store) do |io, context|
  # ...

An example of resizing an image using the image_processing library:

require "image_processing/mini_magick"

process(:store) do |io, context| do |original|
      .resize_to_limit!(800, 800)

The declarations are additive and inheritable, so for the same action you can declare multiple blocks, and they will be performed in the same order, with output from previous block being the input to next.

Manually Run Processing

You can manually trigger the defined processing via the uploader by calling #upload or #process and setting :action to the name of your processing block:

uploader.upload(file, action: :store)  # process and upload
uploader.process(file, action: :store) # only process

If you want the result of processing to be multiple files, use the versions plugin.


Public Class

  1. configure

Public Class methods

configure (uploader)
[show source]
# File lib/shrine/plugins/processing.rb, line 61
def self.configure(uploader)
  uploader.opts[:processing] ||= {}