Class: Riiif::Image

Inherits:
Object
  • Object
show all
Defined in:
app/models/riiif/image.rb

Constant Summary collapse

OUTPUT_FORMATS =
%w(jpg png).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, file = nil) ⇒ Image

Returns a new instance of Image.

Parameters:

  • id (String)

    The identifier of the file to be looked up.

  • file (Riiif::File) (defaults to: nil)

    Optional: The Riiif::File to use instead of looking one up.



43
44
45
46
# File 'app/models/riiif/image.rb', line 43

def initialize(id, file = nil)
  @id = id
  @image = file if file.present?
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



39
40
41
# File 'app/models/riiif/image.rb', line 39

def id
  @id
end

Class Method Details

.cache_key(id, options) ⇒ Object



73
74
75
76
77
78
# File 'app/models/riiif/image.rb', line 73

def cache_key(id, options)
  str = options.to_h.merge(id: id).delete_if { |_, v| v.nil? }.to_s
  # Use a MD5 digest to ensure the keys aren't too long, and a prefix
  # to avoid collisions with other components in shared cache.
  'riiif:' + Digest::MD5.hexdigest(str)
end

.expires_inObject



69
70
71
# File 'app/models/riiif/image.rb', line 69

def expires_in
  Riiif::Engine.config.cache_duration_in_days.days
end

Instance Method Details

#imageObject



48
49
50
# File 'app/models/riiif/image.rb', line 48

def image
  @image ||= file_resolver.find(id)
end

#infoObject



61
62
63
64
65
66
# File 'app/models/riiif/image.rb', line 61

def info
  @info ||= begin
              result = info_service.call(id, image)
              ImageInformation.new(result[:width], result[:height])
            end
end

#render(args) ⇒ Object

Parameters:

  • args (ActiveSupport::HashWithIndifferentAccess)


54
55
56
57
58
59
# File 'app/models/riiif/image.rb', line 54

def render(args)
  options = decode_options!(args)
  cache.fetch(Image.cache_key(id, options), compress: true, expires_in: Image.expires_in) do
    image.extract(options)
  end
end