Shrine

Shrine

  • Guides
  • Plugins
  • External
  • Discourse
  • GitHub
  • Wiki

›Source

Attachment

  • Active Record
  • Column
  • Entity
  • Model
  • Sequel

Flow

  • Backgrounding
  • Presign Endpoint
  • Upload Endpoint

Processing

  • Derivation Endpoint
  • Derivatives

Source

  • Data URI
  • Rack File
  • Remote URL

Validation

  • Remove Invalid
  • Validation
  • Validation Helpers

Metadata

  • Add Metadata
  • Determine MIME Type
  • Infer Extension
  • Metadata Attributes
  • Refresh Metadata
  • Restore Cached Data
  • Signature
  • Store Dimensions
  • Type Predicates

Downloading

  • Download Endpoint
  • Rack Response
  • Tempfile

Form

  • Cached Attachment Data
  • Form Assign
  • Remove Attachment

Settings

  • Default Storage
  • Default URL
  • Dynamic Storage
  • Multi Cache
  • Pretty Location
  • Upload Options
  • URL Options

Other

  • Atomic Helpers
  • Included
  • Instrumentation
  • Keep Files
  • Mirroring
Edit

Rack File

The rack_file plugin enables uploaders to accept Rack uploaded file hashes for uploading.

plugin :rack_file

Usage

When a file is uploaded to your Rack application using the multipart/form-data parameter encoding, Rack converts the uploaded file to a hash.

file_hash #=> 
# { 
#   :name => "file", 
#   :filename => "cats.png", 
#   :type => "image/png", 
#   :tempfile => #<Tempfile:/var/folders/3n/3asd/-Tmp-/RackMultipart201-1476-nfw2-0>, 
#   :head => "Content-Disposition: form-data; ...", 
# } 

Since Shrine only accepts IO objects, you would normally need to fetch the :tempfile object and pass it directly. This plugin enables the attacher to accept the Rack uploaded file hash directly, which is convenient when doing mass attribute assignment.

user.avatar = file_hash
# or 
attacher.assign(file_hash)

API

Internally the Rack uploaded file hash will be converted into an IO object using Shrine.rack_file, which you can also use directly:

# or YourUploader.rack_file(file_hash) 
io = Shrine.rack_file(file_hash)
io.original_filename #=> "cats.png" 
io.content_type      #=> "image/png" 
io.size              #=> 58342 

Note that this plugin is not needed in Rails applications, as Rails already wraps the Rack uploaded file hash into an ActionDispatch::Http::UploadedFile object.

← Data URIRemote URL →
  • Usage
  • API
Shrine
Docs
GuidesPluginsExternalContributing
Community
DiscourseStack Overflow
More
BlogGitHubStar
Follow @shrine_rb
Copyright © 2022 Janko Marohnić