Sha256: 1579a7a441a6569abb859777fc4a5a1aa651478420dc6f351ff56316ae9035d4

Contents?: true

Size: 1.13 KB

Versions: 3

Compression:

Stored size: 1.13 KB

Contents

require 'uri'

module NewspaperWorks
  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 whitelisted_path(path)
        Hyrax.config.whitelisted_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 whitelisted_path(path)
        # we cannot use path if it is not whitelisted for Hyrax ingest, we
        #   would prefer to fail early vs. later+silently
        raise SecurityError,
              "Path specified is not configured in Hyrax ingest whitelist: " \
              "#{path}"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
newspaper_works-1.0.1 lib/newspaper_works/data/path_helper.rb
newspaper_works-1.0.0 lib/newspaper_works/data/path_helper.rb
newspaper_works-0.1.0 lib/newspaper_works/data/path_helper.rb