Module: Bulkrax::FileFactory
- Extended by:
- ActiveSupport::Concern
- Included in:
- ObjectFactory
- Defined in:
- app/models/concerns/bulkrax/file_factory.rb
Instance Method Summary collapse
-
#destroy_existing_files ⇒ Object
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.
- #file_attributes(update_files = false) ⇒ Object
- #file_paths ⇒ Object
- #import_file(path) ⇒ Object
- #import_files ⇒ Object
-
#import_files_filenames ⇒ Object
Retrieve the filenames for the files to be imported.
- #local_file_sets ⇒ Object
- #new_remote_files ⇒ Object
- #ordered_file_sets ⇒ Object
-
#parsed_remote_files ⇒ Object
Its possible to get just an array of strings here, so we need to make sure they are all hashes.
- #set_removed_filesets ⇒ Object
- #update_filesets(current_file) ⇒ Object
-
#upload_ids ⇒ Object
Find existing files or upload new files.
-
#work_files_filenames ⇒ Object
Retrieve the orginal filenames for the files to be imported.
Instance Method Details
#destroy_existing_files ⇒ Object
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
82 83 84 85 86 87 88 89 |
# File 'app/models/concerns/bulkrax/file_factory.rb', line 82 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_paths ⇒ Object
65 66 67 |
# File 'app/models/concerns/bulkrax/file_factory.rb', line 65 def file_paths @file_paths ||= Array.wrap(attributes[:file])&.select { |file| File.exist?(file) } end |
#import_file(path) ⇒ Object
120 121 122 123 124 125 |
# File 'app/models/concerns/bulkrax/file_factory.rb', line 120 def import_file(path) u = Hyrax::UploadedFile.new u.user_id = @user.id u.file = CarrierWave::SanitizedFile.new(path) update_filesets(u) end |
#import_files ⇒ Object
114 115 116 117 118 |
# File 'app/models/concerns/bulkrax/file_factory.rb', line 114 def import_files paths = file_paths.map { |path| import_file(path) }.compact set_removed_filesets if local_file_sets.present? paths end |
#import_files_filenames ⇒ Object
Retrieve the filenames for the files to be imported
75 76 77 |
# File 'app/models/concerns/bulkrax/file_factory.rb', line 75 def import_files_filenames file_paths.map { |f| f.split('/').last } end |
#local_file_sets ⇒ Object
105 106 107 |
# File 'app/models/concerns/bulkrax/file_factory.rb', line 105 def local_file_sets @local_file_sets ||= ordered_file_sets end |
#new_remote_files ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'app/models/concerns/bulkrax/file_factory.rb', line 47 def new_remote_files return if object.is_a? FileSet @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 |
#ordered_file_sets ⇒ Object
109 110 111 112 |
# File 'app/models/concerns/bulkrax/file_factory.rb', line 109 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_files ⇒ Object
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_filesets ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'app/models/concerns/bulkrax/file_factory.rb', line 91 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
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'app/models/concerns/bulkrax/file_factory.rb', line 127 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_ids ⇒ Object
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_filenames ⇒ Object
Retrieve the orginal filenames for the files to be imported
70 71 72 |
# File 'app/models/concerns/bulkrax/file_factory.rb', line 70 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 |