Class: Omnizip::FileType::MimeClassifier

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/file_type/mime_classifier.rb

Overview

Centralized MIME type classification for file type detection

This class provides methods to classify MIME types into categories and determine appropriate compression profiles based on file type.

Constant Summary collapse

TEXT_TYPES =

Text-based MIME types

%w[
  text/plain
  text/html
  text/css
  text/javascript
  text/xml
  text/csv
  text/markdown
  application/json
  application/xml
  application/javascript
  application/ecmascript
  application/x-httpd-php
  application/x-sh
  application/x-csh
  application/x-perl
  application/x-python
  application/x-ruby
  application/x-sql
  application/sql
].freeze
ARCHIVE_TYPES =

Archive MIME types

%w[
  application/zip
  application/x-7z-compressed
  application/x-rar-compressed
  application/x-tar
  application/gzip
  application/x-gzip
  application/x-bzip2
  application/x-xz
  application/x-lzip
  application/x-lzma
  application/x-compress
  application/zstd
  application/x-archive
  application/x-iso9660-image
].freeze
EXECUTABLE_TYPES =

Executable MIME types

%w[
  application/x-executable
  application/x-mach-binary
  application/x-elf
  application/x-sharedlib
  application/x-msdownload
  application/x-dosexec
  application/vnd.microsoft.portable-executable
].freeze
BINARY_TYPES =

Binary/unknown MIME types (treated as binary data)

%w[
  application/octet-stream
].freeze
MEDIA_TYPES =

Media MIME types (images, audio, video)

[
  /\Aimage\//,
  /\Aaudio\//,
  /\Avideo\//,
  "application/pdf",
].freeze

Class Method Summary collapse

Class Method Details

.archive?(mime_type) ⇒ Boolean

Check if the MIME type is an archive

Parameters:

  • mime_type (String)

    The MIME type to check

Returns:

  • (Boolean)

    true if archive



90
91
92
93
94
# File 'lib/omnizip/file_type/mime_classifier.rb', line 90

def archive?(mime_type)
  return false unless mime_type

  ARCHIVE_TYPES.include?(mime_type)
end

.executable?(mime_type) ⇒ Boolean

Check if the MIME type is executable

Parameters:

  • mime_type (String)

    The MIME type to check

Returns:

  • (Boolean)

    true if executable



100
101
102
103
104
# File 'lib/omnizip/file_type/mime_classifier.rb', line 100

def executable?(mime_type)
  return false unless mime_type

  EXECUTABLE_TYPES.include?(mime_type) || BINARY_TYPES.include?(mime_type)
end

.media?(mime_type) ⇒ Boolean

Check if the MIME type is media (image/audio/video)

Parameters:

  • mime_type (String)

    The MIME type to check

Returns:

  • (Boolean)

    true if media



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/omnizip/file_type/mime_classifier.rb', line 110

def media?(mime_type)
  return false unless mime_type

  MEDIA_TYPES.any? do |pattern|
    case pattern
    when String
      mime_type == pattern
    when Regexp
      pattern.match?(mime_type)
    end
  end
end

.profile_category(mime_type) ⇒ Symbol

Determine the recommended profile category for a MIME type

Parameters:

  • mime_type (String)

    The MIME type to classify

Returns:

  • (Symbol)

    The recommended profile category (:text, :binary, :archive, :balanced)



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/omnizip/file_type/mime_classifier.rb', line 128

def profile_category(mime_type)
  return :balanced unless mime_type

  if text?(mime_type)
    :text
  elsif executable?(mime_type)
    :binary
  elsif archive?(mime_type) || media?(mime_type)
    :archive
  else
    :balanced
  end
end

.text?(mime_type) ⇒ Boolean

Check if the MIME type is text-based

Parameters:

  • mime_type (String)

    The MIME type to check

Returns:

  • (Boolean)

    true if text-based



80
81
82
83
84
# File 'lib/omnizip/file_type/mime_classifier.rb', line 80

def text?(mime_type)
  return false unless mime_type

  TEXT_TYPES.include?(mime_type) || mime_type.start_with?("text/")
end