Sha256: 0c3c270a4f401026c85dfeb7e6ade8b05d7d20aab1b73df55b992f548865433a

Contents?: true

Size: 1019 Bytes

Versions: 13

Compression:

Stored size: 1019 Bytes

Contents

# frozen_string_literal: true
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

13 entries across 13 versions & 1 rubygems

Version Path
discourse_theme-0.8.0 lib/discourse_theme/downloader.rb
discourse_theme-0.7.6 lib/discourse_theme/downloader.rb
discourse_theme-0.7.5 lib/discourse_theme/downloader.rb
discourse_theme-0.7.4 lib/discourse_theme/downloader.rb
discourse_theme-0.7.3 lib/discourse_theme/downloader.rb
discourse_theme-0.7.2 lib/discourse_theme/downloader.rb
discourse_theme-0.7.1 lib/discourse_theme/downloader.rb
discourse_theme-0.7.0 lib/discourse_theme/downloader.rb
discourse_theme-0.6.0 lib/discourse_theme/downloader.rb
discourse_theme-0.5.2 lib/discourse_theme/downloader.rb
discourse_theme-0.5.1 lib/discourse_theme/downloader.rb
discourse_theme-0.5.0 lib/discourse_theme/downloader.rb
discourse_theme-0.4.1 lib/discourse_theme/downloader.rb