Sha256: bf995cdcfafa1485dceae7c50032623ae88b9e5e13b529a42a33ddb07e31c601

Contents?: true

Size: 1.83 KB

Versions: 46

Compression:

Stored size: 1.83 KB

Contents

module Hyrax
  class ThumbnailPathService
    class << self
      # @param [Work, FileSet] 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)
          thumbnail_path(thumb)
        else
          default_image
        end
      end

      private

        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

        # @return the network path to the thumbnail
        # @param [FileSet] thumbnail the object that is the thumbnail
        def thumbnail_path(thumbnail)
          Hyrax::Engine.routes.url_helpers.download_path(thumbnail.id,
                                                         file: 'thumbnail')
        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
        # @param [FileSet] thumb - the object that is the thumbnail
        def thumbnail?(thumb)
          File.exist?(thumbnail_filepath(thumb))
        end

        # @param [FileSet] thumb - the object that is the thumbnail
        def thumbnail_filepath(thumb)
          Hyrax::DerivativePath.derivative_path_for_reference(thumb, 'thumbnail')
        end
    end
  end
end

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
hyrax-2.3.1 app/services/hyrax/thumbnail_path_service.rb
hyrax-2.3.0 app/services/hyrax/thumbnail_path_service.rb
hyrax-2.0.3 app/services/hyrax/thumbnail_path_service.rb
hyrax-2.2.4 app/services/hyrax/thumbnail_path_service.rb
hyrax-2.2.3 app/services/hyrax/thumbnail_path_service.rb
hyrax-2.2.2 app/services/hyrax/thumbnail_path_service.rb
hyrax-2.2.1 app/services/hyrax/thumbnail_path_service.rb
hyrax-2.2.0 app/services/hyrax/thumbnail_path_service.rb
hyrax-2.1.0 app/services/hyrax/thumbnail_path_service.rb
hyrax-2.1.0.rc4 app/services/hyrax/thumbnail_path_service.rb
hyrax-2.1.0.rc3 app/services/hyrax/thumbnail_path_service.rb
hyrax-2.1.0.rc2 app/services/hyrax/thumbnail_path_service.rb
hyrax-2.1.0.rc1 app/services/hyrax/thumbnail_path_service.rb
hyrax-2.1.0.beta2 app/services/hyrax/thumbnail_path_service.rb
hyrax-2.0.2 app/services/hyrax/thumbnail_path_service.rb
hyrax-2.1.0.beta1 app/services/hyrax/thumbnail_path_service.rb
hyrax-2.0.1 app/services/hyrax/thumbnail_path_service.rb
hyrax-2.0.0 app/services/hyrax/thumbnail_path_service.rb
hyrax-2.0.0.rc3 app/services/hyrax/thumbnail_path_service.rb
hyrax-2.0.0.rc2 app/services/hyrax/thumbnail_path_service.rb