Sha256: cb109e16f960f49e708c1aec76ee5504a30d47dab7acb58363f037f880d47be4

Contents?: true

Size: 1.69 KB

Versions: 2

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

require 'aws-sdk-s3'
require 'flatiron_s3_uploader/exceptions'

require 'dotenv/load' if ENV['GEM_ENV'] == 'development'

module FlatironS3Uploader
  autoload :CLI,                  'flatiron_s3_uploader/cli'
  autoload :FileUploader,         'flatiron_s3_uploader/file_uploader'
  autoload :ImageResizer,         'flatiron_s3_uploader/image_resizer'
  autoload :VERSION,              'flatiron_s3_uploader/version'

  # Uploads an image file from the specified filepath to the specified S3 bucket.
  # Returns a URL for the uploaded image.
  #
  # Options are:
  # [:width]
  #   The width of the resized image. If a +height+ is not specified, the height
  #   will scale based on the specified width.
  # [:height]
  #   The height of the resized image. If a +width+ is not specified, the width
  #   will scale based on the specified height.
  # [:directory]
  #   The directory of the S3 bucket to upload to (i.e. 'phase-3/lab-name')
  #
  # Examples:
  #   FlatironS3Uploader.upload('path/to/image.png', 'bucket-name', { width: 400 })
  #   # => "https://bucket-name.s3.amazonaws.com/image.png"
  #
  #   FlatironS3Uploader.upload('path/to/image.png', 'bucket-name', { width: 400, directory: 'folder/subfolder' })
  #   # => "https://bucket-name.s3.amazonaws.com/folder/subfolder/image.png"
  #
  def self.upload(filepath, bucket, options = {})
    resizer = ImageResizer.new(filepath)
    filepath = resizer.resize(width: options[:width], height: options[:height]) if options[:width] || options[:height]
    mime_type = resizer.mime_type

    image = File.open(filepath)

    client = Aws::S3::Client.new
    FileUploader.new(client).upload(bucket, image, mime_type, options[:directory])
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
flatiron-s3-uploader-0.1.2 lib/flatiron_s3_uploader.rb
flatiron-s3-uploader-0.1.1 lib/flatiron_s3_uploader.rb