Class: DiscourseTheme::Downloader

Inherits:
Object
  • Object
show all
Defined in:
lib/discourse_theme/downloader.rb

Instance Method Summary collapse

Constructor Details

#initialize(dir:, client:) ⇒ Downloader

Returns a new instance of Downloader.



5
6
7
8
9
# File 'lib/discourse_theme/downloader.rb', line 5

def initialize(dir:, client:)
  @dir = dir
  @client = client
  @theme_id = nil
end

Instance Method Details

#download_theme(id) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/discourse_theme/downloader.rb', line 11

def download_theme(id)
  raw, filename = @client.get_raw_theme_export(id)

  if filename.end_with?(".zip")
    Zip::File.open_buffer(raw) do |zip_file|
      zip_file.each do |entry|
        new_path = File.join(@dir, entry.name)
        entry.extract(new_path)
      end
    end
  else
    sio = StringIO.new(raw)
    gz = Zlib::GzipReader.new(sio)
    Minitar.unpack(gz, @dir)

    # Minitar extracts into a sub directory, move all the files up one dir
    Dir.chdir(@dir) do
      folders = Dir.glob('*/')
      raise "Extraction failed" unless folders.length == 1
      FileUtils.mv(Dir.glob("#{folders[0]}*"), "./")
      FileUtils.remove_dir(folders[0])
    end
  end
end