<commit_before>require 'digest/sha1'
require 'image_sizer'
require 's3'
require 'local_store'
require 'tempfile'
require 'pathname'

class Upload < ActiveRecord::Base
  belongs_to :user

  has_many :post_uploads
  has_many :posts, through: :post_uploads

  has_many :optimized_images, dependent: :destroy

  validates_presence_of :filesize
  validates_presence_of :original_filename

  def thumbnail
    @thumbnail ||= optimized_images.where(width: width, height: height).first
  end

  def thumbnail_url
    thumbnail.url if has_thumbnail?
  end

  def has_thumbnail?
    thumbnail.present?
  end

  def create_thumbnail!
    return unless SiteSetting.create_thumbnails?
    return if SiteSetting.enable_s3_uploads?
    return unless width > SiteSetting.auto_link_images_wider_than
    return if has_thumbnail?
    thumbnail = OptimizedImage.create_for(self, width, height)
    # TODO: @regis we may want to do the more cleanly, create_for may change the dimensions
    # this avoids a duplicate key, it should be done more cleanly, this is cheating
    thumbnail.width = width
    thumbnail.height = height

    optimized_images << thumbnail if thumbnail
  end

  def destroy
    Upload.transaction do
      Upload.remove_file url<commit_msg>Revert "cheat to fix duplicate key on thumbnails"<commit_after>require 'digest/sha1'
require 'image_sizer'
require 's3'
require 'local_store'
require 'tempfile'
require 'pathname'

class Upload < ActiveRecord::Base
  belongs_to :user

  has_many :post_uploads
  has_many :posts, through: :post_uploads

  has_many :optimized_images, dependent: :destroy

  validates_presence_of :filesize
  validates_presence_of :original_filename

  def thumbnail
    @thumbnail ||= optimized_images.where(width: width, height: height).first
  end

  def thumbnail_url
    thumbnail.url if has_thumbnail?
  end

  def has_thumbnail?
    thumbnail.present?
  end

  def create_thumbnail!
    return unless SiteSetting.create_thumbnails?
    return if SiteSetting.enable_s3_uploads?
    return unless width > SiteSetting.auto_link_images_wider_than
    return if has_thumbnail?
    thumbnail = OptimizedImage.create_for(self, width, height)
    optimized_images << thumbnail if thumbnail
  end

  def destroy
    Upload.transaction do
      Upload.remove_file url
{'fraction_non_alphanumeric': 0.06247269550021844, 'fraction_numerical': 0.002621231979030144, 'mean_word_length': 4.42654028436019, 'pattern_counts': {'":': 0, '<': 9, '<?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': '5482678', 'n_tokens_mistral': 735, 'n_tokens_neox': 702, 'n_words': 197}