Module: Ddr::Models::FileManagement

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/ddr/models/file_management.rb

Defined Under Namespace

Classes: FileToAdd

Instance Method Summary collapse

Instance Method Details

#add_external_datastream(dsid, opts = {}) ⇒ Object



66
67
68
69
70
71
# File 'lib/ddr/models/file_management.rb', line 66

def add_external_datastream dsid, opts={}
  create_datastream(Ddr::Datastreams::ExternalFileDatastream, dsid).tap do |ds|
    add_datastream(ds)
    self.class.build_datastream_accessor(dsid)
  end
end

#add_external_file(file, dsid, mime_type: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Normally this method should not be called directly. Call ‘add_file` with dsid for external datastream, or with `:external=>true` option if no spec for dsid.



49
50
51
52
53
54
55
56
# File 'lib/ddr/models/file_management.rb', line 49

def add_external_file(file, dsid, mime_type: nil)
  file_path = Ddr::Utils.file_path(file) # raises ArgumentError
  ds = datastreams[dsid] || add_external_datastream(dsid)
  unless ds.external?
    raise ArgumentError, "Cannot add external file to non-external datastream."
  end
  ds.add_file(file_path, mime_type: mime_type)
end

#add_file(file, dsid, mime_type: nil, external: false, original_filename: nil) ⇒ Object

Parameters:

  • file (String, File, ActionDispatch::Http::UploadedFile)

    The file, or path to file, to add.

  • dsid (String)

    The datastream ID to which file should be added.

  • mime_type (String) (defaults to: nil)

    Explicit mime type to set (otherwise discerned from file path or name).

  • external (Boolean) (defaults to: false)

    Add file to external datastream. Not required for external datastream classes or datastream instances having controlGroup ‘E’.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ddr/models/file_management.rb', line 22

def add_file(file, dsid, mime_type: nil, external: false, original_filename: nil)
  mime_type         ||= MediaType.call(file) # XXX Should we use original_filename, if present?
  source_path         = Ddr::Utils.file_path(file)
  original_filename ||= Ddr::Utils.file_name(file)
  file_to_add = FileToAdd.new(dsid, source_path, original_filename)
  cache.with(file_to_add: file_to_add) do
    run_callbacks(:add_file) do
      if external || ( datastreams.include?(dsid) && datastreams[dsid].external? )
        add_external_file(file, dsid, mime_type: mime_type)
      else
        add_file_datastream(file, dsid: dsid, mimeType: mime_type)
      end
    end
  end
end

#add_file_datastream(file, opts = {}) ⇒ Object



38
39
40
41
42
43
# File 'lib/ddr/models/file_management.rb', line 38

def add_file_datastream(file, opts={})
  if Ddr::Utils.file_path?(file)
    file = File.new(file, "rb")
  end
  super
end

#external_datastream_file_pathsObject



62
63
64
# File 'lib/ddr/models/file_management.rb', line 62

def external_datastream_file_paths
  external_datastreams.map(&:file_paths).flatten
end

#external_datastreamsObject



58
59
60
# File 'lib/ddr/models/file_management.rb', line 58

def external_datastreams
  datastreams.values.select { |ds| ds.external? }
end