2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-10-03 09:19:35 -04:00
|
|
|
require_dependency 'compression/engine'
|
2019-07-18 08:34:48 -04:00
|
|
|
|
2018-03-12 03:36:06 -04:00
|
|
|
module ThemeStore; end
|
|
|
|
|
2019-10-03 09:19:35 -04:00
|
|
|
class ThemeStore::ZipImporter
|
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
|
|
|
@temp_folder = "#{Pathname.new(Dir.tmpdir).realpath}/discourse_theme_#{SecureRandom.hex}"
|
|
|
|
@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!
|
|
|
|
FileUtils.mkdir(@temp_folder)
|
2019-07-18 08:34:48 -04:00
|
|
|
|
2019-07-10 10:38:51 -04:00
|
|
|
Dir.chdir(@temp_folder) do
|
2019-10-03 09:19:35 -04:00
|
|
|
Compression::Engine.engine_for(@original_filename).tap do |engine|
|
|
|
|
engine.decompress(@temp_folder, @filename)
|
2019-07-18 08:34:48 -04:00
|
|
|
end
|
2019-10-03 09:19:35 -04:00
|
|
|
|
|
|
|
# --strip 1 equivalent
|
|
|
|
FileUtils.mv(Dir.glob("#{@temp_folder}/*/*"), @temp_folder)
|
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 cleanup!
|
|
|
|
FileUtils.rm_rf(@temp_folder)
|
|
|
|
end
|
|
|
|
|
|
|
|
def version
|
|
|
|
""
|
|
|
|
end
|
|
|
|
|
|
|
|
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 09:40:21 -05:00
|
|
|
def all_files
|
|
|
|
Dir.chdir(@temp_folder) do
|
2019-07-18 08:34:48 -04:00
|
|
|
Dir.glob("**/**").reject { |f| File.directory?(f) }
|
2019-01-23 09:40:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-12 03:36:06 -04:00
|
|
|
def [](value)
|
|
|
|
fullpath = real_path(value)
|
|
|
|
return nil unless fullpath
|
|
|
|
File.read(fullpath)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|