Module: Aspera::UriReader

Defined in:
lib/aspera/uri_reader.rb

Overview

Read content from a URI; supported schemes: file:, http:, https:, data:

file: URL convention

Two equivalent forms are accepted:

file:///relative/path    -> relative path "relative/path"
file:////absolute/path   -> absolute path "/absolute/path"

A shorter form is also accepted (no authority component):

file:relative/path       -> relative path "relative/path"
file:/absolute/path      -> absolute path "/absolute/path"

The short form is consistent with RFC 8089 (file:/// = absolute, file: = relative). Both forms are handled identically by this module. The canonical form built by UriReader.file_url uses the file:/// prefix.

Class Method Summary collapse

Class Method Details

.file?(url) ⇒ Boolean

Returns true if url uses the file: scheme recognized by this module.

Returns:

  • (Boolean) —

    true if url uses the file: scheme recognized by this module



41
42
43
# File 'lib/aspera/uri_reader.rb', line 41

def file?(url)
  url.start_with?(SCHEME_FILE_PFX1)
end

.file_path(url) ⇒ String

Extract the file-system path from a file: URL. Accepts both the canonical file:/// form and the short file: form. Returns the literal path (relative or absolute) without working-directory expansion.

Parameters:

  • url (String) —

    a file: URL (canonical or short form)

Returns:

  • (String) —

    the literal path encoded in the URL



58
59
60
61
62
63
# File 'lib/aspera/uri_reader.rb', line 58

def file_path(url)
  Aspera.assert(file?(url), type: ParameterError) { "use format: #{file_url('<path>')}" }
  # Strip canonical prefix "file:///" first (covers relative and absolute canonical forms).
  # If absent, strip only the short "file:" prefix.
  return url.start_with?(SCHEME_FILE_PFX2) ? url.delete_prefix(SCHEME_FILE_PFX2) : url.delete_prefix(SCHEME_FILE_PFX1)
end

.file_url(path) ⇒ String

Build a file: URL from path. A relative path yields file:///path; an absolute path yields file:////path.

Parameters:

  • path (String) —

    relative or absolute file-system path

Returns:

  • (String) —

    corresponding file: URL



49
50
51
# File 'lib/aspera/uri_reader.rb', line 49

def file_url(path)
  return "#{SCHEME_FILE_PFX2}#{path}"
end

.read(uri_to_read, headers: {}) ⇒ String

Read content from a URI and return it as a String. Supported schemes: http, https, data, file, and bare paths (no scheme). For file: URLs the path is extracted via file_path to respect the module convention (see module-level documentation). Ruby's URI parser is not used for file: URLs because it interprets the three-slash prefix differently (it always produces an absolute path). Bare paths (no scheme) are passed to File.read directly; leading /~/, /./, /../ are expanded via File.expand_path after stripping the synthetic leading slash added by URI.

Parameters:

  • uri_to_read (String) —

    URI to read

  • headers (Hash{String => String}) (defaults to: {}) —

    Additional HTTP request headers (schemes http and https only)

Returns:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/aspera/uri_reader.rb', line 76

def read(uri_to_read, headers: {})
  # Handle file: URLs directly to honour the file:///relative vs file:////absolute convention.
  return File.read(file_path(uri_to_read)) if file?(uri_to_read)
  uri = URI.parse(uri_to_read)
  case uri.scheme
  when 'http', 'https'
    return Rest::Client.new(base_url: uri_to_read, redirect_max: 5).read(nil, headers: {'Accept' => '*/*'}.merge(headers))
  when 'data'
    , encoded_data = uri.opaque.split(',', 2)
    if .end_with?(';base64')
      Base64.decode64(encoded_data)
    else
      URI.decode_www_form_component(encoded_data)
    end
  when NilClass
    local_file_path = uri.path
    Aspera.assert(!local_file_path.nil?, type: ParameterError) { 'URL shall have a path, check syntax' }
    local_file_path = File.expand_path(local_file_path.gsub(%r{^/}, '')) if %r{^/(~|.|..)/}.match?(local_file_path)
    return File.read(local_file_path)
  else Aspera.error_unexpected_value(uri.scheme) { "scheme for [#{uri_to_read}]" }
  end
end

.read_as_file(url) ⇒ String

Return the local file-system path for the content at url, downloading to a temp file if needed. For file: URLs the path is extracted directly (no download). For data: and http(s): URLs the content is written to a temporary file and its path is returned.

Returns:

  • (String) —

    local path to a file containing the URL content



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/aspera/uri_reader.rb', line 149

def read_as_file(url)
  if url.start_with?(SCHEME_FILE_PFX1)
    # file: scheme: extract the literal path encoded in the URL (relative or absolute).
    return file_path(url)
  elsif url.start_with?('data:')
    # download to temp file
    # auto-delete on exit
    temp_file = TempFileManager.instance.new_file_path_global('uri_reader')
    File.write(temp_file, read(url), binmode: true)
    return temp_file
  else
    # download to temp file
    # auto-delete on exit
    temp_file = TempFileManager.instance.new_file_path_global(suffix: File.basename(url))
    Aspera::Rest::Client.new(base_url: url, redirect_max: 3).call(operation: 'GET', save_to: temp_file)
    return temp_file
  end
end

.read_fail_fast(url, **read_options) ⇒ String

Read URL with a short timeout and no retry (temporarily changes global HTTP parameters)

Parameters:

  • url (String) —

    URL to read

  • read_options (Hash) —

    Options of read (e.g. headers)

Returns:



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/aspera/uri_reader.rb', line 103

def read_fail_fast(url, **read_options)
  parameters = Rest::Parameters.instance
  session_cb = parameters.session_cb
  retry_max = parameters.retry_max
  parameters.retry_max = 0
  parameters.session_cb = lambda do |http_session|
    session_cb&.call(http_session)
    http_session.open_timeout = [http_session.open_timeout, FAIL_FAST_TIMEOUT].compact.min
    http_session.read_timeout = [http_session.read_timeout, FAIL_FAST_TIMEOUT].compact.min
  end
  read(url, **read_options)
ensure
  parameters.session_cb = session_cb
  parameters.retry_max = retry_max
end

.read_first(urls, what: 'content', **read_options) {|content| ... } ⇒ Object

Read the first URL that provides valid content. All but the last URL are read with a short timeout and no retry, so that fallback is quick. On failure of a URL (read error, or validation error), a warning is logged and the next URL is tried. The failure of the last URL is raised.

Parameters:

  • urls (Array<String>) —

    URLs, in order of preference

  • what (String) (defaults to: 'content') —

    description of the content, for messages

  • read_options (Hash) —

    Options of read (e.g. headers)

Yields:

  • (content) —

    Optional: validates and converts content, shall raise on invalid content

Yield Parameters:

  • content (String) —

    content read

Yield Returns:

  • (Object) —

    value returned by this method

Returns:

  • (Object) —

    result of block if given, else content



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/aspera/uri_reader.rb', line 130

def read_first(urls, what: 'content', **read_options)
  Aspera.assert(!urls.empty?) { "no URL for #{what}" }
  urls.each_with_index do |url, index|
    last = index.eql?(urls.length - 1)
    Log.log.debug { "Retrieving #{what} from #{url}" }
    begin
      content = last ? read(url, **read_options) : read_fail_fast(url, **read_options)
      return block_given? ? yield(content) : content
    rescue StandardError => e
      raise if last
      Log.log.warn { "Failed to retrieve #{what} from #{url}: #{e.message}, trying next" }
    end
  end
end