module CarrierWave::Blitline

Extend the behaviour of CarrierWave to support Blitline services

Constants

BLITLINE_VERSION

Blitline API version

Function

A Struct class for storing name and params for each function parameter.

See also: ImageVersionFunctionPresenter
RIP_VERSION_NAMES_AT_START

Does the version name come at the start (carrierwave default) or at the end of the filename

UNIQUE_IDENTIFIER_TEMPLATE
VERSION

Public Instance Methods

file_name_for_version(version) click to toggle source
# File lib/carrierwave/blitline.rb, line 123
def file_name_for_version(version)
  file_name, file_type  = filename.split('.')
  name_components       = [version.name, file_name].compact
  name_components.reverse! unless RIP_VERSION_NAMES_AT_START
  file_namewith_version = name_components.join("_") + ".#{file_type}"
  File.join(store_dir, file_namewith_version).to_s
end
filename() click to toggle source
# File lib/carrierwave/blitline.rb, line 111
def filename
  "#{model.class.to_s.underscore}.#{file.extension}" if file
end
functions() click to toggle source

Returns a Hash for each function included in the Blitline API post

# File lib/carrierwave/blitline.rb, line 89
def functions
  blitline_image_versions.map do |version|
    ImageVersionFunctionPresenter.new(version, self).to_hash
  end
end
job_hash() click to toggle source

Returns a Hash of params posted off to Blitline API

# File lib/carrierwave/blitline.rb, line 79
def job_hash
  {
    "application_id": CarrierWave::Blitline.blitline_application_id,
    "src": url,
    "v": BLITLINE_VERSION,
    "functions": functions
  }.with_indifferent_access
end
optimize!() click to toggle source

sends a request to Blitline to re-process themain image and all versions

# File lib/carrierwave/blitline.rb, line 96
def optimize!
  rip_process_images(true) if process_via_blitline?
end
params_for_function(function_name, *args) click to toggle source
# File lib/carrierwave/blitline.rb, line 131
def params_for_function(function_name, *args)
  send("params_for_#{function_name}", *args)
end
params_for_no_op(*_args) click to toggle source
# File lib/carrierwave/blitline.rb, line 135
def params_for_no_op(*_args)
  {}
end
params_for_resize_to_fill(*args) click to toggle source
# File lib/carrierwave/blitline.rb, line 139
def params_for_resize_to_fill(*args)
  args.flatten!
  { width: args.first, height: args.last }
end
params_for_resize_to_fit(*args) click to toggle source
# File lib/carrierwave/blitline.rb, line 144
def params_for_resize_to_fit(*args)
  args.flatten!
  { width: args.first, height: args.last }
end
rip_can_begin_processing?() click to toggle source

Can we post the images to Blitline for processing?

CarrierWave creates virtual Uploaders for each version of an image. These
versions are processed before the original, so the only way to tell if the
versions are all complete is to check the classname for the current call
and if there is no '::' it is the original class.

Returns a boolean

# File lib/carrierwave/blitline.rb, line 107
def rip_can_begin_processing?
  process_via_blitline? && (!self.class.name.include? "::")
end
rip_process_images(_file) click to toggle source

Send a request to Blitline to optimize the original file and create any required versions.

 This is called by an after_store macro and because Carrier creates virtual
 instancies for each version would be called 4 times for an image with three
 versions.

 Because we only want to do this on completion we check all the versions
 have been called by testing it is OK to begin processing

 A hash is created (job_hash) with Blitline's required commands and sent using the
 Blitline gem.

file - not used within the method, but required for the callback to function
# File lib/carrierwave/blitline.rb, line 64
def rip_process_images(_file)
  return unless rip_can_begin_processing?
  Rails.logger.tagged("Blitline") { |l| l.debug(job_hash.to_json) }
  blitline_service.add_job_via_hash(job_hash)
  begin
    blitline_service.post_jobs
  rescue StandardError => e
    Rails.logger.tagged("Blitline") do |logger|
      logger.error format("ERROR: Blitline processing error for %<class>\n%<message>",
                          class: model.class.name, message: e.message)
    end
  end
end
unique_identifier() click to toggle source
# File lib/carrierwave/blitline.rb, line 115
def unique_identifier
  @unique_identifier ||= begin
    UNIQUE_IDENTIFIER_TEMPLATE % { app_name: Rails.application.class.name,
                                   rails_env: Rails.env,
                                   token: SecureRandom.base64(10) }
  end
end

Private Instance Methods

blitline_service() click to toggle source
# File lib/carrierwave/blitline.rb, line 153
def blitline_service
  @blitline_service ||= ::Blitline.new
end