<commit_before>module Kms
  class Asset < ActiveRecord::Base
    include UpdateStylesheetsText

    mount_uploader :file, AssetUploader

    # inspired by Locomotive
    before_validation :store_text

    validate :encoded_as_utf8, on: :create
    validate :unique_filename, on: :create

    attr_accessor :text, :performing_plain_text

    def filename
      read_attribute(:file)
    end

    def url
      file.url
    end

    def text
      @text ||= (file.read.force_encoding('UTF-8') rescue nil)
    end

    alias :content :text

    def performing_plain_text?
      performing_plain_text
    end

    def stylesheet_or_javascript?
      stylesheet? || javascript?
    end

    def text_or_javascript?
      text_type? || javascript?
    end

    def stylesheet?
      content_type.include?("css")
    end

    def javascript?
      content_type.include?("javascript")
    end

    def text_type?
      content_type.start_with?("text")
    end

    def store_text
      return if persisted? && !text_or_javascript?

      data = performing_plain_text? ? text : (file.present? ? file.read : nil)

      return if !text_or_javascript? || data.blank?

      sanitized_source = replace_urls(data)

      self.file = ::CarrierWave::SanitizedFile.new({
        tempfile: StringIO.new(sanitized_source),
        filename: filename || file.filename
      })

      @text = sanitized_source
    end

    protected

    def encoded_as_utf8
      text.encode('UTF-8')
    rescue Encoding::UndefinedConversionError
      self.errors.add(:file, I18n.t(:wrong_encoding, scope: [:activerecord, :errors, :messages]))
    end

    def unique_filename
      if Asset.where(file: file.filename).exists?
        self.errors.add(:file, I18n.t(:duplicate, scope: [:activerecord, :errors, :messages]))
      end
    end

    def replace_urls(text)
      return if text.blank?

      text.gsub(/url\((.+)\)/) do |path|
        asset_name = $1.split("/")[-1]

        if asset = Asset.where(file: asset_name.gsub(/['"]/, '')).first
          "url(#{asset.file.url})"
        else
          path
        end
      end
    end
  end
end<commit_msg>fix replacing url(...) in css asset files<commit_after>module Kms
  class Asset < ActiveRecord::Base
    include UpdateStylesheetsText

    mount_uploader :file, AssetUploader

    # inspired by Locomotive
    before_validation :store_text

    validate :encoded_as_utf8, on: :create
    validate :unique_filename, on: :create

    attr_accessor :text, :performing_plain_text

    def filename
      read_attribute(:file)
    end

    def url
      file.url
    end

    def text
      @text ||= (file.read.force_encoding('UTF-8') rescue nil)
    end

    alias :content :text

    def performing_plain_text?
      performing_plain_text
    end

    def stylesheet_or_javascript?
      stylesheet? || javascript?
    end

    def text_or_javascript?
      text_type? || javascript?
    end

    def stylesheet?
      content_type.include?("css")
    end

    def javascript?
      content_type.include?("javascript")
    end

    def text_type?
      content_type.start_with?("text")
    end

    def store_text
      return if persisted? && !text_or_javascript?

      data = performing_plain_text? ? text : (file.present? ? file.read : nil)

      return if !text_or_javascript? || data.blank?

      sanitized_source = replace_urls(data)

      self.file = ::CarrierWave::SanitizedFile.new({
        tempfile: StringIO.new(sanitized_source),
        filename: filename || file.filename
      })

      @text = sanitized_source
    end

    protected

    def encoded_as_utf8
      text.encode('UTF-8')
    rescue Encoding::UndefinedConversionError
      self.errors.add(:file, I18n.t(:wrong_encoding, scope: [:activerecord, :errors, :messages]))
    end

    def unique_filename
      if Asset.where(file: file.filename).exists?
        self.errors.add(:file, I18n.t(:duplicate, scope: [:activerecord, :errors, :messages]))
      end
    end

    # this would replace any url(...) in css files
    # with correct urls to assets that already exist
    def replace_urls(text)
      return if text.blank?

      text.gsub(/url\((\S+)\)/) do |path|
        asset_name = File.basename($1)

        if asset = Asset.where(file: asset_name.gsub(/['"]/, '').gsub(/\?.*/, '')).first
          "url('#{asset.file.url}')"
        else
          path
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.10839002267573696, 'fraction_numerical': 0.004308390022675737, 'mean_word_length': 2.5774533657745335, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '625060', 'n_tokens_mistral': 1445, 'n_tokens_neox': 1356, 'n_words': 316}