Module: TaliaCore::DataTypes::DataLoader::ClassMethods

Included in:
FileRecord
Defined in:
lib/talia_core/data_types/data_loader.rb

Instance Method Summary collapse

Instance Method Details

#create_from_url(uri, options = {}) ⇒ Object

Load the data from the given URL. If the mime_type option is given, the handler will always use the parameter for the MIME type (which can be a Mime::Type object or a string like ‘text/html’, or a mime type symbol).

Attention: This method will return an Array of data objects. This is for those cases, where a single data file will be processed into multiple objects (e.g. IIP data).

If the mime type is not given, the method will attempt to automatically determine the type, using the file extension or the response code.

The :http_credentials option may be used to pass login information for http like this:

http_credentials = { :http_basic_authentication => [, password] }

See the openuri documentation for more.

You may pass the :location parameter to identify the “location” value for the new data record. In general, this is not neccessary. If the location is given, the system will always attempt to determine the mime type through the location parameter, unless an explicit mime type is given.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/talia_core/data_types/data_loader.rb', line 28

def create_from_url(uri, options = {})
  options.to_options!
  options.assert_valid_keys(:mime_type, :location, :http_credentials)
  
  mime_type = options[:mime_type] 
  location = options[:location]
  # If a Mime type is given, use that.
  if(mime_type)
    mime_type = Mime::Type.lookup(mime_type) if(mime_type.is_a?(String))
  end

  data_records = []

  # Remove file:// from URIs to allow standard file URIs
  uri = file_url(uri)
  
  # We have diffent code paths for local and remote files. This is mainly because
  # the system will try to not open local files at all and just copy them around -
  # which will greatly speed up the operation.
  is_file = File.exist?(uri)

  location ||= File.basename(uri) if(is_file)
  # If we have a "standard" uri, we cut off at the last slash (the
  # File.basename would use the system file separator)
  location ||= uri.rindex('/') ? uri[(uri.rindex('/') + 1)..-1] : uri
  
  if(is_file)
    mime_type ||= mime_by_location(location)
    open_and_create(mime_type, location, uri, true)
  else
    open_from_url(uri, options[:http_credentials]) do |io|
      mime_type ||= Mime::Type.lookup(io.content_type)
      # Just in case we didn't get any content type
      mime_type ||= mime_by_location(location)
      open_and_create(mime_type, location, io, false)
    end
  end

end