class Refile::Backend::FileSystem

A backend which stores uploaded files in the local filesystem

@example

backend = Refile::Backend::FileSystem.new("some/path")
file = backend.upload(StringIO.new("hello"))
backend.read(file.id) # => "hello"

Attributes

directory[R]

@return [String] the directory where files are stored

max_size[R]

@return [String] the maximum size of files stored in this backend

Public Class Methods

new(directory, max_size: nil, hasher: Refile::RandomHasher.new) click to toggle source

Creates the given directory if it doesn't exist.

@param [String] directory The path to a directory where files should be stored @param [Integer, nil] max_size The maximum size of an uploaded file @param [#hash] hasher A hasher which is used to generate ids from files

# File lib/refile/backend/file_system.rb, line 23
def initialize(directory, max_size: nil, hasher: Refile::RandomHasher.new)
  @hasher = hasher
  @directory = directory
  @max_size = max_size

  FileUtils.mkdir_p(@directory)
end

Public Instance Methods

clear!(confirm = nil) click to toggle source

Remove all files in this backend. You must confirm the deletion by passing the symbol `:confirm` as an argument to this method.

@example

backend.clear!(:confirm)

@raise [Refile::Confirm] Unless the `:confirm` symbol has been passed. @param [:confirm] confirm Pass the symbol `:confirm` to confirm deletion. @return [void]

# File lib/refile/backend/file_system.rb, line 103
def clear!(confirm = nil)
  raise Refile::Confirm unless confirm == :confirm
  FileUtils.rm_rf(@directory)
  FileUtils.mkdir_p(@directory)
end
delete(id) click to toggle source
# File lib/refile/backend/file_system.rb, line 58
          def delete(id)
  FileUtils.rm(path(id)) if exists?(id)
end
exists?(id) click to toggle source
# File lib/refile/backend/file_system.rb, line 91
          def exists?(id)
  ::File.exist?(path(id))
end
get(id) click to toggle source
# File lib/refile/backend/file_system.rb, line 50
          def get(id)
  Refile::File.new(self, id)
end
open(id) click to toggle source
# File lib/refile/backend/file_system.rb, line 67
          def open(id)
  ::File.open(path(id), "rb")
end
path(id) click to toggle source
# File lib/refile/backend/file_system.rb, line 113
          def path(id)
  ::File.join(@directory, id)
end
read(id) click to toggle source
# File lib/refile/backend/file_system.rb, line 75
          def read(id)
  ::File.read(path(id)) if exists?(id)
end
size(id) click to toggle source
# File lib/refile/backend/file_system.rb, line 83
          def size(id)
  ::File.size(path(id)) if exists?(id)
end
upload(uploadable) click to toggle source
# File lib/refile/backend/file_system.rb, line 35
                  def upload(uploadable)
  id = @hasher.hash(uploadable)
  IO.copy_stream(uploadable, path(id))

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