class Refile::Fog::Backend

Attributes

directory[R]
max_size[R]

Public Class Methods

new(directory:, max_size: nil, prefix: nil, hasher: Refile::RandomHasher.new, connection: nil, **options) click to toggle source
# File lib/refile/fog.rb, line 14
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

Public Instance Methods

clear!(confirm = nil) click to toggle source
# File lib/refile/fog.rb, line 62
def clear!(confirm = nil)
  raise Refile::Confirm unless confirm == :confirm
  @directory.files.select { |f| f.key.start_with?(@prefix.to_s) }.each(&:destroy)
end
delete(id) click to toggle source
# File lib/refile/fog.rb, line 35
          def delete(id)
  file = head(id)
  file.destroy if file
end
exists?(id) click to toggle source
# File lib/refile/fog.rb, line 54
          def exists?(id)
  !!head(id)
end
get(id) click to toggle source
# File lib/refile/fog.rb, line 31
          def get(id)
  Refile::File.new(self, id)
end
head(id) click to toggle source
# File lib/refile/fog.rb, line 58
          def head(id)
  @directory.files.head(path(id))
end
open(id) click to toggle source
# File lib/refile/fog.rb, line 40
          def open(id)
  StringIO.new(read(id))
end
read(id) click to toggle source
# File lib/refile/fog.rb, line 44
          def read(id)
  file = @directory.files.get(path(id))
  file.body if file
end
size(id) click to toggle source
# File lib/refile/fog.rb, line 49
          def size(id)
  file = head(id)
  file.content_length if file
end
upload(uploadable) click to toggle source
# File lib/refile/fog.rb, line 23
                  def upload(uploadable)
  id = @hasher.hash(uploadable)

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

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

Private Instance Methods

path(id) click to toggle source
# File lib/refile/fog.rb, line 69
          def path(id)
  ::File.join(*@prefix, id)
end