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
@return [String] the directory where files are stored
@return [String] the maximum size of files stored in this backend
Public Class Methods
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
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 105 def clear!(confirm = nil) raise Refile::Confirm unless confirm == :confirm FileUtils.rm_rf(@directory) FileUtils.mkdir_p(@directory) end
# File lib/refile/backend/file_system.rb, line 60 def delete(id) FileUtils.rm(path(id)) if exists?(id) end
# File lib/refile/backend/file_system.rb, line 93 def exists?(id) ::File.exist?(path(id)) end
# File lib/refile/backend/file_system.rb, line 52 def get(id) Refile::File.new(self, id) end
# File lib/refile/backend/file_system.rb, line 69 def open(id) ::File.open(path(id), "rb") end
# File lib/refile/backend/file_system.rb, line 115 def path(id) ::File.join(@directory, id) end
# File lib/refile/backend/file_system.rb, line 77 def read(id) ::File.read(path(id)) if exists?(id) end
# File lib/refile/backend/file_system.rb, line 85 def size(id) ::File.size(path(id)) if exists?(id) end
# 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) ensure uploadable.close end