Shrine

Shrine

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

›Downloading

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

Tempfile

The tempfile plugin makes it easier to reuse a single copy of an uploaded file on disk.

Shrine.plugin :tempfile

The plugin provides the UploadedFile#tempfile method, which when called on an open uploaded file will return a copy of its content on disk. The first time the method is called the file content will cached into a temporary file and returned. On any subsequent method calls the cached temporary file will be returned directly. The temporary file is deleted when the uploaded file is closed.

uploaded_file.open do
  # ... 
  uploaded_file.tempfile #=> #<Tempfile:...> (file is cached) 
  # ... 
  uploaded_file.tempfile #=> #<Tempfile:...> (cache is returned) 
  # ... 
end # tempfile is deleted 
 
# OR 
 
uploaded_file.open
# ... 
uploaded_file.tempfile #=> #<Tempfile:...> (file is cached) 
# ... 
uploaded_file.tempfile #=> #<Tempfile:...> (cache is returned) 
# ... 
uploaded_file.close # tempfile is deleted 

This plugin also modifies Shrine.with_file to call UploadedFile#tempfile when the given IO object is an open UploadedFile. Since Shrine.with_file is typically called on the Shrine class directly, it's recommended to load this plugin globally.

← Rack ResponseCached Attachment Data →
Shrine
Docs
GuidesPluginsExternalContributing
Community
DiscourseStack Overflow
More
BlogGitHubStar
Follow @shrine_rb
Copyright © 2022 Janko Marohnić