Class: Kithe::DerivativeUploader

Inherits:
Shrine
  • Object
show all
Defined in:
app/uploaders/kithe/derivative_uploader.rb

Overview

The derivative uploader doesn’t have to do too much, we don’t even use promotion for derivatives, just writing directly to a storage.

But it needs activerecord integration, and limited metadata automatic extraction.

Instance Method Summary collapse

Instance Method Details

#extract_metadata(io, context = {}) ⇒ Object

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.



32
33
34
35
36
37
38
39
40
41
42
# File 'app/uploaders/kithe/derivative_uploader.rb', line 32

def (io, context = {})
  result = super

  if context.dig(:metadata, :kithe_derivative_key) &&
     context[:record]
    extension = MiniMime.lookup_by_content_type(result["mime_type"] || "")&.extension
    result["filename"] = "#{context[:record].asset.friendlier_id}_#{context[:metadata][:kithe_derivative_key]}.#{extension}"
  end

  return result
end

#generate_location(io, context) ⇒ Object

should this be in a plugin? location in file system based on original asset id and derivative key, as well as unique random file id from shrine.



20
21
22
23
24
25
26
# File 'app/uploaders/kithe/derivative_uploader.rb', line 20

def generate_location(io, context)
  # assumes we're only used with Derivative model, that has an asset_id and key
  asset_id = context[:record].asset_id
  key = context[:record].key
  original = super
  [asset_id, key, original].compact.join("/")
end