<commit_before>require 'mimemagic'
module Anki
  class Media

    def self.process filepath, str
      media = MimeMagic.by_magic(File.open(filepath))
      case
        when media.image?
          tag = %(<img src="#{File.basename(filepath)}"/>)

          if str.match(/{{img}}/)
            str.sub!(/{{img}}/, tag)
          else
            str << tag
          end
        when media.audio?, media.video? #video also has a sound tag, idk
          str << "[sound:#{File.basename(filepath)}]"
        else
          raise "Unknown media type - #{filepath}"
      end
    end

    def self.valid_media? filepath
      return false unless File.exists?(filepath)

      media = MimeMagic.by_magic(File.open(filepath))
      if media.image? or media.video? or media.audio?
        true
      else
        false
      end
    end

  end
end
<commit_msg>Test for existing media tags before adding.<commit_after>require 'mimemagic'
module Anki
  class Media

    def self.process filepath, str
      media = MimeMagic.by_magic(File.open(filepath))
      case
        when media.image?
          tag = %(<img src="#{File.basename(filepath)}"/>)

          if str.match(/{{img}}/)
            str.sub!(/{{img}}/, tag)
          else
            str << tag unless str.match tag
          end
        when media.audio?, media.video? #video also has a sound tag, idk
          tag = "[sound:#{File.basename(filepath)}]"
          str << tag unless str.match tag
        else
          raise "Unknown media type - #{filepath}"
      end
    end

    def self.valid_media? filepath
      return false unless File.exists?(filepath)

      media = MimeMagic.by_magic(File.open(filepath))
      if media.image? or media.video? or media.audio?
        true
      else
        false
      end
    end

  end
end

{'fraction_non_alphanumeric': 0.11234705228031146, 'fraction_numerical': 0.0, 'mean_word_length': 1.9157212317666126, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 5, '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': '1171603', 'n_tokens_mistral': 558, 'n_tokens_neox': 535, 'n_words': 150}