2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-03-21 22:28:52 +08:00
|
|
|
require "compression/engine"
|
2019-07-18 20:34:48 +08:00
|
|
|
|
2018-03-12 15:36:06 +08:00
|
|
|
module ThemeStore
|
|
|
|
end
|
|
|
|
|
2019-10-03 21:19:35 +08:00
|
|
|
class ThemeStore::ZipImporter
|
2018-03-12 15:36:06 +08:00
|
|
|
attr_reader :url
|
|
|
|
|
2019-10-03 21:19:35 +08:00
|
|
|
def initialize(filename, original_filename)
|
2018-03-12 15:36:06 +08:00
|
|
|
@temp_folder = "#{Pathname.new(Dir.tmpdir).realpath}/discourse_theme_#{SecureRandom.hex}"
|
|
|
|
@filename = filename
|
2019-10-03 21:19:35 +08:00
|
|
|
@original_filename = original_filename
|
2018-03-12 15:36:06 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def import!
|
|
|
|
FileUtils.mkdir(@temp_folder)
|
2019-07-18 20:34:48 +08:00
|
|
|
|
2019-11-13 17:57:39 +08:00
|
|
|
available_size = SiteSetting.decompressed_theme_max_file_size_mb
|
|
|
|
Compression::Engine
|
|
|
|
.engine_for(@original_filename)
|
|
|
|
.tap do |engine|
|
|
|
|
engine.decompress(@temp_folder, @filename, available_size)
|
2022-09-30 02:00:38 +08:00
|
|
|
strip_root_directory
|
2018-03-12 15:36:06 +08:00
|
|
|
end
|
2019-01-23 22:40:21 +08:00
|
|
|
rescue RuntimeError
|
|
|
|
raise RemoteTheme::ImportError, I18n.t("themes.import_error.unpack_failed")
|
2019-10-03 21:19:35 +08:00
|
|
|
rescue Compression::Zip::ExtractFailed
|
|
|
|
raise RemoteTheme::ImportError, I18n.t("themes.import_error.file_too_big")
|
2018-03-12 15:36:06 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def cleanup!
|
|
|
|
FileUtils.rm_rf(@temp_folder)
|
|
|
|
end
|
|
|
|
|
|
|
|
def version
|
|
|
|
""
|
|
|
|
end
|
|
|
|
|
2022-09-30 02:00:38 +08:00
|
|
|
def strip_root_directory
|
|
|
|
root_files = Dir.glob("#{@temp_folder}/*")
|
|
|
|
if root_files.size == 1 && File.directory?(root_files[0])
|
|
|
|
FileUtils.mv(Dir.glob("#{@temp_folder}/*/*"), @temp_folder)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-12 15:36:06 +08:00
|
|
|
def real_path(relative)
|
|
|
|
fullpath = "#{@temp_folder}/#{relative}"
|
|
|
|
return nil unless File.exist?(fullpath)
|
|
|
|
|
|
|
|
# careful to handle symlinks here, don't want to expose random data
|
|
|
|
fullpath = Pathname.new(fullpath).realpath.to_s
|
|
|
|
|
|
|
|
if fullpath && fullpath.start_with?(@temp_folder)
|
|
|
|
fullpath
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-23 22:40:21 +08:00
|
|
|
def all_files
|
2019-11-14 07:45:09 +08:00
|
|
|
Dir.glob("**/**", base: @temp_folder).reject { |f| File.directory?(File.join(@temp_folder, f)) }
|
2019-01-23 22:40:21 +08:00
|
|
|
end
|
|
|
|
|
2018-03-12 15:36:06 +08:00
|
|
|
def [](value)
|
|
|
|
fullpath = real_path(value)
|
|
|
|
return nil unless fullpath
|
|
|
|
File.read(fullpath)
|
|
|
|
end
|
|
|
|
end
|