Shrine

Shrine

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

›Metadata

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

Refresh Metadata

The refresh_metadata plugin allows you to re-extract metadata from an uploaded file.

plugin :refresh_metadata

It provides #refresh_metadata! method, which triggers metadata extraction (calls Shrine#extract_metadata) with the uploaded file opened for reading, and updates the existing metadata hash with the results. This can be done on the Shrine::Attacher or the Shrine::UploadedFile level.

Attacher

Calling #refresh_metadata! on a Shrine::Attacher object will re-extract metadata of the attached file, and when used with a model, it will write new file data back into the attachment attribute.

attacher.refresh_metadata!
attacher.file.metadata    # re-extracted metadata 
attacher.record.file_data #=> '{ ... data with updated metadata ... }' 

The Attacher#context hash will be forwarded to metadata extraction, as well as any options that you pass in.

# via context 
attacher.context[:foo] = "bar"
attacher.refresh_metadata! # passes `{ foo: "bar" }` options to metadata extraction 
 
# via arguments 
attacher.refresh_metadata!(foo: "bar") # passes `{ foo: "bar" }` options to metadata extraction 

Uploaded File

The #refresh_metadata! method can be called on a Shrine::UploadedFile object as well.

uploaded_file.refresh_metadata!
uploaded_file.metadata # re-extracted metadata 

If the uploaded file is not open, it is opened before and closed after metadata extraction. For remote storage services this will make an HTTP request. However, only the portion of the file needed for extracting metadata will be downloaded.

If the uploaded file is already open, it is passed to metadata extraction as is.

uploaded_file.open do
  uploaded_file.refresh_metadata! # uses the already opened file 
  # ... 
end

Any options passed in will be forwarded to metadata extraction:

uploaded_file.refresh_metadata!(foo: "bar") # passes `{ foo: "bar" }` options to metadata extraction 
← Metadata AttributesRestore Cached Data →
  • Attacher
  • Uploaded File
Shrine
Docs
GuidesPluginsExternalContributing
Community
DiscourseStack Overflow
More
BlogGitHubStar
Follow @shrine_rb
Copyright © 2022 Janko Marohnić