Sha256: 7a36b08a810d32806591501630a7314b0996aed61923810aff384aabf885dd9e

Contents?: true

Size: 1.23 KB

Versions: 8

Compression:

Stored size: 1.23 KB

Contents

require 'fog/aws'

module Adhoq
  module Storage
    class S3 < FogStorage
      def initialize(bucket, s3_options = {})
        @bucket                  = bucket
        @direct_download         = s3_options.delete(:direct_download)
        @direct_download_options = s3_options.delete(:direct_download_options) || default_direct_download_options
        @s3 = Fog::Storage.new({provider: 'AWS'}.merge(s3_options))
      end

      def direct_download?
        @direct_download
      end

      def identifier
        "s3://#{@bucket}"
      end

      def get_url(report)
        get_raw(report.identifier).url(
          1.minutes.from_now.to_i,
          @direct_download_options.call(report)
        )
      end

      private

      def directory
        return @directory if @directory

        @directory = @s3.directories.get(@bucket) || @s3.directories.create(key: @bucket, public: false)
      end

      def default_direct_download_options
        proc do |report|
          {
            query: {
              'response-content-disposition' => "attachment; filename*=UTF-8''#{URI.encode_www_form_component(report.name)}",
              'response-content-type' => report.mime_type,
            }
          }
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
adhoq-1.0.2 lib/adhoq/storage/s3.rb
adhoq-1.0.1 lib/adhoq/storage/s3.rb
adhoq-1.0.0 lib/adhoq/storage/s3.rb
adhoq-0.5.0 lib/adhoq/storage/s3.rb
adhoq-0.5.0.beta1 lib/adhoq/storage/s3.rb
adhoq-0.4.0 lib/adhoq/storage/s3.rb
adhoq-0.3.0 lib/adhoq/storage/s3.rb
adhoq-0.2.0 lib/adhoq/storage/s3.rb