<commit_before>require 'digest/sha1'

class Upload < ActiveRecord::Base
  # attr_accessible :title, :body

  belongs_to :user
  belongs_to :topic

  validates_presence_of :filesize
  validates_presence_of :original_filename


  # Create an upload given a user, file and optional topic_id
  def self.create_for(user, file, topic_id = nil)
    # TODO: Need specs/tests for this functionality
    return create_on_imgur(user, file, topic_id) if SiteSetting.enable_imgur?
    return create_on_s3(user, file, topic_id) if SiteSetting.enable_s3_uploads?
    return create_locally(user, file, topic_id)
  end

  # Store uploads on s3
  def self.create_on_imgur(user, file, topic_id)
    @imgur_loaded = require 'imgur' unless @imgur_loaded


    info = Imgur.upload_file(file)
    Upload.create!({user_id: user.id,
                    topic_id: topic_id,
                    original_filename: file.original_filename}.merge!(info))
  end

  def self.create_locally(user, file, topic_id)
    upload = Upload.create!(user_id: user.id,
                            topic_id: topic_id,
                            url: "",
                            filesize: File.size(file.tempfile),
                            original_filename: file.original_filename)

    # populate the rest of the info
    clean_name = Digest::SHA1.hexdigest("#{Time.now.to_s}#{file.original_filename}")[0,16]
    image_info = FastImage.new(file.tempfile)
    clean_name += ".#{image_info.type}"
    url_root = "/uploads/#{RailsMultisite::ConnectionManagement.current_db}/#{upload.id}"
    path = "#{Rails.root}/public#{url_root}"
    upload.width, upload.height = ImageSizer.resize(*image_info.size)
    FileUtils.mkdir_p path
    # not using cause mv, cause permissions are no good on move
    File.open("#{path}/#{clean_name}", "wb") do |f|
      f.write File.read(file.tempfile)
    end
    upload.url = "#{url_root}/#{clean_name}"
    upload.save

    upload
  end

  def self.create_on_s3(user, file, topic_id)
    @fog_loaded = require 'fog' unless @fog_loaded

    tempfile = file.tempfile

    upload = Upload.new(user_id: user.id,
                        topic_id: topic_id,
                        filesize: File.size(tempfile),
                        original_filename: file.original_filename)

    image_info = FastImage.new(tempfile)
    blob = file.read
    sha1 = Digest::SHA1.hexdigest(blob)


    Fog.credentials_path = "#{Rails.root}/config/fog_credentials.yml"
    fog = Fog::Storage.new(provider: 'AWS')

    remote_filename = "#{sha1[2..-1]}.#{image_info.type}"
    path = "/uploads/#{sha1[0]}/#{sha1[1]}"
    location = "#{SiteSetting.s3_upload_bucket}#{path}"
    directory = fog.directories.create(key: location)

    Rails.logger.info "#{blob.size.inspect}"
    file = directory.files.create(key: remote_filename,
                                  body: tempfile,
                                  public: true,
                                  content_type: file.content_type)
    upload.width, upload.height = ImageSizer.resize(*image_info.size)
    upload.url = "#{Rails.configuration.action_controller.asset_host}#{path}/#{remote_filename}"
    upload.save

    upload
  end
end<commit_msg>FIX: S3 image upload<commit_after>require 'digest/sha1'

class Upload < ActiveRecord::Base
  # attr_accessible :title, :body

  belongs_to :user
  belongs_to :topic

  validates_presence_of :filesize
  validates_presence_of :original_filename


  # Create an upload given a user, file and optional topic_id
  def self.create_for(user, file, topic_id = nil)
    # TODO: Need specs/tests for this functionality
    return create_on_imgur(user, file, topic_id) if SiteSetting.enable_imgur?
    return create_on_s3(user, file, topic_id) if SiteSetting.enable_s3_uploads?
    return create_locally(user, file, topic_id)
  end

  # Store uploads on s3
  def self.create_on_imgur(user, file, topic_id)
    @imgur_loaded = require 'imgur' unless @imgur_loaded


    info = Imgur.upload_file(file)
    Upload.create!({user_id: user.id,
                    topic_id: topic_id,
                    original_filename: file.original_filename}.merge!(info))
  end

  def self.create_locally(user, file, topic_id)
    upload = Upload.create!(user_id: user.id,
                            topic_id: topic_id,
                            url: "",
                            filesize: File.size(file.tempfile),
                            original_filename: file.original_filename)

    # populate the rest of the info
    clean_name = Digest::SHA1.hexdigest("#{Time.now.to_s}#{file.original_filename}")[0,16]
    image_info = FastImage.new(file.tempfile)
    clean_name += ".#{image_info.type}"
    url_root = "/uploads/#{RailsMultisite::ConnectionManagement.current_db}/#{upload.id}"
    path = "#{Rails.root}/public#{url_root}"
    upload.width, upload.height = ImageSizer.resize(*image_info.size)
    FileUtils.mkdir_p path
    # not using cause mv, cause permissions are no good on move
    File.open("#{path}/#{clean_name}", "wb") do |f|
      f.write File.read(file.tempfile)
    end
    upload.url = "#{url_root}/#{clean_name}"
    upload.save

    upload
  end

  def self.create_on_s3(user, file, topic_id)
    @fog_loaded = require 'fog' unless @fog_loaded

    tempfile = file.tempfile

    upload = Upload.new(user_id: user.id,
                        topic_id: topic_id,
                        filesize: File.size(tempfile),
                        original_filename: file.original_filename)

    image_info = FastImage.new(tempfile)
    blob = file.read
    sha1 = Digest::SHA1.hexdigest(blob)

    Fog.credentials_path = "#{Rails.root}/config/fog_credentials.yml"
    fog = Fog::Storage.new(provider: 'AWS')

    remote_filename = "#{sha1[2..-1]}.#{image_info.type}"
    path = "/uploads/#{sha1[0]}/#{sha1[1]}"
    location = "#{SiteSetting.s3_upload_bucket}#{path}"
    directory = fog.directories.create(key: location)

    Rails.logger.info "#{blob.size.inspect}"
    file = directory.files.create(key: remote_filename,
                                  body: tempfile,
                                  public: true,
                                  content_type: file.content_type)
    upload.width, upload.height = ImageSizer.resize(*image_info.size)
    upload.url = "https://#{SiteSetting.s3_upload_bucket}.s3.amazonaws.com#{path}/#{remote_filename}"

    upload.save

    upload
  end
end
{'fraction_non_alphanumeric': 0.11607142857142858, 'fraction_numerical': 0.006422305764411027, 'mean_word_length': 2.7100522951772223, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5731510', 'n_tokens_mistral': 2079, 'n_tokens_neox': 2028, 'n_words': 390}