Sha256: a6165e2ca7b82c16d8d66d4a2031687747ee5544147b0666496245c41d05c5df

Contents?: true

Size: 1.89 KB

Versions: 20

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true
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.to_s
      @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

20 entries across 20 versions & 1 rubygems

Version Path
hyrax-3.6.0 app/services/hyrax/derivative_path.rb
hyrax-4.0.0 app/services/hyrax/derivative_path.rb
hyrax-4.0.0.rc3 app/services/hyrax/derivative_path.rb
hyrax-4.0.0.rc2 app/services/hyrax/derivative_path.rb
hyrax-4.0.0.rc1 app/services/hyrax/derivative_path.rb
hyrax-3.5.0 app/services/hyrax/derivative_path.rb
hyrax-4.0.0.beta2 app/services/hyrax/derivative_path.rb
hyrax-3.4.2 app/services/hyrax/derivative_path.rb
hyrax-4.0.0.beta1 app/services/hyrax/derivative_path.rb
hyrax-3.4.1 app/services/hyrax/derivative_path.rb
hyrax-3.4.0 app/services/hyrax/derivative_path.rb
hyrax-3.3.0 app/services/hyrax/derivative_path.rb
hyrax-3.2.0 app/services/hyrax/derivative_path.rb
hyrax-3.1.0 app/services/hyrax/derivative_path.rb
hyrax-3.0.2 app/services/hyrax/derivative_path.rb
hyrax-3.0.1 app/services/hyrax/derivative_path.rb
hyrax-3.0.0 app/services/hyrax/derivative_path.rb
hyrax-3.0.0.pre.rc4 app/services/hyrax/derivative_path.rb
hyrax-3.0.0.pre.rc3 app/services/hyrax/derivative_path.rb
hyrax-3.0.0.pre.rc2 app/services/hyrax/derivative_path.rb