2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-03-21 10:28:52 -04:00
|
|
|
require "compression/engine"
|
2019-07-18 08:34:48 -04:00
|
|
|
|
2023-09-11 19:38:47 -04:00
|
|
|
class ThemeStore::ZipImporter < ThemeStore::BaseImporter
|
2018-03-12 03:36:06 -04:00
|
|
|
attr_reader :url
|
|
|
|
|
2019-10-03 09:19:35 -04:00
|
|
|
def initialize(filename, original_filename)
|
2018-03-12 03:36:06 -04:00
|
|
|
@filename = filename
|
2019-10-03 09:19:35 -04:00
|
|
|
@original_filename = original_filename
|
2018-03-12 03:36:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def import!
|
2023-08-22 14:30:33 -04:00
|
|
|
FileUtils.mkdir(temp_folder)
|
2019-07-18 08:34:48 -04:00
|
|
|
|
2019-11-13 04:57:39 -05:00
|
|
|
available_size = SiteSetting.decompressed_theme_max_file_size_mb
|
|
|
|
Compression::Engine
|
|
|
|
.engine_for(@original_filename)
|
|
|
|
.tap do |engine|
|
2023-08-22 14:30:33 -04:00
|
|
|
engine.decompress(temp_folder, @filename, available_size)
|
2022-09-29 14:00:38 -04:00
|
|
|
strip_root_directory
|
2018-03-12 03:36:06 -04:00
|
|
|
end
|
2019-01-23 09:40:21 -05:00
|
|
|
rescue RuntimeError
|
|
|
|
raise RemoteTheme::ImportError, I18n.t("themes.import_error.unpack_failed")
|
2019-10-03 09:19:35 -04:00
|
|
|
rescue Compression::Zip::ExtractFailed
|
|
|
|
raise RemoteTheme::ImportError, I18n.t("themes.import_error.file_too_big")
|
2018-03-12 03:36:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def version
|
|
|
|
""
|
|
|
|
end
|
|
|
|
|
2022-09-29 14:00:38 -04:00
|
|
|
def strip_root_directory
|
2023-08-22 14:30:33 -04:00
|
|
|
root_files = Dir.glob("#{temp_folder}/*")
|
2022-09-29 14:00:38 -04:00
|
|
|
if root_files.size == 1 && File.directory?(root_files[0])
|
2023-08-22 14:30:33 -04:00
|
|
|
FileUtils.mv(Dir.glob("#{temp_folder}/*/*"), temp_folder)
|
2022-09-29 14:00:38 -04:00
|
|
|
end
|
|
|
|
end
|
2018-03-12 03:36:06 -04:00
|
|
|
end
|