Sha256: 14377cf28a6988d2e24495d8f4b41b9e0ad015768b221dc02f9b411b271d77a6

Contents?: true

Size: 1.53 KB

Versions: 8

Compression:

Stored size: 1.53 KB

Contents

require_relative 'identification_tool'

module Libis
  module Format

    class FileTool < Libis::Format::IdentificationTool

      def run_list(filelist)

        create_list_file(filelist) do |list_file|

          output = runner(nil, '--files-from', list_file)

          process_output(output)

        end

      end

      def run_dir(dir, recursive = true)

        filelist = find_files(dir, recursive)

        create_list_file(filelist) do |list_file|

          output = runner(nil, '--files-from', list_file)

          process_output(output)

        end

      end

      def run(file)

        output = runner(file)

        process_output(output)

      end

      protected

      def runner(filename, *args)

        # Create new argument list
        opts = []

        # Add fixed options
        # -L : follow symlinks
        # --mime-type : only print MIME type
        opts << '-L' << '--mime-type'

        # Append passed arguments
        opts += args

        # Finally add the filename to process
        opts << filename.escape_for_string if filename

        # Run the UNIX file command and capture the results
        file_tool = ::Libis::Tools::Command.run('file', *opts)

        raise RuntimeError, "File command errors: #{file_tool[:err].join("\n")}" unless file_tool[:err].empty?


        # Parse output text into array and return result
        file_tool[:out].map do |line|
          r = line.split(/:\s+/)
          {filepath: r[0], mimetype: r[1], matchtype: 'magic', source: :file}
        end
      end

    end

  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
libis-format-0.9.41 lib/libis/format/file_tool.rb
libis-format-0.9.40 lib/libis/format/file_tool.rb
libis-format-0.9.39 lib/libis/format/file_tool.rb
libis-format-0.9.38 lib/libis/format/file_tool.rb
libis-format-0.9.37 lib/libis/format/file_tool.rb
libis-format-0.9.36 lib/libis/format/file_tool.rb
libis-format-0.9.35 lib/libis/format/file_tool.rb
libis-format-0.9.33 lib/libis/format/file_tool.rb