Sha256: c03111ac391e363418655f95fc4cf8bc27a43829231922af08bd407c302d9e08

Contents?: true

Size: 1.42 KB

Versions: 4

Compression:

Stored size: 1.42 KB

Contents

module Fontist
  module Utils
    module TarExtractor
      def tar_extract(resource)
        file = @downloaded ? resource : download_file(resource)

        dir = extract_tar_file(file)

        save_fonts(dir)
      end

      private

      def extract_tar_file(file)
        archive_file = File.open(file, "rb")
        dir = Dir.mktmpdir
        tar_reader_class.new(archive_file) do |tar|
          tar.each do |tarfile|
            save_tar_file(tarfile, dir)
          end
        end

        dir
      end

      def tar_reader_class
        @tar_reader_class ||= begin
                            require "rubygems/package"
                            Gem::Package::TarReader
                          end
      end

      def save_tar_file(file, dir)
        path = File.join(dir, file.full_name)

        if file.directory?
          FileUtils.mkdir_p(path)
        else
          File.open(path, "wb") do |f|
            f.print(file.read)
          end
        end
      end

      def save_fonts(dir)
        Array.new.tap do |fonts_paths|
          Dir.glob(File.join(dir, "**/*")).each do |path|
            filename = File.basename(path)
            next unless font_file?(filename)

            target_filename = target_filename(filename)
            font_path = fonts_path.join(target_filename).to_s
            FileUtils.mv(path, font_path)

            fonts_paths << font_path
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
fontist-1.8.7 lib/fontist/utils/tar_extractor.rb
fontist-1.8.6 lib/fontist/utils/tar_extractor.rb
fontist-1.8.5 lib/fontist/utils/tar_extractor.rb
fontist-1.8.4 lib/fontist/utils/tar_extractor.rb