<commit_before>require 'fog'

module Dragonfly
  module DataStorage

    class S3DataStore

      include Configurable
      include Serializer

      configurable_attr :bucket_name
      configurable_attr :access_key_id
      configurable_attr :secret_access_key
      configurable_attr :use_filesystem, true
      configurable_attr :region
      configurable_attr :storage_headers, {'x-amz-acl' => 'public-read'}

      REGIONS = {
        'us-east-1'      => 's3.amazonaws.com',  #default
        'eu-west-1'      => 's3-eu-west-1.amazonaws.com',
        'ap-southeast-1' => 's3-ap-southeast-1.amazonaws.com',
        'us-west-1'      => 's3-us-west-1.amazonaws.com'
      }

      def initialize(opts={})
        self.bucket_name = opts[:bucket_name]
        self.access_key_id = opts[:access_key_id]
        self.secret_access_key = opts[:secret_access_key]
        self.region = opts[:region]
      end

      def store(temp_object, opts={})
        ensure_configured
        ensure_bucket_initialized
        
        meta = opts[:meta] || {}
        headers = opts[:headers] || {}
        uid = opts[:path] || generate_uid(meta[:name] || temp_object.original_filename || 'file')
        
        rescuing_socket_errors do
          if use_filesystem
            temp_object.file do |f|
              storage.put_object(bucket_name, uid, f, full_storage_headers(headers, meta))
            end
          else
            storage.put_object(bucket_name, uid, temp_object.data, full_storage_headers(headers, meta))
          end
        end
        
        uid
      end

      def retrieve(uid)
        ensure_configured
        response = rescuing_socket_errors{ storage.get_object(bucket_name, uid) }
        [
          response.body,
          parse_s3_metadata(response.headers)
        ]
      rescue Excon::Errors::NotFound => e
        raise DataNotFound, "#{e} - #{uid}"
      end

      def destroy(uid)
        rescuing_socket_errors{ storage.delete_object(bucket_name, uid) }
      rescue Excon::Errors::NotFound => e
        raise DataNotFound, "#{e} - #{uid}"
      end

      def url_for(uid, opts={})
        if opts && opts[:expires]
          storage.get_object_https_url(bucket_name, uid, opts[:expires])
        else
          "http://#{bucket_name}.s3.amazonaws.com/#{uid}"
        end
      end

      def domain
        REGIONS[get_region]
      end

      def storage
        @storage ||= Fog::Storage.new(
          :provider => 'AWS',
          :aws_access_key_id => access_key_id,
          :aws_secret_access_key => secret_access_key,<commit_msg>fallback for older versions of fog<commit_after>require 'fog'

module Dragonfly
  module DataStorage

    class S3DataStore

      include Configurable
      include Serializer

      configurable_attr :bucket_name
      configurable_attr :access_key_id
      configurable_attr :secret_access_key
      configurable_attr :use_filesystem, true
      configurable_attr :region
      configurable_attr :storage_headers, {'x-amz-acl' => 'public-read'}

      REGIONS = {
        'us-east-1'      => 's3.amazonaws.com',  #default
        'eu-west-1'      => 's3-eu-west-1.amazonaws.com',
        'ap-southeast-1' => 's3-ap-southeast-1.amazonaws.com',
        'us-west-1'      => 's3-us-west-1.amazonaws.com'
      }

      def initialize(opts={})
        self.bucket_name = opts[:bucket_name]
        self.access_key_id = opts[:access_key_id]
        self.secret_access_key = opts[:secret_access_key]
        self.region = opts[:region]
      end

      def store(temp_object, opts={})
        ensure_configured
        ensure_bucket_initialized
        
        meta = opts[:meta] || {}
        headers = opts[:headers] || {}
        uid = opts[:path] || generate_uid(meta[:name] || temp_object.original_filename || 'file')
        
        rescuing_socket_errors do
          if use_filesystem
            temp_object.file do |f|
              storage.put_object(bucket_name, uid, f, full_storage_headers(headers, meta))
            end
          else
            storage.put_object(bucket_name, uid, temp_object.data, full_storage_headers(headers, meta))
          end
        end
        
        uid
      end

      def retrieve(uid)
        ensure_configured
        response = rescuing_socket_errors{ storage.get_object(bucket_name, uid) }
        [
          response.body,
          parse_s3_metadata(response.headers)
        ]
      rescue Excon::Errors::NotFound => e
        raise DataNotFound, "#{e} - #{uid}"
      end

      def destroy(uid)
        rescuing_socket_errors{ storage.delete_object(bucket_name, uid) }
      rescue Excon::Errors::NotFound => e
        raise DataNotFound, "#{e} - #{uid}"
      end

      def url_for(uid, opts={})
        if opts && opts[:expires]
          if storage.respond_to?(:get_object_https_url) # fog's get_object_url is deprecated (aug 2011)
            storage.get_object_https_url(bucket_name, uid, opts[:expires])
          else
            storage.get_object_url(bucket_name, uid, opts[:expires])
          end
        else
          "http://#{bucket_name}.s3.amazonaws.com/#{uid}"
        end
      end

      def domain
        REGIONS[get_region]
      end

      def storage
        @storage ||= Fog::Storage.new(
          :provider => 'AWS',
          :aws_access_key_id => access_key_id,
          :aws_secret_access_key => secret_access_key,
{'fraction_non_alphanumeric': 0.10748097271208465, 'fraction_numerical': 0.005940226471134212, 'mean_word_length': 2.1843971631205674, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6190542', 'n_tokens_mistral': 1660, 'n_tokens_neox': 1639, 'n_words': 301}