Class: Riiif::Image

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, passed_file = nil) ⇒ Image

Returns a new instance of Image.

Parameters:

  • id (String)

    The identifier of the file to be looked up.

  • file (Riiif::File)

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



27
28
29
30
# File 'app/models/riiif/image.rb', line 27

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

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



23
24
25
# File 'app/models/riiif/image.rb', line 23

def id
  @id
end

Class Method Details

.cache_key(id, options) ⇒ Object



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

def cache_key(id, options)
  str = options.to_h.merge(id: id)
               .delete_if { |_, v| v.nil? }
               .sort_by { |k, _v| k.to_s }
               .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



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

def expires_in
  if Riiif::Engine.config.respond_to?(:cache_duration_in_days)
    Deprecation.warn(self,
                     'Riiif::Engine.config.cache_duration_in_days is deprecated; '\
                     'use #cache_duration instead and pass a fully-qualified date (e.g., `3.days`)')
    Riiif::Engine.config.cache_duration_in_days.days
  else
    Riiif::Engine.config.cache_duration
  end
end

Instance Method Details

#fileObject



32
33
34
# File 'app/models/riiif/image.rb', line 32

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

#infoObject



48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/riiif/image.rb', line 48

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

#render(args) ⇒ String

Returns the image data.

Parameters:

  • args (ActiveSupport::HashWithIndifferentAccess)

Returns:

  • (String)

    the image data



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

def render(args)
  cache_opts = args.select { |a| %w(region size quality rotation format).include? a.to_s }
  key = Image.cache_key(id, cache_opts)

  cache.fetch(key, compress: true, expires_in: Image.expires_in) do
    file.extract(IIIF::Image::OptionDecoder.decode(args), info)
  end
end