<commit_before>class DirectUploadsController < ApplicationController
  before_action :authenticate_user!

  def request_policy
    render json: {
      url: url,
      access_key_id: ENV['AWS_ACCESS_KEY_ID'],
      acl: 'public-read',
      policy: s3_upload_policy_document,
      signature: s3_upload_signature,
      key: "pending/#{current_user.id}/#{params[:file_prefix]}/#{SecureRandom.uuid}"
    }
  end

  private

  def url
    ENV['S3_URL']
  end

  def request_params
    params.require(:file_prefix)
  end

  # generate the policy document that amazon is expecting.
  def s3_upload_policy_document
    Base64.encode64(
      {
        expiration: 30.minutes.from_now.utc.strftime('%Y-%m-%dT%H:%M:%S.000Z'),
        conditions: [
          { bucket: ENV['S3_BUCKET'] },
          { acl: 'public-read' },
          ["starts-with", "$key", "pending/"],
          ["eq", "$Content-Type", params[:content_type]],
          { success_action_status: '201' }
        ]
      }.to_json
    ).gsub(/\n|\r/, '')
  end

  # sign our request by Base64 encoding the policy document.
  def s3_upload_signature
    Base64.encode64(
      OpenSSL::HMAC.digest(
        OpenSSL::Digest::Digest.new('sha1'),
        ENV['AWS_SECRET_ACCESS_KEY'],
        s3_upload_policy_document
      )
    ).gsub(/\n/, '')
  end
end
<commit_msg>Delegate upload form stuff to S3FormConfigurator.<commit_after>class DirectUploadsController < ApplicationController
  before_action :authenticate_user!

  def request_policy
    upload_form = S3FormConfigurator.new url: ENV['S3_URL'],
      bucket_name: ENV['S3_BUCKET'],
      aws_key: ENV['AWS_ACCESS_KEY_ID'],
      aws_secret: ENV['AWS_SECRET_ACCESS_KEY'],
      upload_path: "pending/#{current_user.id}/#{params[:file_prefix]}",
      content_type: params[:content_type]

    render json: {
      url: upload_form.url,
      access_key_id: upload_form.access_key_id,
      acl: upload_form.acl,
      policy: upload_form.policy,
      signature: upload_form.signature,
      key: upload_form.key
    }
  end
end

{'fraction_non_alphanumeric': 0.12340842311459353, 'fraction_numerical': 0.014691478942213516, 'mean_word_length': 3.102409638554217, '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': '2225038', 'n_tokens_mistral': 711, 'n_tokens_neox': 686, 'n_words': 121}