Skip to content

Commit 32083d9

Browse files
author
Kirk Wang
committed
🐛 Fix rerun for entries that came from zips
Previously, when rerunning an entry that came from a zip file, the rerun would fail because it would look for the CSV in an assumed location which is based on it's last importer ID. Since this was a rerun, it does not do an unzip into the assumed location so the directory does not exist. This commit will first check if the assumed location exists, and if not, it will look for the location of the last unizpped files and use that for the rerun. This does cause an interesting behavior where if the entry is a work with a file attached, it will add the file again resulting in duplicate files. I feel this is such an edge case though because typically if the entry is successful, the user will not rerun it. I added a hint text to the importer to let the user know this is a possibility. Ref: - notch8/palni_palci_knapsack#210
1 parent 0e8de61 commit 32083d9

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

app/parsers/bulkrax/csv_parser.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,10 @@ def path_to_files(**args)
360360
@path_to_files = File.join(
361361
zip? ? importer_unzip_path : File.dirname(import_file_path), 'files', filename
362362
)
363+
364+
return @path_to_files if File.exist?(@path_to_files)
365+
366+
File.join(real_importer_unzip_path, 'files', filename) if file? && zip?
363367
end
364368

365369
private
@@ -379,7 +383,7 @@ def unique_collection_identifier(collection_hash)
379383
# We expect a single CSV at the top level of the zip in the CSVParser
380384
# but we are willing to go look for it if need be
381385
def real_import_file_path
382-
return Dir["#{importer_unzip_path}/**/*.csv"].reject { |path| in_files_dir?(path) }.first if file? && zip?
386+
return Dir["#{real_importer_unzip_path}/**/*.csv"].reject { |path| in_files_dir?(path) }.first if file? && zip?
383387

384388
parser_fields['import_file_path']
385389
end
@@ -389,5 +393,18 @@ def real_import_file_path
389393
def in_files_dir?(path)
390394
File.dirname(path).ends_with?('files')
391395
end
396+
397+
# If we don't have an existing unzip path, we'll try and find it.
398+
# Just in case there are multiple paths, we sort by the number at the end of the path and get the last one
399+
def real_importer_unzip_path
400+
return importer_unzip_path if Dir.exist?(importer_unzip_path)
401+
402+
Dir.glob(base_importer_unzip_path + '*').sort_by { |path| path.split(base_importer_unzip_path).last[1..-1].to_i }.last
403+
end
404+
405+
def base_importer_unzip_path
406+
# turns "tmp/imports/tenant/import_1_20250122035229_1" to "tmp/imports/tenant/import_1_20250122035229"
407+
importer_unzip_path.split('_')[0...-1].join('_')
408+
end
392409
end
393410
end

app/views/bulkrax/importers/_edit_item_buttons.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<h5>Options for Updating an Entry</h5>
66
<hr />
77
<p>Rebuild metadata and files.</p>
8+
<p class="text-warning">Files may be duplicated if this option is used on a successful entry. Consider using <em>Remove and then Build</em> instead.</p>
89
<%= link_to 'Build', item_entry_path(item, e), method: :patch, class: 'btn btn-primary' %>
910
<hr />
1011
<p>Remove existing work and then recreate the works metadata and files.</p>

spec/fixtures/csv/files/moon.jpg

4.16 KB
Loading

0 commit comments

Comments
 (0)