Module: Aspera::UriReader

Defined in:
lib/aspera/uri_reader.rb

Overview

Read some content from some URI, support file: , http: and https: schemes

Class Method Summary collapse

Class Method Details

.read(uri_to_read) ⇒ Object

Read some content from some URI, support file: , http: and https: schemes



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/aspera/uri_reader.rb', line 18

def read(uri_to_read)
  uri = URI.parse(uri_to_read)
  case uri.scheme
  when 'http', 'https'
    return Rest.new(base_url: uri_to_read, redirect_max: 5).read(nil, headers: {'Accept' => '*/*'})
  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 SCHEME_FILE, NilClass
    local_file_path = uri.path
    raise Error, 'URL shall have a path, check syntax' if local_file_path.nil?
    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) ⇒ Object

Returns Path to file with content at URL.

Returns:

  • Path to file with content at URL



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/aspera/uri_reader.rb', line 40

def read_as_file(url)
  if url.start_with?(SCHEME_FILE_PFX1)
    # for file scheme, return directly the path
    # require specific file scheme: the path part is "relative", or absolute if there are 4 slash
    raise "use format: #{SCHEME_FILE_PFX2}<path>" unless url.start_with?(SCHEME_FILE_PFX2)
    return File.expand_path(url[SCHEME_FILE_PFX2.length..-1])
  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.new(base_url: url, redirect_max: 3).call(operation: 'GET', save_to_file: temp_file)
    return temp_file
  end
end