Class: Omnizip::Profile::ProfileDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/profile/profile_detector.rb

Overview

Automatic profile detection based on file analysis

This class analyzes files and recommends optimal compression profiles based on MIME type detection and user preferences.

Instance Method Summary collapse

Constructor Details

#initialize(registry = nil) ⇒ ProfileDetector

Initialize a new profile detector

Parameters:

  • registry (ProfileRegistry) (defaults to: nil)

    Profile registry to use



13
14
15
# File 'lib/omnizip/profile/profile_detector.rb', line 13

def initialize(registry = nil)
  @registry = registry || Profile.registry
end

Instance Method Details

#detect(file_path, options = {}) ⇒ CompressionProfile

Detect the optimal profile for a file

Parameters:

  • file_path (String)

    Path to the file

  • options (Hash) (defaults to: {})

    Detection options

Options Hash (options):

  • :fallback (Symbol)

    Fallback profile if detection fails (default: :balanced)

Returns:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/omnizip/profile/profile_detector.rb', line 24

def detect(file_path, options = {})
  fallback = options[:fallback] || :balanced

  # Detect MIME type
  mime_type = detect_mime_type(file_path)

  # Find suitable profiles
  suitable = find_suitable_profiles(mime_type)

  # Return most appropriate profile or fallback to balanced
  select_best_profile(suitable, mime_type) ||
    @registry.get(fallback) ||
    @registry.get(:balanced)
end

#detect_mime_type(file_path) ⇒ String?

Detect MIME type using FileType detector

Parameters:

  • file_path (String)

    Path to the file

Returns:

  • (String, nil)

    MIME type string or nil



43
44
45
46
47
48
49
# File 'lib/omnizip/profile/profile_detector.rb', line 43

def detect_mime_type(file_path)
  return nil unless File.exist?(file_path)

  Omnizip::FileType.detect(file_path)
rescue StandardError
  nil
end

#find_suitable_profiles(mime_type) ⇒ Array<CompressionProfile>

Find profiles suitable for a MIME type

Parameters:

  • mime_type (String, nil)

    MIME type string

Returns:



55
56
57
58
59
# File 'lib/omnizip/profile/profile_detector.rb', line 55

def find_suitable_profiles(mime_type)
  return [] unless mime_type

  @registry.suitable_for(mime_type)
end

#select_best_profile(profiles, mime_type) ⇒ CompressionProfile?

Select the best profile from suitable candidates

Parameters:

  • profiles (Array<CompressionProfile>)

    Suitable profiles

  • mime_type (String, nil)

    MIME type string

Returns:



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/omnizip/profile/profile_detector.rb', line 66

def select_best_profile(profiles, mime_type)
  return nil if profiles.empty?
  return profiles.first if profiles.size == 1

  # Priority order for specific MIME types
  priority = profile_priority(mime_type)

  # Find first profile in priority order
  priority.each do |name|
    profile = profiles.find { |p| p.name == name }
    return profile if profile
  end

  # Return first suitable profile if none matched priority
  profiles.first
end