Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,44 @@ 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

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) || []
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -409,5 +439,3 @@ def list_fake_files
end

end


Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,3 @@
:locals => { :base_directory => base_dir } %>

<div id="sub_viewer_filecollection_cbrain"></div>

Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
<p class="medium_paragraphs">
The table below shows what is <em>inside</em> <strong><%= @userfile.name %>/</strong>
</p>
<% if @userfile.extra_nesting? %>
<p class="medium_paragraphs">
This file collection only contains a folder with the same name as the collection, this is probably <strong> not </strong> what you want.
</p>
<% end %>
<table class="plain_file_list" class="resource_list">
<tr class="list-odd">
<th>
Expand All @@ -48,4 +53,3 @@ The table below shows what is <em>inside</em> <strong><%= @userfile.name %>/</st
:locals => { :base_directory => base_dir }
%>
</table>

2 changes: 1 addition & 1 deletion BrainPortal/spec/models/file_collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
allow(file_collection).to receive(:sync_to_provider)
allow(file_collection).to receive(:set_size!)
allow(file_collection).to receive(:save)
allow(file_collection).to receive(:extra_nesting?)
end

it "should execute 'gunzip' if archive is a *.tar.gz" do
Expand Down Expand Up @@ -224,4 +225,3 @@
end

end

Loading