Class: ThemeStore::ZipImporter

Inherits:
BaseImporter show all
Defined in:
lib/theme_store/zip_importer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseImporter

#[], #all_files, #cleanup!, #file_size, #real_path, #temp_folder

Constructor Details

#initialize(filename, original_filename) ⇒ ZipImporter

Returns a new instance of ZipImporter.



8
9
10
11
# File 'lib/theme_store/zip_importer.rb', line 8

def initialize(filename, original_filename)
  @filename = filename
  @original_filename = original_filename
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



6
7
8
# File 'lib/theme_store/zip_importer.rb', line 6

def url
  @url
end

Instance Method Details

#import!Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/theme_store/zip_importer.rb', line 13

def import!
  FileUtils.mkdir(temp_folder)

  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)
      strip_root_directory
    end
rescue RuntimeError
  raise RemoteTheme::ImportError, I18n.t("themes.import_error.unpack_failed")
rescue Compression::Zip::ExtractFailed
  raise RemoteTheme::ImportError, I18n.t("themes.import_error.file_too_big")
end

#strip_root_directoryObject



33
34
35
36
37
38
# File 'lib/theme_store/zip_importer.rb', line 33

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

#versionObject



29
30
31
# File 'lib/theme_store/zip_importer.rb', line 29

def version
  ""
end