class S3AssetsUploader::Uploader

Public Class Methods

new(&block) click to toggle source
# File lib/s3_assets_uploader/uploader.rb, line 6
def initialize(&block)
  @config = Config.new
  block.call(@config)
  @config.validate!
end

Public Instance Methods

upload() click to toggle source
# File lib/s3_assets_uploader/uploader.rb, line 12
def upload
  upload_path(@config.assets_path)
  @config.additional_paths.each do |path|
    upload_path(Pathname.new(path))
  end
end

Private Instance Methods

compute_asset_key(path) click to toggle source
# File lib/s3_assets_uploader/uploader.rb, line 39
def compute_asset_key(path)
  if @config.assets_prefix
    File.join(@config.assets_prefix, relative_path(path))
  else
    relative_path(path).to_s
  end
end
guess_content_type(path) click to toggle source
# File lib/s3_assets_uploader/uploader.rb, line 47
def guess_content_type(path)
  content_type = @config.guess_content_type(path)
  return content_type if content_type

  mime_type = MIME::Types.type_for(path.basename.to_s).first
  if mime_type
    mime_type.content_type
  else
    'application/octet-stream'
  end
end
relative_path(path) click to toggle source
# File lib/s3_assets_uploader/uploader.rb, line 59
def relative_path(path)
  path.relative_path_from(@config.public_path)
end
upload_path(path) click to toggle source
# File lib/s3_assets_uploader/uploader.rb, line 21
def upload_path(path)
  if path.directory?
    path.each_child do |c|
      upload_path(c)
    end
  else
    path.open do |f|
      @config.s3_client.put_object(
        body: f,
        bucket: @config.bucket,
        key: compute_asset_key(path),
        content_type: guess_content_type(path),
        cache_control: @config.cache_control,
      )
    end
  end
end