module Shrine::Plugins::AddMetadata

  1. lib/shrine/plugins/add_metadata.rb

The add_metadata plugin provides a convenient method for extracting and adding custom metadata values.

plugin :add_metadata

add_metadata :pages do |io, context|
  PDF::Reader.new(io.path).page_count
end

The above will add “pages” to the metadata hash, and also create the #pages reader method on Shrine::UploadedFile.

document.metadata["pages"]
# or
document.pages

You can also extract multiple metadata values at once, by using add_metadata without an argument, and returning a hash of metadata.

add_metadata do |io, context|
  movie = FFMPEG::Movie.new(io.path)

  { "duration"   => movie.duration,
    "bitrate"    => movie.bitrate,
    "resolution" => movie.resolution,
    "frame_rate" => movie.frame_rate }
end

In this case Shrine won't automatically create reader methods for the extracted metadata on Shrine::UploadedFile, but you can create them via metadata_method.

metadata_method :duration, :bitrate, :resolution, :frame_rate

Methods

Public Class

  1. configure

Public Class methods

configure (uploader)
[show source]
# File lib/shrine/plugins/add_metadata.rb, line 37
def self.configure(uploader)
  uploader.opts[:metadata] = []
end