Module: Shrine::Plugins::KitheDerivatives::InstanceMethods

Defined in:
lib/shrine/plugins/kithe_derivatives.rb

Instance Method Summary collapse

Instance Method Details

#extract_metadata(io, derivative: nil, **context) ⇒ Object

  1. Override to fix “filename” metadata to be something reasonable, regardless

of what if anything was the filename of the IO being attached. shrine S3 will insist on setting a default content-disposition with this filename, so we can use that.

  1. Set any specified derivative metadata on derivative. Specified add_metadata

found in context.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/shrine/plugins/kithe_derivatives.rb', line 42

def (io, derivative:nil, **context)
  result = super

  if derivative && context[:record] && result["mime_type"]
    extension = MiniMime.lookup_by_content_type(result["mime_type"] || "")&.extension || "bin"
    result["filename"] = "#{context[:record].friendlier_id}_#{derivative}.#{extension}"
  end

  # If derivative, add timestamp and any specified extra data
  if derivative
    result["created_at"] ||= Time.current.utc.iso8601.to_s

    if ( = context.dig(:add_metadata, derivative)).present?
      result.merge!(.stringify_keys)
    end
  end

  result
end