# File lib/FileSystemImage.rb, line 20
def FileSystemImage.best_fit(file_bytes,filename=nil)
  candidates=[]
  file_ext=File.extname(filename.sub(/\.gz$/,"")).downcase
  RipXploreLog.debug("looking for best fit for image format for #{filename} : extension #{file_ext}")
  ImageFormat.all_image_formats.each do |image_format|
    if !file_ext.nil? && !image_format.possible_extensions.include?(file_ext) then
      RipXploreLog.debug("skipping #{image_format} - unmatched extension #{file_ext}")
    else
      image_format.possible_file_systems.each do |file_system|
        RipXploreLog.debug("checking image format #{image_format} and file system #{file_system}")
        candidate=FileSystemImage.new(file_bytes,file_system,image_format,filename)
        RipXploreLog.debug("score - #{candidate.compatability_score}")
        candidates<<candidate
      end
    end    
  end
  
  
  if candidates.length==0 then
    RipXploreLog.debug( 'no valid combination of file system and image format found')
    return nil
  end
  best_candidate=candidates.sort{|a,b| b.compatability_score<=>a.compatability_score}[0]
  RipXploreLog.debug("best candidate = #{best_candidate.file_system}/#{best_candidate.image_format} score: #{best_candidate.compatability_score}")
  best_candidate
end