module SendS3File

Constants

VERSION

Public Instance Methods

send_s3_file(s3_object, options = {}) click to toggle source
# File lib/send_s3_file.rb, line 9
def send_s3_file s3_object, options = {}
  headers.merge!(
      'Content-Type'              => build_content_type(options),
      'Content-Disposition'       => build_disposition(options),
      'Content-Transfer-Encoding' => 'binary',
      'Content-Length' => s3_object.content_length
  )

  render :status => options[:status], :text => Proc.new {|response, output|
    logger.info "Streaming file from s3 #{s3_object.bucket} #{s3_object.key}" unless logger.nil?
    s3_object.read do |chunk|
      output.write(chunk)
    end
    logger.info "Done Streaming file from s3 #{s3_object.bucket} #{s3_object.key}" unless logger.nil?
  }
end

Private Instance Methods

build_content_type(options) click to toggle source
# File lib/send_s3_file.rb, line 33
def build_content_type(options)
  content_type = options[:type]
  if content_type.is_a?(Symbol)
    raise ArgumentError, "Unknown MIME type #{options[:type]}" unless Mime::EXTENSION_LOOKUP.has_key?(content_type.to_s)
    content_type = Mime::Type.lookup_by_extension(content_type.to_s)
  end
  content_type = content_type.to_s.strip
end
build_disposition(options) click to toggle source
# File lib/send_s3_file.rb, line 27
def build_disposition(options)
   disposition = (options[:disposition] || 'attachment').dup
   disposition <<= %(; filename="#{options[:filename]}") if options[:filename]
   disposition
end