Module: Bulkrax::FileFactory

Extended by:
ActiveSupport::Concern
Included in:
ObjectFactory
Defined in:
app/models/concerns/bulkrax/file_factory.rb

Instance Method Summary collapse

Instance Method Details

#destroy_existing_filesObject

Called if #replace_files is true Destroy all file_sets for this object Reload the object to ensure the remaining methods have the most up to date object



88
89
90
91
92
93
94
95
# File 'app/models/concerns/bulkrax/file_factory.rb', line 88

def destroy_existing_files
  return unless object.present? && object.file_sets.present?
  object.file_sets.each do |fs|
    Hyrax::Actors::FileSetActor.new(fs, @user).destroy
  end
  @object = object.reload
  log_deleted_fs(object)
end

#file_attributes(update_files = false) ⇒ Object



19
20
21
22
23
24
25
26
# File 'app/models/concerns/bulkrax/file_factory.rb', line 19

def file_attributes(update_files = false)
  @update_files = update_files
  hash = {}
  return hash if klass == Collection
  hash[:uploaded_files] = upload_ids if attributes[:file].present?
  hash[:remote_files] = new_remote_files if new_remote_files.present?
  hash
end

#file_pathsObject



71
72
73
# File 'app/models/concerns/bulkrax/file_factory.rb', line 71

def file_paths
  @file_paths ||= Array.wrap(attributes[:file])&.select { |file| File.exist?(file) }
end

#import_file(path) ⇒ Object



126
127
128
129
130
131
# File 'app/models/concerns/bulkrax/file_factory.rb', line 126

def import_file(path)
  u = Hyrax::UploadedFile.new
  u.user_id = @user.id
  u.file = CarrierWave::SanitizedFile.new(path)
  update_filesets(u)
end

#import_filesObject



120
121
122
123
124
# File 'app/models/concerns/bulkrax/file_factory.rb', line 120

def import_files
  paths = file_paths.map { |path| import_file(path) }.compact
  set_removed_filesets if local_file_sets.present?
  paths
end

#import_files_filenamesObject

Retrieve the filenames for the files to be imported



81
82
83
# File 'app/models/concerns/bulkrax/file_factory.rb', line 81

def import_files_filenames
  file_paths.map { |f| f.split('/').last }
end

#local_file_setsObject



111
112
113
# File 'app/models/concerns/bulkrax/file_factory.rb', line 111

def local_file_sets
  @local_file_sets ||= ordered_file_sets
end

#new_remote_filesObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/concerns/bulkrax/file_factory.rb', line 47

def new_remote_files
  @new_remote_files ||= if object.is_a? FileSet
                          parsed_remote_files.select do |file|
                            # is the url valid?
                            is_valid = file[:url]&.match(URI::ABS_URI)
                            # does the file already exist
                            is_existing = object.import_url && object.import_url == file[:url]
                            is_valid && !is_existing
                          end
                        elsif object.present? && object.file_sets.present?
                          parsed_remote_files.select do |file|
                            # is the url valid?
                            is_valid = file[:url]&.match(URI::ABS_URI)
                            # does the file already exist
                            is_existing = object.file_sets.detect { |f| f.import_url && f.import_url == file[:url] }
                            is_valid && !is_existing
                          end
                        else
                          parsed_remote_files.select do |file|
                            file[:url]&.match(URI::ABS_URI)
                          end
                        end
end

#ordered_file_setsObject



115
116
117
118
# File 'app/models/concerns/bulkrax/file_factory.rb', line 115

def ordered_file_sets
  # OVERRIDE Hyrda-works 1.2.0 - this method was deprecated in v1.0
  object&.ordered_members.to_a.select(&:file_set?)
end

#parsed_remote_filesObject

Its possible to get just an array of strings here, so we need to make sure they are all hashes



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/concerns/bulkrax/file_factory.rb', line 29

def parsed_remote_files
  return @parsed_remote_files if @parsed_remote_files.present?
  @parsed_remote_files = attributes[:remote_files] || []
  @parsed_remote_files = @parsed_remote_files.map do |file_value|
    if file_value.is_a?(Hash)
      file_value
    elsif file_value.is_a?(String)
      name = Bulkrax::Importer.safe_uri_filename(file_value)
      { url: file_value, file_name: name }
    else
      Rails.logger.error("skipped remote file #{file_value} because we do not recognize the type")
      nil
    end
  end
  @parsed_remote_files.delete(nil)
  @parsed_remote_files
end

#set_removed_filesetsObject



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/models/concerns/bulkrax/file_factory.rb', line 97

def set_removed_filesets
  local_file_sets.each do |fileset|
    fileset.files.first.create_version
    opts = {}
    opts[:path] = fileset.files.first.id.split('/', 2).last
    opts[:original_name] = 'removed.png'
    opts[:mime_type] = 'image/png'

    fileset.add_file(File.open(Bulkrax.removed_image_path), opts)
    fileset.save
    ::CreateDerivativesJob.set(wait: 1.minute).perform_later(fileset, fileset.files.first.id)
  end
end

#update_filesets(current_file) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'app/models/concerns/bulkrax/file_factory.rb', line 133

def update_filesets(current_file)
  if @update_files && local_file_sets.present?
    fileset = local_file_sets.shift
    return nil if fileset.files.first.checksum.value == Digest::SHA1.file(current_file.file.path).to_s

    fileset.files.first.create_version
    opts = {}
    opts[:path] = fileset.files.first.id.split('/', 2).last
    opts[:original_name] = current_file.file.file.original_filename
    opts[:mime_type] = current_file.file.content_type

    fileset.add_file(File.open(current_file.file.to_s), opts)
    fileset.save
    ::CreateDerivativesJob.set(wait: 1.minute).perform_later(fileset, fileset.files.first.id)
    nil
  else
    current_file.save
    current_file.id
  end
end

#upload_idsObject

Find existing files or upload new files. This assumes a Work will have unique file titles;

and that those file titles will not have changed

could filter by URIs instead (slower). When an uploaded_file already exists we do not want to pass its id in file_attributes otherwise it gets reuploaded by work_actor. support multiple files; ensure attributes is an Array



13
14
15
16
17
# File 'app/models/concerns/bulkrax/file_factory.rb', line 13

def upload_ids
  return [] if klass == Collection
  attributes[:file] = file_paths
  import_files
end

#work_files_filenamesObject

Retrieve the orginal filenames for the files to be imported



76
77
78
# File 'app/models/concerns/bulkrax/file_factory.rb', line 76

def work_files_filenames
  object.file_sets.map { |fn| fn.original_file.file_name.to_a }.flatten if object.present? && object.file_sets.present?
end