Skip to content

Commit

Permalink
Merge pull request #2047 from james-harder/remove-extra-dirs
Browse files Browse the repository at this point in the history
Adds code to remove extra directories when uploading archives.
  • Loading branch information
Floppy authored Apr 19, 2024
2 parents dc6f8a2 + a821929 commit 2c80d68
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ docker-compose.yml
/config/locales/gettext
/config/locales/localization.*.yml
/config/locales/translation.*.yml

# ignore asdf config files
/.tool-versions
15 changes: 15 additions & 0 deletions app/controllers/uploads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def save_files(upload, library_path)
end

def unzip(dest_folder_name, datafile)
pn = Pathname.new(dest_folder_name)

flags = Archive::EXTRACT_PERM
reader = Archive::Reader.open_filename(datafile.path)
Dir.mkdir(dest_folder_name)
Expand All @@ -39,6 +41,19 @@ def unzip(dest_folder_name, datafile)
reader.extract(entry, flags.to_i)
end
end

# Checks the directory just created and if it contains only one directory,
# moves the contents of that directory up a level, then deletes the empty directory.
if pn.children.length == 1 && pn.children[0].directory?
dup_dir = Pathname.new(pn.children[0])

dup_dir.children.each do |child|
fixed_path = Pathname.new(pn.to_s + "/" + child.basename.to_s)
File.rename(child.to_s, fixed_path.to_s)
end

Dir.delete(dup_dir.to_s)
end
ensure
reader&.close
end
Expand Down

0 comments on commit 2c80d68

Please sign in to comment.