Sha256: e6f14c245b657efbacd83d40a100d24bdfa92ab5038d725e8c6a8453e00ccb44

Contents?: true

Size: 1.15 KB

Versions: 9

Compression:

Stored size: 1.15 KB

Contents

require 'uri'

module IiifPrint
  module Data
    # Mixin for methods related to paths on filesystem
    module PathHelper
      def normalize_path(path)
        path = path.to_s
        isuri?(path) ? path : File.expand_path(path)
      end

      def isuri?(path)
        !path.scan(URI.regexp).empty?
      end

      def path_to_uri(path)
        isuri?(path) ? path : "file://#{path}"
      end

      def registered_ingest_path(path)
        IiifPrint.config.registered_ingest_dirs.any? do |dir|
          path.start_with?(dir) && path.length > dir.length
        end
      end

      def validate_path(path)
        # treat file URIs equivalent to local paths
        path = File.expand_path(path.sub(/^file:\/\//, ''))
        # make sure file exists
        raise IOError, "Not found: #{path}" unless File.exist?(path)
        return if registered_ingest_path(path)
        # we cannot use path if it is not in the registered list for Hyrax ingest, we
        #   would prefer to fail early vs. later+silently
        raise SecurityError,
          "Path specified is not configured in Hyrax ingest registered list: " \
          "#{path}"
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
iiif_print-3.0.4 lib/iiif_print/data/path_helper.rb
iiif_print-3.0.3 lib/iiif_print/data/path_helper.rb
iiif_print-3.0.2 lib/iiif_print/data/path_helper.rb
iiif_print-3.0.1 lib/iiif_print/data/path_helper.rb
iiif_print-3.0.0 lib/iiif_print/data/path_helper.rb
iiif_print-2.0.1 lib/iiif_print/data/path_helper.rb
iiif_print-2.0.0 lib/iiif_print/data/path_helper.rb
iiif_print-1.1.0 lib/iiif_print/data/path_helper.rb
iiif_print-1.0.0 lib/iiif_print/data/path_helper.rb