Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEATURE: Gz to zip for exports (#7889)
* Revert "Revert "FEATURE: admin/user exports are compressed using the zip format (#7784)"" This reverts commit f89bd55. * Replace .tar.zip with .zip
- Loading branch information
1 parent
c1b5861
commit f5c707c
Showing
15 changed files
with
126 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'zip' | ||
|
||
class ZipUtils | ||
def zip_directory(path, export_name) | ||
zip_filename = "#{export_name}.zip" | ||
absolute_path = "#{path}/#{export_name}" | ||
entries = Dir.entries(absolute_path) - %w[. ..] | ||
|
||
Zip::File.open(zip_filename, Zip::File::CREATE) do |zipfile| | ||
write_entries(entries, absolute_path, '', zipfile) | ||
end | ||
|
||
"#{absolute_path}.zip" | ||
end | ||
|
||
def unzip_directory(path, zip_filename, allow_non_root_folder: false) | ||
Zip::File.open(zip_filename) do |zip_file| | ||
root = root_folder_present?(zip_file, allow_non_root_folder) ? '' : 'unzipped/' | ||
zip_file.each do |entry| | ||
entry_path = File.join(path, "#{root}#{entry.name}") | ||
FileUtils.mkdir_p(File.dirname(entry_path)) | ||
entry.extract(entry_path) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def root_folder_present?(filenames, allow_non_root_folder) | ||
filenames.map { |p| p.name.split('/').first }.uniq!.size == 1 || allow_non_root_folder | ||
end | ||
|
||
# A helper method to make the recursion work. | ||
def write_entries(entries, base_path, path, zipfile) | ||
entries.each do |e| | ||
zipfile_path = path == '' ? e : File.join(path, e) | ||
disk_file_path = File.join(base_path, zipfile_path) | ||
|
||
if File.directory? disk_file_path | ||
recursively_deflate_directory(disk_file_path, zipfile, base_path, zipfile_path) | ||
else | ||
put_into_archive(disk_file_path, zipfile, zipfile_path) | ||
end | ||
end | ||
end | ||
|
||
def recursively_deflate_directory(disk_file_path, zipfile, base_path, zipfile_path) | ||
zipfile.mkdir zipfile_path | ||
subdir = Dir.entries(disk_file_path) - %w[. ..] | ||
write_entries subdir, base_path, zipfile_path, zipfile | ||
end | ||
|
||
def put_into_archive(disk_file_path, zipfile, zipfile_path) | ||
zipfile.add(zipfile_path, disk_file_path) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'import_export/zip_utils' | ||
|
||
module ThemeStore; end | ||
|
||
class ThemeStore::TgzImporter | ||
This comment has been minimized.
Sorry, something went wrong.
davidtaylorhq
Member
|
||
|
@@ -13,8 +15,16 @@ def initialize(filename) | |
|
||
def import! | ||
FileUtils.mkdir(@temp_folder) | ||
|
||
Dir.chdir(@temp_folder) do | ||
Discourse::Utils.execute_command("tar", "-xzvf", @filename, "--strip", "1") | ||
if @filename.include?('.zip') | ||
ZipUtils.new.unzip_directory(@temp_folder, @filename) | ||
|
||
# --strip 1 equivalent | ||
FileUtils.mv(Dir.glob("#{@temp_folder}/*/*"), @temp_folder) | ||
else | ||
Discourse::Utils.execute_command("tar", "-xzvf", @filename, "--strip", "1") | ||
end | ||
end | ||
rescue RuntimeError | ||
raise RemoteTheme::ImportError, I18n.t("themes.import_error.unpack_failed") | ||
|
@@ -44,7 +54,7 @@ def real_path(relative) | |
|
||
def all_files | ||
Dir.chdir(@temp_folder) do | ||
Dir.glob("**/*").reject { |f| File.directory?(f) } | ||
Dir.glob("**/**").reject { |f| File.directory?(f) } | ||
end | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 comment
on commit f5c707c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found a few small issues when working on discourse/discourse_theme@de0940b. I've commented two above, and also the content-type of the download should be changed from application/x-gzip
to application/zip
ThemeStore::TgzExporter
should be renamed toZipExporter
, to avoid confusion