Sha256: 6be1b32f0dad1a46486c4a6953d794657d1f69ea39827c56b4f96564aae39b02

Contents?: true

Size: 1.8 KB

Versions: 6

Compression:

Stored size: 1.8 KB

Contents

require 'attached/storage/base'
require 'fog'


module Attached
  module Storage
    class Fog < Base


      attr_reader :defaults

      attr_reader :bucket
      attr_reader :access_key_id
      attr_reader :secret_access_key


      # Create a new interface supporting save and destroy operations (should be overridden and called).

      def initialize(credentials)
        @defaults = { :public => true, :metadata => Attached::Attachment.options[:metadata] }
      end


      # Access the host for a storage service (must override).
      #
      # Usage:
      #
      #   storage.host

      def host()
        raise NotImplementedError.new
      end


      # Save a file to a given path.
      #
      # Parameters:
      #
      # * file - The file to save.
      # * path - The path to save.

      def save(file, path)
        file = File.open(file.path)
        directory.files.create(self.options(path).merge(self.defaults.merge(:key => path, :body => file)))
        file.close
      end


      # Retrieve a file from a given path.
      #
      # Parameters:
      #
      # * path - The path to retrieve.

      def retrieve(path)
        file = directory.files.get(path)

        body = file.body

        extname = File.extname(path)
        basename = File.basename(path, extname)

        file = Tempfile.new([basename, extname])
        file.binmode
        file.write(body)
        file.rewind

        file
      end


      # Destroy a file at a given path.
      #
      # Parameters:
      #
      # * path - The path to destroy.

      def destroy(path)
        directory.files.get(path).destroy if directory.files.head(path)
      end


    private


      def directory
        raise NotImplementedError.new
      end


      def connection
        raise NotImplementedError.new
      end


    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
attached-1.0.4 lib/attached/storage/fog.rb
attached-1.0.3 lib/attached/storage/fog.rb
attached-1.0.2 lib/attached/storage/fog.rb
attached-1.0.1 lib/attached/storage/fog.rb
attached-1.0.0 lib/attached/storage/fog.rb
attached-0.6.0 lib/attached/storage/fog.rb