<commit_before>module BeachApiCore
  class Asset < ApplicationRecord
    belongs_to :entity, polymorphic: true

    attachment :file
    validates :file, :entity, :file_extension, presence: true

    before_validation :set_file_extension

    scope :images, -> { where(file_extension: IMAGE_EXTENSION) }
    scope :files, -> { where.not(file_extension: IMAGE_EXTENSION) }

    IMAGE_EXTENSION = [:jpg, :jpeg, :bmp, :png, :gif].freeze

    def base64=(value)
      partitions = value.partition(';base64,')
      decoded_image = Base64.decode64(partitions.last)
      content_type = partitions.first.gsub(/data\:/, '')

      Tempfile.new('asset').tap do |f|
        f.binmode
        f.write(decoded_image)
        f.unlink
        assign_attributes(file: f,
                          file_filename: "asset.#{content_type.gsub(/image\//, '')}",
                          file_size: decoded_image.size,
                          file_content_type: content_type
        )
      end
    end

    private

    def set_file_extension
      self.file_extension = file_filename.split('.').last.to_s.downcase if file_filename
    end
  end
end<commit_msg>Fix S3 upload<commit_after>module BeachApiCore
  class Asset < ApplicationRecord
    belongs_to :entity, polymorphic: true

    attachment :file
    validates :file, :entity, :file_extension, presence: true

    before_validation :set_file_extension

    scope :images, -> { where(file_extension: IMAGE_EXTENSION) }
    scope :files, -> { where.not(file_extension: IMAGE_EXTENSION) }

    IMAGE_EXTENSION = [:jpg, :jpeg, :bmp, :png, :gif].freeze

    def base64=(value)
      partitions = value.partition(';base64,')
      decoded_image = Base64.decode64(partitions.last)
      content_type = partitions.first.gsub(/data\:/, '')

      Tempfile.new('asset').tap do |f|
        f.binmode
        f.write(decoded_image)
        f.rewind
        assign_attributes(file: f,
                          file_filename: "asset.#{content_type.gsub(/image\//, '')}",
                          file_size: decoded_image.size,
                          file_content_type: content_type
        )
      end
    end

    private

    def set_file_extension
      self.file_extension = file_filename.split('.').last.to_s.downcase if file_filename
    end
  end
end
{'fraction_non_alphanumeric': 0.1099476439790576, 'fraction_numerical': 0.007417102966841187, 'mean_word_length': 2.5114854517611027, 'pattern_counts': {'":': 0, '<': 5, '<?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': '107874', 'n_tokens_mistral': 740, 'n_tokens_neox': 703, 'n_words': 135}