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 :exif do |io, context|
  begin
    Exif::Data.new(io).to_h
  rescue Exif::NotReadable # not a valid image
    {}
  end
end

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

image.metadata["exif"]
# or
image.exif

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|
  begin
    data = Exif::Data.new(io)
  rescue Exif::NotReadable # not a valid image
    next {}
  end

  { date_time:     data.date_time,
    flash:         data.flash,
    focal_length:  data.focal_length,
    exposure_time: data.exposure_time }
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 :date_time, :flash

The io might not always be a file object, so if you're using an analyzer which requires the source file to be on disk, you can use Shrine.with_file to ensure you have a file object.

add_metadata do |io, context|
  movie = Shrine.with_file(io) { |file| FFMPEG::Movie.new(file.path) }

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

Methods

Public Class

  1. configure

Public Class methods

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