diff --git a/BrainPortal/cbrain_plugins/cbrain-plugins-base/userfiles/file_collection/file_collection.rb b/BrainPortal/cbrain_plugins/cbrain-plugins-base/userfiles/file_collection/file_collection.rb index 45f22066f..c47fa2555 100755 --- a/BrainPortal/cbrain_plugins/cbrain-plugins-base/userfiles/file_collection/file_collection.rb +++ b/BrainPortal/cbrain_plugins/cbrain-plugins-base/userfiles/file_collection/file_collection.rb @@ -72,6 +72,25 @@ def extract_collection_from_archive_file(archive_file_name) self.remove_unwanted_files + # we flatten directory if archive file names coincide with only entry + if extra_nesting? # archive name/dir is same as its only 1st level entry, and is dir e.g. subx archive has only subx folder + basename = File.basename(directory) + self.addlog_context(self, "The only directory #{basename} inside similarly named archive, reducing nesting by one level.") + subdir = File.join(directory, basename) + + # simple system("mv #{escaped_subdir}/* #{escaped_directory}") would fail on 3ple nested folder subx/subx/subx + # + tmpdir = File.join(directory, ".tmp_#{basename}_#{Time.now.strftime('%Y%m%d%H%M%S%N')}_#{rand(1_000_000_000)}") + # todo consider a safer tmp dir solution - require 'mkmpdir'; Dir.mktmpdir(".tmp_#{basename}_", directory) + FileUtils.mv(subdir, tmpdir) + # Move tmpdir's children up into directory + Dir.children(tmpdir).each do |f| + FileUtils.mv(File.join(tmpdir, f), directory) + end + + self.remove_unwanted_files + end + @dir_list = nil self.sync_to_provider self.set_size! self.save @@ -79,6 +98,18 @@ def extract_collection_from_archive_file(archive_file_name) true end + # Detect a file collection with only dir, which has the same name as the collection. + # CBRAIN currently creates a FileCollection with the name of the ZIP file and puts + # in it the results of extracting the archive. What often happens then is that the + # resulting unintended level, e.g. for "sub-01.zip" we + # get "sub-01/sub-01/...". + def extra_nesting? + directory = self.cache_full_path + basename = File.basename(directory) + entries = Dir.entries(directory) - %w( . .. ) + return entries.size == 1 && File.directory?(File.join(directory, basename)) + end + # Calculates and sets the size attribute (active recount forced) def set_size! allfiles = self.list_files(:all, :regular) || [] @@ -135,7 +166,6 @@ def merge_collections(userfiles) # Mathieu Desrosiers # Returns an array of the relative paths to first level subdirectories contained in this collection. - # this function only for usage in spmbatch, feel free to contact me if you would like to remove it. def list_first_level_dirs return @dir_list if @dir_list Dir.chdir(self.cache_full_path.parent) do @@ -409,5 +439,3 @@ def list_fake_files end end - - diff --git a/BrainPortal/cbrain_plugins/cbrain-plugins-base/userfiles/file_collection/views/_file_collection.html.erb b/BrainPortal/cbrain_plugins/cbrain-plugins-base/userfiles/file_collection/views/_file_collection.html.erb index d184b163d..be2c8989a 100644 --- a/BrainPortal/cbrain_plugins/cbrain-plugins-base/userfiles/file_collection/views/_file_collection.html.erb +++ b/BrainPortal/cbrain_plugins/cbrain-plugins-base/userfiles/file_collection/views/_file_collection.html.erb @@ -33,4 +33,3 @@ :locals => { :base_directory => base_dir } %>
- diff --git a/BrainPortal/cbrain_plugins/cbrain-plugins-base/userfiles/file_collection/views/_file_collection_top_table.html.erb b/BrainPortal/cbrain_plugins/cbrain-plugins-base/userfiles/file_collection/views/_file_collection_top_table.html.erb index b7b1a1c2a..c22789c20 100644 --- a/BrainPortal/cbrain_plugins/cbrain-plugins-base/userfiles/file_collection/views/_file_collection_top_table.html.erb +++ b/BrainPortal/cbrain_plugins/cbrain-plugins-base/userfiles/file_collection/views/_file_collection_top_table.html.erb @@ -33,6 +33,11 @@The table below shows what is inside <%= @userfile.name %>/
+<% if @userfile.extra_nesting? %> ++ This file collection only contains a folder with the same name as the collection, this is probably not what you want. +
+<% end %>| @@ -48,4 +53,3 @@ The table below shows what is inside <%= @userfile.name %>/ { :base_directory => base_dir } %> |
|---|