Sha256: c24bafd6a124d6bd0950841115cdc058af78b16cbd94fa39c8f9ca3cd7ddbd3f

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

require 'marcel'
require 'fileutils'

module Reaver
  # extract, move file if need
  class Walk
    def initialize(filename, dest, keep_name)
      @filename = filename
      @dest = dest
      @keep_name = keep_name || false
      check_extension
      check_name
      x
    end

    def x
      case @extension
      when %r{^image/(jpeg|png)}
        copy_file
      when %r{^application/zip}
        extract_zip
      when %r{^application/(gzip|x-xz)}
        extract_gzip
      when %r{^font/ttf}
        copy_file
      else
        puts "Filetype #{@extension} not yet supported, skipping..."
      end
    end

    private

    def check_extension
      File.open @filename do |f|
        @extension = Marcel::MimeType.for f
      end
    end

    def check_name
      name = @filename.split('.').first
      @final_dest = if @keep_name
                      "#{ENV['HOME']}/#{@dest}/#{name}"
                    else
                      "#{ENV['HOME']}/#{@dest}"
                    end
    end

    def copy_file
      puts "Copying file #{@filename} at #{@final_dest}..."
      FileUtils.mkdir_p @final_dest
      FileUtils.cp @filename, "#{@final_dest}/#{@filename}"
    end

    def extract_zip
      puts "Extracting zip archive #{@filename} at #{@final_dest}..."
      FileUtils.mkdir_p @final_dest
      `unzip -o -j #{@filename} -d #{@final_dest}`
    end

    def extract_gzip
      ext = @extension.split('/').last
      puts "Extracting #{ext} archive #{@filename} at #{@final_dest}..."
      FileUtils.mkdir_p @final_dest
      `tar x --strip-components=1 -f #{@filename} --one-top-level=#{@final_dest}`
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
reaver-0.11.1 lib/reaver/walk.rb