Sha256: 977e0897f8b95a23f4d4be7a664a596b9cb7367ebcef800317b064a9af9f1c56

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 KB

Contents

module CurationConcerns
  class ThumbnailPathService
    class << self
      # @param [Work, FileSet] the object to get the thumbnail for
      # @return [String] a path to the thumbnail
      def call(object)
        return default_image unless object.thumbnail_id

        thumb = fetch_thumbnail(object)
        return unless thumb
        return call(thumb) unless thumb.is_a?(::FileSet)
        if thumb.audio?
          audio_image
        elsif thumbnail?(thumb)
          Rails.application.routes.url_helpers.download_path(object.thumbnail_id, file: 'thumbnail')
        else
          default_image
        end
      end

      def fetch_thumbnail(object)
        return object if object.thumbnail_id == object.id
        ::ActiveFedora::Base.find(object.thumbnail_id)
      rescue ActiveFedora::ObjectNotFoundError
        Rails.logger.error("Couldn't find thumbnail #{object.thumbnail_id} for #{object.id}")
        nil
      end

      def default_image
        ActionController::Base.helpers.image_path 'default.png'
      end

      def audio_image
        ActionController::Base.helpers.image_path 'audio.png'
      end

      # @return true if there a file on disk for this object, otherwise false
      def thumbnail?(thumb)
        File.exist?(thumbnail_filepath(thumb))
      end

      def thumbnail_filepath(thumb)
        CurationConcerns::DerivativePath.derivative_path_for_reference(thumb, 'thumbnail')
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
curation_concerns-1.0.0.beta8 app/services/curation_concerns/thumbnail_path_service.rb
curation_concerns-1.0.0.beta7 app/services/curation_concerns/thumbnail_path_service.rb