Sha256: 7528153d6ac5f7854a496009d5cbe8973e3cc957a74da01b8c169d33684fa69e

Contents?: true

Size: 1.27 KB

Versions: 5

Compression:

Stored size: 1.27 KB

Contents

class S3Relay::UploadsController < ApplicationController

  before_action :authenticate
  skip_before_action :verify_authenticity_token

  def new
    render json: S3Relay::UploadPresigner.new.form_data
  end

  def create
    @upload = S3Relay::Upload.new(upload_attrs)

    if @upload.save
      data = {
        private_url: @upload.private_url,
        parent_type: @upload.parent_type,
        parent_id: @upload.parent_id,
        user_id: BSON::ObjectId(user_attrs[:user_id])
      }
      render json: data, status: 201
    else
      render json: { errors: @upload.errors }, status: 422
    end
  end

  protected

  def authenticate
    if respond_to?(:authenticate_for_s3_relay)
      authenticate_for_s3_relay
    end
  end

  def parent_attrs
    {
      parent_type: params[:parent_type].try(:classify),
      parent_id: BSON::ObjectId(params[:parent_id])
    }
  end

  def upload_attrs
    attrs = {
      upload_type:  params[:association].try(:classify),
      uuid:         params[:uuid],
      filename:     params[:filename],
      content_type: params[:content_type]
    }

    attrs.merge!(parent_attrs)
    attrs.merge!(user_attrs)
  end

  def user_attrs
    if respond_to?(:current_user) && (id = current_user&.id)
      { user_id: id }
    else
      {}
    end
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mongoid-direct-s3-upload-0.1.7 app/controllers/s3_relay/uploads_controller.rb
mongoid-direct-s3-upload-0.1.6 app/controllers/s3_relay/uploads_controller.rb
mongoid-direct-s3-upload-0.1.5 app/controllers/s3_relay/uploads_controller.rb
mongoid-direct-s3-upload-0.1.4 app/controllers/s3_relay/uploads_controller.rb
mongoid-direct-s3-upload-0.1.3 app/controllers/s3_relay/uploads_controller.rb