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



79
80
81
82
83
84
85
86
# File 'app/models/concerns/bulkrax/file_factory.rb', line 79

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



62
63
64
# File 'app/models/concerns/bulkrax/file_factory.rb', line 62

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

#import_file(path) ⇒ Object



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

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



106
107
108
109
110
# File 'app/models/concerns/bulkrax/file_factory.rb', line 106

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



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

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

#local_file_setsObject



102
103
104
# File 'app/models/concerns/bulkrax/file_factory.rb', line 102

def local_file_sets
  @local_file_sets ||= object&.ordered_file_sets
end

#new_remote_filesObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/concerns/bulkrax/file_factory.rb', line 46

def new_remote_files
  @new_remote_files ||= if 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

#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
# 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)
      { url: file_value }
    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



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

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



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'app/models/concerns/bulkrax/file_factory.rb', line 119

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



67
68
69
# File 'app/models/concerns/bulkrax/file_factory.rb', line 67

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