Sha256: 7c54392d5c3f7c9f3430ea645abf9c45bd3e486cb716556c6244f24c83df55b8

Contents?: true

Size: 989 Bytes

Versions: 7

Compression:

Stored size: 989 Bytes

Contents

require 'zip'

class DiscourseTheme::Downloader

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

  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

  private

  def add_headers(request)
    if @is_theme_creator
      request["User-Api-Key"] = @api_key
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
discourse_theme-0.4.0 lib/discourse_theme/downloader.rb
discourse_theme-0.3.5 lib/discourse_theme/downloader.rb
discourse_theme-0.3.4 lib/discourse_theme/downloader.rb
discourse_theme-0.3.3 lib/discourse_theme/downloader.rb
discourse_theme-0.3.2 lib/discourse_theme/downloader.rb
discourse_theme-0.3.1 lib/discourse_theme/downloader.rb
discourse_theme-0.3.0 lib/discourse_theme/downloader.rb