Class: Kithe::Asset::DerivativeCreator

Inherits:
Object
  • Object
show all
Defined in:
app/models/kithe/asset/derivative_creator.rb

Overview

Creates derivatives from definitions stored on an Asset class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definitions, source_io:, shrine_attacher:, only: nil, except: nil, lazy: false) ⇒ DerivativeCreator

A helper class that provides the implementation for Kithe::Asset#create_derivatives, normally only expected to be called from there.

Creates derivatives according to derivative definitions. Normally any definition with ‘default_create` true, but that can be changed with `only:` and `except:` params, which take arrays of definition keys.

Bytestream returned by a derivative definition block will be closed AND unlinked (deleted) if it is a File or Tempfile object.

Parameters:

  • definitions

    an array of DerivativeDefinition

  • shrine_attacher

    a shrine attacher instance holding attachment state from an individual model, from eg ‘asset.file_attacher`

  • only (defaults to: nil)

    array of definition keys, only execute these (doesn’t matter if they are ‘default_create` or not)

  • except (defaults to: nil)

    array of definition keys, exclude these from definitions of derivs to be created

  • lazy (default false) (defaults to: false)

    , Normally we will create derivatives for all applicable definitions, overwriting any that already exist for a given key. If the definition has changed, a new derivative created with new definition will overwrite existing. However, if you pass lazy false, it’ll skip derivative creation if the derivative already exists, which can save time if you are only intending to create missing derivatives.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/kithe/asset/derivative_creator.rb', line 25

def initialize(definitions, source_io:, shrine_attacher:, only:nil, except:nil, lazy: false)
  @definitions = definitions
  @source_io = source_io
  @shrine_attacher = shrine_attacher
  @only = only && Array(only).collect(&:to_sym)
  @except = except && Array(except).collect(&:to_sym)
  @lazy = !!lazy

  unless shrine_attacher.kind_of?(Shrine::Attacher)
    raise ArgumentError.new("expect second arg Shrine::Attacher not #{shrine_attacher.class}")
  end
end

Instance Attribute Details

#definitionsObject (readonly)

Returns the value of attribute definitions.



3
4
5
# File 'app/models/kithe/asset/derivative_creator.rb', line 3

def definitions
  @definitions
end

#exceptObject (readonly)

Returns the value of attribute except.



3
4
5
# File 'app/models/kithe/asset/derivative_creator.rb', line 3

def except
  @except
end

#lazyObject (readonly)

Returns the value of attribute lazy.



3
4
5
# File 'app/models/kithe/asset/derivative_creator.rb', line 3

def lazy
  @lazy
end

#onlyObject (readonly)

Returns the value of attribute only.



3
4
5
# File 'app/models/kithe/asset/derivative_creator.rb', line 3

def only
  @only
end

#shrine_attacherObject (readonly)

Returns the value of attribute shrine_attacher.



3
4
5
# File 'app/models/kithe/asset/derivative_creator.rb', line 3

def shrine_attacher
  @shrine_attacher
end

#source_ioObject (readonly)

Returns the value of attribute source_io.



3
4
5
# File 'app/models/kithe/asset/derivative_creator.rb', line 3

def source_io
  @source_io
end

Instance Method Details

#callObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/kithe/asset/derivative_creator.rb', line 38

def call
  return unless shrine_attacher.file.present? # if no file, can't create derivatives

  definitions_to_create = applicable_definitions

  if lazy
    existing_derivative_keys = shrine_attacher.derivatives.keys
    definitions_to_create.reject! do |defn|
      existing_derivative_keys.include?(defn.key)
    end
  end

  return {} unless definitions_to_create.present?

  derivatives = {}

  # Make sure we have this as a local file, because many processors
  # require it, for instance for shelling out to command line.
  # This might trigger a download, but note we are only doing it if we have
  # `definitions_to_create`, otherwise we already returned.
  shrine_attacher.shrine_class.with_file(source_io) do |source_io_as_file|
    definitions_to_create.each do |defn|
      deriv_bytestream = defn.call(original_file: source_io_as_file, attacher: shrine_attacher)

      if deriv_bytestream
        derivatives[defn.key] =  deriv_bytestream
      end

      # may not need this but it doesn't hurt...
      source_io.rewind
    end
  end

  derivatives
end