Skip to content

Commit

Permalink
🐛 Fix #importer_unzip_path logic
Browse files Browse the repository at this point in the history
We discovered a bug with the previous updates to
the `#importer_unzip_path` method where when the method is being called
to create a path, it returns `nil` because the path does not exist and
the whole reason why we are calling the method is to create the path.

We add a `mkdir` argument to differentiate between when we're looking up
the path vs wanting to create it.
  • Loading branch information
kirkkwang committed Jan 30, 2025
1 parent 70d4b8c commit c6e7cd4
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions app/models/bulkrax/importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,9 @@ def import_metadata_format
# end

# If the import data is zipped, unzip it to this path
def importer_unzip_path
def importer_unzip_path(mkdir: false)
@importer_unzip_path ||= File.join(parser.base_path, "import_#{path_string}")
return @importer_unzip_path if Dir.exist?(@importer_unzip_path)
return @importer_unzip_path if Dir.exist?(@importer_unzip_path) || mkdir == true

# turns "tmp/imports/tenant/import_1_20250122035229_1" to "tmp/imports/tenant/import_1_20250122035229"
base_importer_unzip_path = @importer_unzip_path.split('_')[0...-1].join('_')
Expand Down
4 changes: 2 additions & 2 deletions app/parsers/bulkrax/application_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -432,15 +432,15 @@ def unzip(file_to_unzip)

Zip::File.open(file_to_unzip) do |zip_file|
zip_file.each do |entry|
entry_path = File.join(importer_unzip_path, entry.name)
entry_path = File.join(importer_unzip_path(mkdir: true), entry.name)
FileUtils.mkdir_p(File.dirname(entry_path))
zip_file.extract(entry, entry_path) unless File.exist?(entry_path)
end
end
end

def untar(file_to_untar)
Dir.mkdir(importer_unzip_path) unless File.directory?(importer_unzip_path)
Dir.mkdir(importer_unzip_path(mkdir: true)) unless File.directory?(importer_unzip_path)
command = "tar -xzf #{Shellwords.escape(file_to_untar)} -C #{Shellwords.escape(importer_unzip_path)}"
result = system(command)
raise "Failed to extract #{file_to_untar}" unless result
Expand Down

0 comments on commit c6e7cd4

Please sign in to comment.