Sha256: f53a06b1b82f4dc0609a6ffda6b4ce530a9426e6947356c08e84370aeb9b31c6

Contents?: true

Size: 1.06 KB

Versions: 4

Compression:

Stored size: 1.06 KB

Contents

module Blocky
  class ImagesController < Blocky::ApplicationController

    before_action :authorize_user

    def create
      if s3_bucket
        file = params[:file]
        time_file = create_filename(file)
        s3_object = s3_bucket.objects.create(time_file, file.tempfile, {
          acl: :public_read
        })
        render text: s3_object.public_url.to_s
      else
        throw Exception, "S3 not setup"
      end
    end

    private

    def authorize_user
      authorize! :manage, Blocky::ContentBlock
    end

    def s3_bucket
      if Blocky.s3_access_key_id && Blocky.s3_secret_access_key && Blocky.s3_bucket
        s3 = AWS::S3.new(
          access_key_id: Blocky.s3_access_key_id,
          secret_access_key: Blocky.s3_secret_access_key
        )
        s3.buckets[Blocky.s3_bucket]
      end
    end

    def create_filename(file)
      file_ext = File.extname file.original_filename
      file_base = File.basename file.original_filename, file_ext
      time = Time.now.to_i
      file_with_time = "#{file_base}_#{time}#{file_ext}"
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
blocky-0.0.11 app/controllers/blocky/images_controller.rb
blocky-0.0.10 app/controllers/blocky/images_controller.rb
blocky-0.0.9 app/controllers/blocky/images_controller.rb
blocky-0.0.8 app/controllers/blocky/images_controller.rb