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, asset, only: nil, except: nil, lazy: false, mark_created: :not_set) ⇒ 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

  • asset

    an Asset instance

  • 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. With lazy:false, the asset derivatives association will be consulted, so should be eager-loaded if you are going to be calling on multiple assets.

  • mark_created (Boolean) (defaults to: :not_set)

    if true will set shrine metadata indicating we’ve done derivative creation phase, so Asset#derivatives_created? will return true. Defaults to nil, meaning true if and only if ‘only` is nil – mark created if creating default derivatives.



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

def initialize(definitions, asset, only:nil, except:nil, lazy: false, mark_created: :not_set)
  @definitions = definitions
  @asset = asset
  @only = only && Array(only)
  @except = except && Array(except)
  @lazy = !!lazy
  @mark_created = mark_created.nil? ? (only.nil? && except.nil?) : !! mark_created
end

Instance Attribute Details

#assetObject (readonly)

Returns the value of attribute asset.



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

def asset
  @asset
end

#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

#mark_createdObject (readonly)

Returns the value of attribute mark_created.



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

def mark_created
  @mark_created
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

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
# File 'app/models/kithe/asset/derivative_creator.rb', line 38

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

  definitions_to_create = applicable_definitions
  if lazy
    existing_derivative_keys = asset.derivatives.collect(&:key).collect(&:to_s)
    definitions_to_create.reject! do |defn|
      existing_derivative_keys.include?(defn.key.to_s)
    end
  end

  return unless definitions_to_create.present?

  # Note, MAY make a superfluous copy and/or download of original file, ongoing
  # discussion https://github.com/shrinerb/shrine/pull/329#issuecomment-443615868
  # https://github.com/shrinerb/shrine/pull/332
  Shrine.with_file(asset.file) do |original_file|
    definitions_to_create.each do |defn|
      deriv_bytestream = defn.call(original_file: original_file, record: asset)

      if deriv_bytestream
        asset.update_derivative(defn.key, deriv_bytestream, storage_key: defn.storage_key)
        cleanup_returned_io(deriv_bytestream)
      end

      original_file.rewind
    end
    mark_derivatives_created! if mark_created
  end
end