Class: Dor::ThumbnailService

Inherits:
Object
  • Object
show all
Defined in:
lib/dor/services/thumbnail_service.rb

Overview

Responsible for finding a path to a thumbnail based on the contentMetadata of an object

Constant Summary collapse

MIME_TYPE_FINDER =

allow the mimetype attribute to be lower or camelcase when searching to make it more robust

"@mimetype='image/jp2' or @mimeType='image/jp2'"
THUMB_XPATH_FINDERS =

these are the finders we will use to search for a thumb resource in contentMetadata, they will be searched in the order provided, stopping when one is reached

[
  # first find a file of mimetype jp2 explicitly marked as a thumb in the resource type and with a thumb=yes attribute
  { image_type: 'local', finder: "/contentMetadata/resource[@type='thumb' and @thumb='yes']/file[#{MIME_TYPE_FINDER}]" },
  # same thing for external files
  { image_type: 'external', finder: "/contentMetadata/resource[@type='thumb' and @thumb='yes']/externalFile[#{MIME_TYPE_FINDER}]" },
  # next find any image or page resource types with the thumb=yes attribute of mimetype jp2
  { image_type: 'local', finder: "/contentMetadata/resource[(@type='page' or @type='image') and @thumb='yes']/file[#{MIME_TYPE_FINDER}]" },
  # same thing for external file
  { image_type: 'external', finder: "/contentMetadata/resource[(@type='page' or @type='image') and @thumb='yes']/externalFile[#{MIME_TYPE_FINDER}]" },
  # next find a file of mimetype jp2 and resource type=thumb but not marked with the thumb directive
  { image_type: 'local', finder: "/contentMetadata/resource[@type='thumb']/file[#{MIME_TYPE_FINDER}]" },
  # same thing for external file
  { image_type: 'external', finder: "/contentMetadata/resource[@type='thumb']/externalFile[#{MIME_TYPE_FINDER}]" },
  # finally find the first page or image resource of mimetype jp2
  { image_type: 'local', finder: "/contentMetadata/resource[@type='page' or @type='image']/file[#{MIME_TYPE_FINDER}]" },
  # same thing for external file
  { image_type: 'external', finder: "/contentMetadata/resource[@type='page' or @type='image']/externalFile[#{MIME_TYPE_FINDER}]" }
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ ThumbnailService

Returns a new instance of ThumbnailService.



30
31
32
# File 'lib/dor/services/thumbnail_service.rb', line 30

def initialize(object)
  @object = object
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



34
35
36
# File 'lib/dor/services/thumbnail_service.rb', line 34

def object
  @object
end

Instance Method Details

#thumbString

Returns the computed thumb filename, with the druid prefix and a slash in front of it, e.g. oo000oo0001/filenamewith space.jp2.

Returns:

  • (String)

    the computed thumb filename, with the druid prefix and a slash in front of it, e.g. oo000oo0001/filenamewith space.jp2



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dor/services/thumbnail_service.rb', line 37

def thumb
  return unless object.respond_to?(:contentMetadata) && object..present?

  cm = object..ng_xml
  thumb_image = nil

  THUMB_XPATH_FINDERS.each do |search_path|
    thumb_files = cm.xpath(search_path[:finder]) # look for a thumb
    next if thumb_files.empty?

    # if we find one, return the filename based on whether it is a local file or external file
    thumb_image = if search_path[:image_type] == 'local'
                    "#{object.remove_druid_prefix}/#{thumb_files[0]['id']}"
                  else
                    "#{object.remove_druid_prefix(thumb_files[0]['objectId'])}/#{thumb_files[0]['fileId']}"
                  end
    break # break out of the loop so we stop searching
  end

  thumb_image
end