<commit_before>class AssetType
  
  # The Asset Type encapsulates a type of attachment.
  # Conventionally this would a sensible category like 'image' or 'video'
  # that should be processed and presented in a particular way.
  # An AssetType currently provides:
  #   * processor definitions for paperclip
  #   * styles definitions for paperclip
  #   * mime type list for file recognition
  #   * selectors and scopes for retrieving this (or not this) category of asset
  #   * radius tags for those subsets of assets (temporarily removed pending discussion of interface)
  
  @@types = []
  @@type_lookup = {}
  @@mime_lookup = {}
  @@default_type = nil
  attr_reader :name, :processors, :styles, :icon_name, :catchall
  
  def initialize(name, options = {})
    options = options.symbolize_keys
    @name = name
    @icon_name = options[:icon] || name
    @processors = options[:processors] || []
    @styles = options[:styles] || {}
    @mimes = options[:mime_types] || []
    if @mimes.any?
      @mimes.each { |mimetype| @@mime_lookup[mimetype] ||= self }
    end
    this = self
    Asset.send :define_method, "#{name}?".intern do this.mime_types.include?(asset_content_type) end 
    Asset.send :define_class_method, "#{name}_condition".intern do this.condition; end
    Asset.send :define_class_method, "not_#{name}_condition".intern do this.non_condition; end
    Asset.send :named_scope, plural.to_sym, :conditions => condition
    Asset.send :named_scope, "not_#{plural}".to_sym, :conditions => non_condition
    
    # Page.define_radius_tags_for_asset_type self     #TODO discuss interface
    @@types.push self
    @@type_lookup[@name] = self
  end
  
  def plural
    name.to_s.pluralize
  end

  def icon(style_name='icon')
    if File.exist?("#{RAILS_ROOT}/public/images/admin/assets/#{icon_name}_#{style_name.to_s}.png")
      return "/images/admin/assets/#{icon_name}_#{style_name.to_s}.png"
    else
      return "/images/admin/assets/#{icon_name}_icon.png"
    end
  end
  
  def icon_path(style_name='icon')
    "#{RAILS_ROOT}/public#{icon(style_name)}"
  end

  def condition
    if @mimes.any?
      ["asset_content_type IN (#{@mimes.map{'?'}.join(',')})", *@mimes]
    else
      self.class.other_condition
    end
  end
  
  def sanitized_condition
    ActiveRecord::Base.send :sanitize_sql_array, condition
  end

  def non_condition
    if @mimes.any?
      ["NOT asset_content_type IN (#{@mimes.map{'?'}.join(',')})", *@mimes]
    else
      self.class.non_other_condition
    end
  end

  def sanitized_non_condition
    ActiveRecord::Base.send :sanitize_sql_array, non_condition
  end

  def mime_types
    @mimes
  end

  def paperclip_processors
    processors
  end
  
  def paperclip_styles
    if paperclip_processors.any?
      styles.merge(configured_styles)
    end
  end
  
  def configured_styles
    Radiant::Config["assets.additional_thumbnails"].gsub(' ','').split(',').collect{|s| s.split('=')}.inject({}) {|ha, (k, v)| ha[k.to_sym] = [v, :jpg]; ha}
  end
  
  def style_dimensions(style_name)
    if style = paperclip_styles[style_name.to_sym]
      style.is_a?(Array) ? style.first : style
    end
  end
  
  def style_format(style_name)
    if style = paperclip_styles[style_name.to_sym]
      style.last if style.is_a?(Array)
    end
  end

  # class methods
  
  def self.from(mimetype)
    @@mime_lookup[mimetype] || catchall
  end
  
  def self.catchall
    @@default_type ||= self.find(:other)
  end
  
  def self.known?(name)<commit_msg>asset processing can be disabled for a particular type if, eg, you know ffmpeg is unavailable<commit_after>class AssetType
  
  # The Asset Type encapsulates a type of attachment.
  # Conventionally this would a sensible category like 'image' or 'video'
  # that should be processed and presented in a particular way.
  # An AssetType currently provides:
  #   * processor definitions for paperclip
  #   * styles definitions for paperclip
  #   * mime type list for file recognition
  #   * selectors and scopes for retrieving this (or not this) category of asset
  #   * radius tags for those subsets of assets (temporarily removed pending discussion of interface)
  
  @@types = []
  @@type_lookup = {}
  @@mime_lookup = {}
  @@default_type = nil
  attr_reader :name, :processors, :styles, :icon_name, :catchall
  
  def initialize(name, options = {})
    options = options.symbolize_keys
    @name = name
    @icon_name = options[:icon] || name
    @processors = options[:processors] || []
    @styles = options[:styles] || {}
    @mimes = options[:mime_types] || []
    if @mimes.any?
      @mimes.each { |mimetype| @@mime_lookup[mimetype] ||= self }
    end
    this = self
    Asset.send :define_method, "#{name}?".intern do this.mime_types.include?(asset_content_type) end 
    Asset.send :define_class_method, "#{name}_condition".intern do this.condition; end
    Asset.send :define_class_method, "not_#{name}_condition".intern do this.non_condition; end
    Asset.send :named_scope, plural.to_sym, :conditions => condition
    Asset.send :named_scope, "not_#{plural}".to_sym, :conditions => non_condition
    
    # Page.define_radius_tags_for_asset_type self     #TODO discuss interface
    @@types.push self
    @@type_lookup[@name] = self
  end
  
  def plural
    name.to_s.pluralize
  end

  def icon(style_name='icon')
    if File.exist?("#{RAILS_ROOT}/public/images/admin/assets/#{icon_name}_#{style_name.to_s}.png")
      return "/images/admin/assets/#{icon_name}_#{style_name.to_s}.png"
    else
      return "/images/admin/assets/#{icon_name}_icon.png"
    end
  end
  
  def icon_path(style_name='icon')
    "#{RAILS_ROOT}/public#{icon(style_name)}"
  end

  def condition
    if @mimes.any?
      ["asset_content_type IN (#{@mimes.map{'?'}.join(',')})", *@mimes]
    else
      self.class.other_condition
    end
  end
  
  def sanitized_condition
    ActiveRecord::Base.send :sanitize_sql_array, condition
  end

  def non_condition
    if @mimes.any?
      ["NOT asset_content_type IN (#{@mimes.map{'?'}.join(',')})", *@mimes]
    else
      self.class.non_other_condition
    end
  end

  def sanitized_non_condition
    ActiveRecord::Base.send :sanitize_sql_array, non_condition
  end

  def mime_types
    @mimes
  end

  def paperclip_processors
    Radiant.config["assets.skip_#{name}_processing?"] ? [] : processors
  end
  
  def paperclip_styles
    paperclip_processors.any? ? styles.merge(configured_styles) : {}
  end
  
  def configured_styles
    Radiant::Config["assets.additional_thumbnails"].gsub(' ','').split(',').collect{|s| s.split('=')}.inject({}) {|ha, (k, v)| ha[k.to_sym] = [v, :jpg]; ha}
  end
  
  def style_dimensions(style_name)
    if style = paperclip_styles[style_name.to_sym]
      style.is_a?(Array) ? style.first : style
    end
  end
  
  def style_format(style_name)
    if style = paperclip_styles[style_name.to_sym]
      style.last if style.is_a?(Array)
    end
  end

  # class methods
  
  def self.from(mimetype)
    @@mime_lookup[mimetype] || catchall
  end
  
  def self.catchall
    @@default_type ||= self.find(:other)
  end
  
  def self.known?(name)
{'fraction_non_alphanumeric': 0.12452565003513703, 'fraction_numerical': 0.0, 'mean_word_length': 3.5469648562300318, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1745523', 'n_tokens_mistral': 2446, 'n_tokens_neox': 2395, 'n_words': 543}