Sha256: b3349431cb2cb83fa03a8cc8e460fd1ca73b844cedff076886eee07c8356264b

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

require "fog"
require "refile"
require "refile/fog/version"

module Refile
  module Fog
    class Backend
      extend Refile::BackendMacros

      attr_reader :directory, :max_size

      def initialize(directory:, max_size: nil, prefix: nil, hasher: Refile::RandomHasher.new, connection: nil, **options)
        @connection = connection || ::Fog::Storage.new(options)
        @prefix = prefix
        @hasher = hasher
        @max_size = max_size

        @directory = @connection.directories.new(key: directory)
      end

      verify_uploadable def upload(uploadable)
        id = @hasher.hash(uploadable)

        @directory.files.create(key: path(id), body: uploadable.read)

        Refile::File.new(self, id)
      end

      verify_id def get(id)
        Refile::File.new(self, id)
      end

      verify_id def delete(id)
        file = head(id)
        file.destroy if file
      end

      verify_id def open(id)
        StringIO.new(read(id))
      end

      verify_id def read(id)
        file = @directory.files.get(path(id))
        file.body if file
      end

      verify_id def size(id)
        file = head(id)
        file.content_length if file
      end

      verify_id def exists?(id)
        !!head(id)
      end

      verify_id def head(id)
        @directory.files.head(path(id))
      end

      def clear!(confirm = nil)
        raise Refile::Confirm unless confirm == :confirm
        @directory.files.select { |f| f.key.start_with?(@prefix.to_s) }.each(&:destroy)
      end

    private

      verify_id def path(id)
        ::File.join(*@prefix, id)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
refile-fog-0.2.0 lib/refile/fog.rb