Module: BulkOps::InterpretFilesBehavior

Extended by:
ActiveSupport::Concern
Included in:
Parser
Defined in:
lib/concerns/interpret_files_behavior.rb

Instance Method Summary collapse

Instance Method Details

#interpret_file_fieldsObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
# File 'lib/concerns/interpret_files_behavior.rb', line 5

def interpret_file_fields
  # This method handles file additions and deletions from the spreadsheet
  # if additional files need to be deleted because the update is set to replace
  # some or all existing files, those replacement-related deletions are handled
  # by the BulkOps::Operation.
  #

  @raw_row.each do |field, value|
    next if value.blank?  or field.blank?
    field = field.to_s
    #If our CSV interpreter is feeding us the headers as a line, ignore it.
    next if field == value

    # Check if this is a file field, and whether we are removing or adding a file
    next unless (action = BulkOps::Verification.is_file_field?(field))
    
    # Move on if this field is the name of another property (e.g. masterFilename)
    next if find_field_name(field)
    
    # Check if we are removing a file
    if action == "remove"
      get_removed_filesets(value).each { |fileset_id| delete_file_set(file_set_id) } 
    else
      # Add a file
      operation.get_file_paths(value).each do |filepath|
        begin
          uploaded_file = Hyrax::UploadedFile.create(file:  File.open(filepath), user: operation.user)
          (@metadata[:uploaded_files] ||= []) << uploaded_file.id unless uploaded_file.id.nil?
        rescue Exception => e  
          report_error(:upload_error,
                       message: "Error opening file: #{ filepath } -- #{e}",
                       file: File.join(BulkOps::INGEST_MEDIA_PATH,filename),
                       row_number: row_number)
        end
      end
    end

    # Check if any of the upcoming rows are child filesets
    i = 1
    while self.class.is_file_set?(@metadata,row_number+i)
      child_row.each do |field,value|
        next if value.blank?
        title = value if ["title","label"].include?(field.downcase.strip)
        if BulkOps::Verification.is_file_field?(field)
          operation.get_file_paths(value).each do |filepath|
            uploaded_file = Hyrax::UploadedFile.create(file:  File.open(filepath), user: operation.user)
          end
        end
      end
      i+=1
    end

  end
end