Module: Shrine::Plugins::StoreDimensions

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

Overview

Documentation can be found on shrinerb.com/docs/plugins/store_dimensions

Defined Under Namespace

Modules: ClassMethods, FileMethods, InstanceMethods Classes: DimensionsAnalyzer

Constant Summary collapse

LOG_SUBSCRIBER =
-> (event) do
  Shrine.logger.info "Image Dimensions (#{event.duration}ms) – #{{
    io:       event[:io].class,
    uploader: event[:uploader],
  }.inspect}"
end

Class Method Summary collapse

Class Method Details

.configure(uploader, log_subscriber: LOG_SUBSCRIBER, **opts) ⇒ Object


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/shrine/plugins/store_dimensions.rb', line 14

def self.configure(uploader, log_subscriber: LOG_SUBSCRIBER, **opts)
  uploader.opts[:store_dimensions] ||= { analyzer: :fastimage, on_error: :warn, auto_extraction: true }
  uploader.opts[:store_dimensions].merge!(opts)

  # resolve error strategy
  uploader.opts[:store_dimensions][:on_error] =
    case uploader.opts[:store_dimensions][:on_error]
    when :fail   then -> (error) { fail error }
    when :warn   then -> (error) { Shrine.warn "Error occurred when attempting to extract image dimensions: #{error.inspect}" }
    when :ignore then -> (error) { }
    else
      uploader.opts[:store_dimensions][:on_error]
    end

  # instrumentation plugin integration
  uploader.subscribe(:image_dimensions, &log_subscriber) if uploader.respond_to?(:subscribe)
end