<commit_before>require 'tempfile'
require 'mini_magick'

class Resource < ActiveRecord::Base
  validates_uniqueness_of :filename
  after_destroy :delete_filename_on_disk
  before_validation :uniq_filename_on_disk, :on => :create

  belongs_to :article

  def fullpath(file = nil)
    "#{::Rails.root.to_s}/public/files/#{file.nil? ? filename : file}"
  end

  def write_to_disk(up)
    begin
      # create the public/files dir if it doesn't exist
      FileUtils.mkdir(fullpath('')) unless File.directory?(fullpath(''))
      if up.kind_of?(Tempfile) and !up.local_path.nil? and File.exist?(up.local_path)
        File.chmod(0600, up.local_path)
        FileUtils.copy(up.local_path, fullpath)
      else
        bytes = up
        if up.kind_of?(StringIO)
          up.rewind
          bytes = up.read
        end
        File.open(fullpath, "wb") { |f| f.write(bytes) }
      end
      File.chmod(0644, fullpath)
      self.size = File.stat(fullpath).size rescue 0
      update
      self
    rescue
      raise
    end
  end

  def create_thumbnail
    blog = Blog.default
    return unless self.mime =~ /image/<commit_msg>Fixes file upload, at least until we change our model uploader.<commit_after>require 'tempfile'
require 'mini_magick'

class Resource < ActiveRecord::Base
  validates_uniqueness_of :filename
  after_destroy :delete_filename_on_disk
  before_validation :uniq_filename_on_disk, :on => :create

  belongs_to :article

  def fullpath(file = nil)
    "#{::Rails.root.to_s}/public/files/#{file.nil? ? filename : file}"
  end

  def write_to_disk(up)
    begin
      # create the public/files dir if it doesn't exist
      FileUtils.mkdir(fullpath('')) unless File.directory?(fullpath(''))
      if up.kind_of?(Tempfile) and !up.local_path.nil? and File.exist?(up.local_path)
        File.chmod(0600, up.local_path)
        FileUtils.copy(up.local_path, fullpath)
      elsif up.kind_of?(ActionDispatch::Http::UploadedFile)
        File.chmod(0600, up.path)
        FileUtils.copy(up.path, fullpath)
      else
        bytes = up
        if up.kind_of?(StringIO)
          up.rewind
          bytes = up.read
        end
        File.open(fullpath, "wb") { |f| f.write(bytes) }
      end
      File.chmod(0644, fullpath)
      self.size = File.stat(fullpath).size rescue 0
      update
      self
    rescue
      raise
    end
  end

  def create_thumbnail
    blog = Blog.default
    return unless self.mime =~ /image/
{'fraction_non_alphanumeric': 0.10819672131147541, 'fraction_numerical': 0.009016393442622951, 'mean_word_length': 2.7844961240310075, 'pattern_counts': {'":': 0, '<': 5, '<?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': '475517', 'n_tokens_mistral': 831, 'n_tokens_neox': 817, 'n_words': 179}