Class: Omnizip::Profile::ProfileDetector
- Inherits:
-
Object
- Object
- Omnizip::Profile::ProfileDetector
- 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
-
#detect(file_path, options = {}) ⇒ CompressionProfile
Detect the optimal profile for a file.
-
#detect_mime_type(file_path) ⇒ String?
Detect MIME type using FileType detector.
-
#find_suitable_profiles(mime_type) ⇒ Array<CompressionProfile>
Find profiles suitable for a MIME type.
-
#initialize(registry = nil) ⇒ ProfileDetector
constructor
Initialize a new profile detector.
-
#select_best_profile(profiles, mime_type) ⇒ CompressionProfile?
Select the best profile from suitable candidates.
Constructor Details
#initialize(registry = nil) ⇒ ProfileDetector
Initialize a new profile detector
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
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, = {}) fallback = [: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
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
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
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 |