From 260dbf59fa746360adf2b349783761a5b3bb6f75 Mon Sep 17 00:00:00 2001 From: Kirk Wang Date: Fri, 31 Jan 2025 08:41:34 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20`#importer=5Funzip=5Fpath`?= =?UTF-8?q?=20logic=20(#1014)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- app/models/bulkrax/importer.rb | 4 ++-- app/parsers/bulkrax/application_parser.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/models/bulkrax/importer.rb b/app/models/bulkrax/importer.rb index 166f375e..be492514 100644 --- a/app/models/bulkrax/importer.rb +++ b/app/models/bulkrax/importer.rb @@ -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('_') diff --git a/app/parsers/bulkrax/application_parser.rb b/app/parsers/bulkrax/application_parser.rb index 161a6740..8e27b2d0 100644 --- a/app/parsers/bulkrax/application_parser.rb +++ b/app/parsers/bulkrax/application_parser.rb @@ -432,7 +432,7 @@ 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 @@ -440,7 +440,7 @@ def unzip(file_to_unzip) 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