Sha256: de6ab79e6585890d774118a51c23db2c9d2859e39dfd831559bb47182fe1c331

Contents?: true

Size: 1.9 KB

Versions: 55

Compression:

Stored size: 1.9 KB

Contents

module Hyrax
  class DerivativePath
    attr_reader :id, :destination_name

    class << self
      # Path on file system where derivative file is stored
      # @param [ActiveFedora::Base or String] object either the AF object or its id
      # @param [String] destination_name
      def derivative_path_for_reference(object, destination_name)
        new(object, destination_name).derivative_path
      end

      # @param [ActiveFedora::Base or String] object either the AF object or its id
      # @return [Array<String>] Array of paths to derivatives for this object.
      def derivatives_for_reference(object)
        new(object).all_paths
      end
    end

    # @param [ActiveFedora::Base, String] object either the AF object or its id
    # @param [String] destination_name
    def initialize(object, destination_name = nil)
      @id = object.is_a?(String) ? object : object.id
      @destination_name = destination_name.gsub(/^original_file_/, '') if destination_name
    end

    def derivative_path
      "#{path_prefix}-#{file_name}"
    end

    def all_paths
      Dir.glob(root_path.join("*")).select do |path|
        path.start_with?(path_prefix.to_s)
      end
    end

    private

      # @return [String] Returns the root path where derivatives will be generated into.
      def root_path
        Pathname.new(derivative_path).dirname
      end

      # @return <Pathname> Full prefix of the path for object.
      def path_prefix
        Pathname.new(Hyrax.config.derivatives_path).join(pair_path)
      end

      def pair_path
        id.split('').each_slice(2).map(&:join).join('/')
      end

      def file_name
        return unless destination_name
        destination_name + extension
      end

      def extension
        case destination_name
        when 'thumbnail'
          ".#{MIME::Types.type_for('jpg').first.extensions.first}"
        else
          ".#{destination_name}"
        end
      end
  end
end

Version data entries

55 entries across 55 versions & 1 rubygems

Version Path
hyrax-2.9.6 app/services/hyrax/derivative_path.rb
hyrax-2.9.5 app/services/hyrax/derivative_path.rb
hyrax-2.9.4 app/services/hyrax/derivative_path.rb
hyrax-2.9.3 app/services/hyrax/derivative_path.rb
hyrax-2.9.2 app/services/hyrax/derivative_path.rb
hyrax-2.9.1 app/services/hyrax/derivative_path.rb
hyrax-2.9.0 app/services/hyrax/derivative_path.rb
hyrax-2.8.0 app/services/hyrax/derivative_path.rb
hyrax-2.7.2 app/services/hyrax/derivative_path.rb
hyrax-2.7.1 app/services/hyrax/derivative_path.rb
hyrax-2.7.0 app/services/hyrax/derivative_path.rb
hyrax-2.6.0 app/services/hyrax/derivative_path.rb
hyrax-2.5.1 app/services/hyrax/derivative_path.rb
hyrax-2.5.0 app/services/hyrax/derivative_path.rb
hyrax-3.0.0.pre.beta2 app/services/hyrax/derivative_path.rb
hyrax-2.4.1 app/services/hyrax/derivative_path.rb
hyrax-3.0.0.pre.beta1 app/services/hyrax/derivative_path.rb
hyrax-2.4.0 app/services/hyrax/derivative_path.rb
hyrax-2.3.3 app/services/hyrax/derivative_path.rb
hyrax-2.3.2 app/services/hyrax/derivative_path.rb