Sha256: c38c1914bc9f127a915dd5b99bb37d1e07dbc5b577d0a42a03cb95a723bf8196

Contents?: true

Size: 1.03 KB

Versions: 12

Compression:

Stored size: 1.03 KB

Contents

require_dependency "blogelator/admin/application_controller"

module Blogelator
  module Admin
    class ImagesController < ApplicationController
      before_action :authorize_user
      respond_to :json
      
      # POST /api/images
      def create
        if s3_bucket
          file = params[:file]
          s3_object = s3_bucket.objects.create(file.original_filename, file.tempfile, {
            acl: :public_read
          })
          render json: { filename: s3_object.public_url.to_s }
        else
          throw Exception, "S3 not setup"
        end
      end
      
    private
    
      def authorize_user
        authorize! :manage, Blogelator::Post
      end
      
      def s3_bucket
        if Blogelator.s3_access_key_id && Blogelator.s3_secret_access_key && Blogelator.s3_bucket
          s3 = AWS::S3.new(
            access_key_id: Blogelator.s3_access_key_id,
            secret_access_key: Blogelator.s3_secret_access_key
          )
          s3.buckets[Blogelator.s3_bucket]
        end
      end
      
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
blogelator-0.2.5 app/controllers/blogelator/admin/images_controller.rb
blogelator-0.2.4 app/controllers/blogelator/admin/images_controller.rb
blogelator-0.2.3 app/controllers/blogelator/admin/images_controller.rb
blogelator-0.2.2 app/controllers/blogelator/admin/images_controller.rb
blogelator-0.2.1 app/controllers/blogelator/admin/images_controller.rb
blogelator-0.1.6 app/controllers/blogelator/admin/images_controller.rb
blogelator-0.1.5 app/controllers/blogelator/admin/images_controller.rb
blogelator-0.1.4 app/controllers/blogelator/admin/images_controller.rb
blogelator-0.1.3 app/controllers/blogelator/admin/images_controller.rb
blogelator-0.1.2 app/controllers/blogelator/admin/images_controller.rb
blogelator-0.1.1 app/controllers/blogelator/admin/images_controller.rb
blogelator-0.1.0 app/controllers/blogelator/admin/images_controller.rb